Mongoose variable key name

你。 提交于 2019-11-28 12:11:59

You'll be better off if you avoid dynamic keys in your schema and go with your second idea of:

user_info: [{sessionid: String, value: String}]

You can use the $ positional operator to update individual user_info array elements by sessionid.

You may try with Schema Type Mixed like this way

var user = new Schema({
   info:    [Schema.Types.Mixed]
 });

user.info = { any: { thing: 'i want' } };
user.markModified('info');

You can read more about it here

After testing the above, I found that defining the schema as user_info: { String: String } is a valid way to do this (option 1 specified in the question).

You may define objects and arrays in your schema. You may even combine them. For example, this is an array of objects:

var user = new Schema({
    foo: [ {
        address: {type: String},
        email: {type: String, unique: true}
    }],
    bar: [ "simple", "array" ]
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!