在Mongoose String键中存储Json对象

时间:2022-07-26 02:35:42

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模式中,我有一个String字段,我希望能够在其中存储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对象)只是一个简单的读写值而且不需要查询,但是,因为它只是一个值而不是值数组。

4 个解决方案

#1


72  

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)}。希望你知道你也可以在你的mongoose架构中设置{myJsonProperty:Object}并存储整个对象,而无需将其转换为字符串。它不必是具有模式的嵌套文档,它可以只是一个普通的javascript对象。

#2


7  

if you can change the type of your field form "String" to "Object" you can save the json as it is.

如果您可以将字段“String”的类型更改为“Object”,则可以保存json。

var schema_obj = new Schema({
field1: Object,
..
});

#3


3  

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 ..

但是,如果您有一个要存储的对象,并且您无法控制对象键(例如,它们可能是用户提交的),您可能需要考虑将这些存储为stringifed 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);
  }
}

#4


0  

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个更改限制,无法更改原始文件。重新发布,令人敬畏的工作汤姆,只是在catch块中丢失了捕获(错误)

data: {
  type: String,
  get: function(data) {
    try { 
      return JSON.parse(data);
    } catch(err) { 
      return data;
    }
  },
  set: function(data) {
    return JSON.stringify(data);
  }
}

#1


72  

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)}。希望你知道你也可以在你的mongoose架构中设置{myJsonProperty:Object}并存储整个对象,而无需将其转换为字符串。它不必是具有模式的嵌套文档,它可以只是一个普通的javascript对象。

#2


7  

if you can change the type of your field form "String" to "Object" you can save the json as it is.

如果您可以将字段“String”的类型更改为“Object”,则可以保存json。

var schema_obj = new Schema({
field1: Object,
..
});

#3


3  

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 ..

但是,如果您有一个要存储的对象,并且您无法控制对象键(例如,它们可能是用户提交的),您可能需要考虑将这些存储为stringifed 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);
  }
}

#4


0  

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个更改限制,无法更改原始文件。重新发布,令人敬畏的工作汤姆,只是在catch块中丢失了捕获(错误)

data: {
  type: String,
  get: function(data) {
    try { 
      return JSON.parse(data);
    } catch(err) { 
      return data;
    }
  },
  set: function(data) {
    return JSON.stringify(data);
  }
}