简体   繁体   English

如何在 mongodb 中添加到数组

[英]How do you add to array in mongodb

I just wanted to know how to add to a array in a mongodb document because I only know how to reassign the whole array in the document.我只是想知道如何添加到 mongodb 文档中的数组,因为我只知道如何在文档中重新分配整个数组。

As reported in MongoDB docs, if you have the following document in the students如 MongoDB 文档中所述,如果学生中有以下文档

collection:

{
   "_id" : 5,
   "quizzes" : [
      { "wk": 1, "score" : 10 },
      { "wk": 2, "score" : 8 },
      { "wk": 3, "score" : 5 },
      { "wk": 4, "score" : 6 }
   ]
}

you can add the following documents to the quizzes array with the following command:您可以使用以下命令将以下文档添加到 quizzes 数组中:

db.students.update(
   { _id: 5 },
   {
     $push: {
       quizzes: {
          $each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ]
       }
     }
   }
)

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

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