簡體   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" : [{}]
        }
    ]
}]

以上是模型的 json 格式(來自 mongobooter)讓我們說“行”,我有 _id 和 dots.id,我想將新對象添加到位置。 那我該怎么做(使用貓鼬)?

您可以選擇:

貓鼬對象方式:

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

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