【问题标题】:update Collection record in mongo更新 mongo 中的 Collection 记录
【发布时间】:2018-05-07 06:00:21
【问题描述】:

我目前正在为我当前的项目开发 mongodb。

{
    "_id" : ObjectId("5a168f467cf3661df0df9c11"),
    "player_id" : "5a02db1170aaf41013d32747",
    "quiz_type" : "Single",
    "created_date" : ISODate("2017-11-23T09:05:10Z"),
    "questions_answered" : [
        {
            "question_id" : ObjectId("5a0ac1bfa9897441e038c2f7"),
            "player_selection_status" : "Pending",
            "time_taken" : 0,
            "points_gained" : 0,
            "like" : 0
        },
        {
            "question_id" : ObjectId("5a0ac212a9897441e038c2f8"),
            "player_selection_status" : "Pending",
            "time_taken" : 0,
            "points_gained" : 0,
            "like" : 0
        }
    ],
    "__v" : 0
}

以上是我在玩家收藏中的记录,我想更新第二个

"questions_answered" : [  {
            "question_id" : ObjectId("5a0ac212a9897441e038c2f8"),
            "player_selection_status" : "Pending",
            "time_taken" : 0,
            "points_gained" : 0,
            "like" : 0
 }

喜欢

"player_selection_status" : "Correct",
"time_taken" : 10,
"points_gained": 5,
"like": 10,
"answered_date":ISODate("2017-11-23T09:05:10Z")

我试过如下

updateData = {questions_answered: {time_taken: 10, like: 1, 
     answered_date: moment().format()}};
Player_quiz.update({_id: qid, player_id: uid, 
    "questions_answered.question_id": question_id}, 
    {$set: updateData}).exec();

但这对我不起作用..请帮助我解决正确的解决方案??

【问题讨论】:

    标签: mongodb mongoose mongodb-query


    【解决方案1】:

    您必须使用positional operator $ 进行数组更新。这里我们有 questions_answered 作为文档数组。

    更新文档的查询是

    db.collection.update(
        {"questions_answered.question_id" : ObjectId("5a0ac212a9897441e038c2f8")},
        {$set: { 
          "questions_answered.$.time_taken":10,
          "questions_answered.$.player_selection_status" : "Correct",
          "questions_answered.$.points_gained": 5, 
          "questions_answered.$.like": 10, 
          "questions_answered.$.answered_date":ISODate("2017-11-23T09:05:10Z")
        }}
     );
    

    【讨论】:

      【解决方案2】:
      db.collection.update({
              questions_answered: {
                  $elemMatch: {
                      "question_id": ObjectId("5a0ac212a9897441e038c2f8")
                  }
              }
          }, {
              $set: {
                  "questions_answered.$.player_selection_status": "Correct",
                  "questions_answered.$.time_taken": 10,
                  "questions_answered.$.points_gained": 5,
                  "questions_answered.$.like": 10,
                  "questions_answered.$.answered_date": ISODate("2017-11-23T09:05:10Z")
              }
      
          }
      
      )
      

      在上述查询中,$elemMatch 运算符用于匹配数组中的元素

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 2020-10-07
        • 1970-01-01
        • 1970-01-01
        • 2015-10-17
        • 1970-01-01
        • 2020-12-29
        相关资源
        最近更新 更多