简体   繁体   English

将新对象插入到mongoose中的子文档数组字段中

[英]Insert a new object into a sub-document array field in mongoose

[{
    "_id" : ObjectId("579de5ad16944ccc24d5f4f1"),
    "dots" : 
    [
        {
            "id" : 1,
            "location" : 
            [
                 {
                    "lx" : 10,
                    "ly" : 10
                 }
            ]
        },
        {
            "id" : 2,
            "location" : [{}]
        }
    ]
}]

Above is json format of model (from mongobooter) let's say "lines", and i have _id and dots.id and i want to add new object into location.以上是模型的 json 格式(来自 mongobooter)让我们说“行”,我有 _id 和 dots.id,我想将新对象添加到位置。 then how can i do that (using mongoose)?那我该怎么做(使用猫鼬)?

You can choose between:您可以选择:

Mongoose Object-way:猫鼬对象方式:

document.dots[0].location.push({ /* your subdoc*/ });
document.save(callback);

Mongo/Mongoose Query (using$push and $ operator ): Mongo/Mongoose 查询(使用$push$运算符):

YourModel.update(
  {_id: /* doc id */, 'dots.id': /* subdoc id */ },
  {$push: {'dots.$.location': { /* your subdoc */ }},
  callback
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM