node.js 将 Json 对象存储在 Mongoose 字符串键中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17497875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing Json Object in Mongoose String key
提问by Saransh Mohapatra
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.
在我的 Mongoose 模式中,我有一个字符串字段,我希望能够在其中存储一个 JSON 对象。是否可以?在 Postgres 中,可以在字符串列中存储字典。
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.
我想这样做是因为字典(实际上是 JS 中的一个 JSON 对象)只是一个简单的读写值,不需要查询,而且,因为它只是一个值而不是一组值。
回答by Peter Lyons
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.
是的,您可以只存储{myJsonProperty: JSON.stringify(myObject)}. 希望您知道您也可以{myJsonProperty: Object}在 mongoose 模式中设置并存储整个对象,而无需无缘无故地将其转换为字符串。它不必是带有模式的嵌套文档,它可以只是一个普通的 javascript 对象。
回答by dam1
if you can change the type of your field form "String" to "Object" you can save the json as it is.
如果您可以将字段表单“字符串”的类型更改为“对象”,则可以按原样保存 json。
var schema_obj = new Schema({
field1: Object,
..
});
回答by Tom Spencer
The accepted answer is good for the majority of situations.
接受的答案适用于大多数情况。
However, if you have an object which you want to store, and you have no control over the object keys (e.g. they might be user-submitted), you might want to consider storing these as stringifed JSON. This allows you to overcome the limitation imposed by MongoDB that keys must not contain the reserved characters$or ..
但是,如果您有一个要存储的对象,并且您无法控制对象键(例如它们可能是用户提交的),您可能需要考虑将这些存储为字符串化 JSON。这使您可以克服 MongoDB 强加的限制,即键不得包含保留字符$或..
You can achieve this using Mongoose getters and setters, for example:
您可以使用 Mongoose getter 和 setter 来实现这一点,例如:
data: {
type: String,
get: function(data) {
try {
return JSON.parse(data);
} catch() {
return data;
}
},
set: function(data) {
return JSON.stringify(data);
}
}
回答by John Griffiths
Couldn't alter the original due to the 6 change limit on stack-overflow. re-posted, awesome work Tom, was just missing catch(err) in the catch block
由于堆栈溢出的 6 次更改限制,无法更改原始文件。重新发布,很棒的工作 Tom,只是在 catch 块中丢失了 catch(err)
data: {
type: String,
get: function(data) {
try {
return JSON.parse(data);
} catch(err) {
return data;
}
},
set: function(data) {
return JSON.stringify(data);
}
}
回答by MichaelWClark
We needed to retain structure and not do a string so I came up with the following methods to handle this:
我们需要保留结构而不是字符串,所以我想出了以下方法来处理这个问题:
const setMongoMixedWithBadKeys = data =>
Array.isArray(data)
? data.map(setMongoMixedWithBadKeys)
: typeof data === 'object'
? Object.entries(data).reduce((a, [key,value])=>({...a, [key.replace('.','__').replace('$','___')]:setMongoMixedWithBadKeys(value)}), {})
: data
const getMongoMixedWithBadKeys = data =>
Array.isArray(data)
? data.map(getMongoMixedWithBadKeys)
: typeof data === 'object'
? Object.entries(data).reduce((a, [key, value])=> ({...a, [key.replace('__','.').replace('___','$')]:getMongoMixedWithBadKeys(value)}), {})
: data
data: {
type: Mixed,
get: getMongoMixedWithBadKeys,
set: setMongoMixedWithBadKeys
}

