更新mongo中的对象数组

时间:2018-01-23 10:46:14

标签: node.js mongoose

在db集合中记录

"_id" : ObjectId("5a66de1c69493c323d83a23d"), 
"students" : [
    {
        "studentId" : ObjectId("5a56fd3a02a50271d233a92f"), 
        "_id" : ObjectId("5a66de1c69493c323d83a23f")
    }, 
    {
        "studentId" : ObjectId("5a65ce5cc0533638e4e2df3f"), 
        "_id" : ObjectId("5a66de1c69493c323d83a23e")
    }
]

我有这个记录。现在我想用这条记录更新这个学生对象数组

要更新的值

"_id" : ObjectId("5a66de1c69493c323d83a23d"), 
"students" : [
    {
        "comments" : "not bad", 
        "marks" : NumberInt(50), 
        "studentId" : ObjectId("5a56fd3a02a50271d233a92f"), 
        "_id" : ObjectId("5a6709d49fb0e54697b9d7f5")
    }, 
    {
        "comments" : "good", 
        "marks" : NumberInt(100), 
        "studentId" : ObjectId("5a65ce5cc0533638e4e2df3f"), 
        "_id" : ObjectId("5a6709d49fb0e54697b9d7f4")
    }
]

当我尝试使用此查询时,它会向数组中添加额外的字段

node js

student.findOneAndUpdate({
  _id: req.body.id
}, {
  "$addToSet": {
    "students": {
      "$each": [
    {
        "comments" : "not bad", 
        "marks" : NumberInt(50), 
        "studentId" : ObjectId("5a56fd3a02a50271d233a92f"), 
        "_id" : ObjectId("5a6709d49fb0e54697b9d7f5")
    }, 
    {
        "comments" : "good", 
        "marks" : NumberInt(100), 
        "studentId" : ObjectId("5a65ce5cc0533638e4e2df3f"), 
        "_id" : ObjectId("5a6709d49fb0e54697b9d7f4")
    }
]
    }
  }
}, function(err, result) {
  if (err) {
    res.json({
      success: false,
      message: err
    })
  } else {
    res.status(200).json({
        success: true,
        message: "successfully updated"
      })
  }
});

现在我的数据库看起来像这样

"_id" : ObjectId("5a66de1c69493c323d83a23d"), 
 "students" : [
    {
        "studentId" : ObjectId("5a56fd3a02a50271d233a92f"), 
        "_id" : ObjectId("5a66de1c69493c323d83a23f")
    }, 
    {
        "studentId" : ObjectId("5a65ce5cc0533638e4e2df3f"), 
        "_id" : ObjectId("5a66de1c69493c323d83a23e")
    }, 
    {
        "comments" : "not bad", 
        "marks" : NumberInt(50), 
        "studentId" : ObjectId("5a56fd3a02a50271d233a92f"), 
        "_id" : ObjectId("5a6709d49fb0e54697b9d7f5")
    }, 
    {
        "comments" : "good", 
        "marks" : NumberInt(100), 
        "studentId" : ObjectId("5a65ce5cc0533638e4e2df3f"), 
        "_id" : ObjectId("5a6709d49fb0e54697b9d7f4")
    }
]

我需要在数组中找到studentId然后更新。但是这里所有数据都可以进入阵列。 所以任何人都可以告诉我如何通过id在对象数组中找到并用它更新。

提前感谢。

1 个答案:

答案 0 :(得分:1)

问题是 $ addToSet 用于添加到数组中,您需要使用的是 $ set

student.findOneAndUpdate({
  _id: req.body.id
}, {
  "$set": {
    "students": [
    {
        "comments" : "not bad", 
        "marks" : NumberInt(50), 
        "studentId" : ObjectId("5a56fd3a02a50271d233a92f"), 
        "_id" : ObjectId("5a6709d49fb0e54697b9d7f5")
    }, 
    {
        "comments" : "good", 
        "marks" : NumberInt(100), 
        "studentId" : ObjectId("5a65ce5cc0533638e4e2df3f"), 
        "_id" : ObjectId("5a6709d49fb0e54697b9d7f4")
    }
    ]
  }
}, function(err, result) {
  if (err) {
    res.json({
      success: false,
      message: err
    })
  } else {
    res.status(200).json({
        success: true,
        message: "successfully updated"
      })
  }
});