Storing Json Object in Mongoose String key

前端 未结 5 809
温柔的废话
温柔的废话 2021-01-30 04:14

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 i

相关标签:
5条回答
  • 2021-01-30 04:47

    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
    }
    
    0 讨论(0)
  • 2021-01-30 04:48

    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.

    0 讨论(0)
  • 2021-01-30 04:52

    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

    data: {
      type: String,
      get: function(data) {
        try { 
          return JSON.parse(data);
        } catch(err) { 
          return data;
        }
      },
      set: function(data) {
        return JSON.stringify(data);
      }
    }
    
    0 讨论(0)
  • 2021-01-30 04:55

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

    var schema_obj = new Schema({
    field1: Object,
    ..
    });
    
    0 讨论(0)
  • 2021-01-30 05:00

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

    You can achieve this using Mongoose getters and setters, for example:

    data: {
      type: String,
      get: function(data) {
        try { 
          return JSON.parse(data);
        } catch() { 
          return data;
        }
      },
      set: function(data) {
        return JSON.stringify(data);
      }
    }
    
    0 讨论(0)
提交回复
热议问题