我想在我的猫鼬模型中有一个对象(例如“成分”),其中键是ObjectID,其值是数字。有可能这样做吗?我应该如何定义我的猫鼬模式?您可以在下面找到一个示例。
JSON示例:
{
"_id": ""5a2539b41c574006c46f1a07",
"name": "xyz",
"ingredients": {
"5a23f5e6159f5c3438c75971": 50,
"5a23f60b159f5c3438c75972": 50,
"5a255b04c9d9c40ac8927dd5": 50
}
}
提前谢谢你的帮助。
你可以使用混合模式
{
"_id": ""5a2539b41c574006c46f1a07",
"name": "xyz",
"ingredients": mongoose.Schema.Types.mix
}
插入动态密钥是如此简单
insertData_dynamic_colone: function(collection, colone1, colone2) {
var obj = {};
obj[colone1] = "14";
obj[colone2] = "15";
dbObject.collection(collection).insertOne(obj, function(err, result) {
assert.equal(err, null);
});
}
我知道您将来还需要更新动态密钥以参考更新Mongo数组:删除动态密钥
collection.update(
{"_id": ObjectId("5a2539b41c574006c46f1a07")},
{"$unset": {"ingredients.5a23f5e6159f5c3438c75971": ""}}
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句