在Mongoose字符串键中存储Json对象 [英] Storing Json Object in Mongoose String key

查看:404
本文介绍了在Mongoose字符串键中存储Json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Mongoose模式中,我有一个字符串字段,我希望能够在其中存储JSON对象.是否有可能?在Postgres中,可以将字典存储在字符串列中.

In my Mongoose schema, I have a field which is a String, and I want to be able to store an JSON object in it. Is it possible? In Postgres, it's possible to store a dictionary in a string column.

我想这样做是因为字典(实际上是JS中的JSON对象)只是一个简单的读写值,不需要查询,而且,因为它只是一个值而不是值的数组.

I want to do that because the dictionary (actually a JSON object in JS) is just a simple read and write value and doesn't need queries, but also, because it is just one value and not an array of values.

推荐答案

是的,您可以只存储{myJsonProperty: JSON.stringify(myObject)}.希望您知道,您也可以只在猫鼬模式中设置{myJsonProperty: Object}并存储整个对象,而无需将其无缘无故地转换为字符串.它不必是带有模式的嵌套文档,也可以只是普通的javascript对象.

Yes, you can just store {myJsonProperty: JSON.stringify(myObject)}. Hopefully you know that you can also just set {myJsonProperty: Object} in your mongoose schema and store the whole object without converting it to a string for no reason. It doesn't have to be a nested document with a schema, it can be just a plain javascript object.

这篇关于在Mongoose字符串键中存储Json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆