我需要更新一個數組錯誤而在一個陣列更新特定對象與貓鼬
{
"_id": "543e2f8e769ac100008777d0",
"createdDate": "2014-10-15 01:25 am",
"cancle": false,
"eventDateAndTime": "2014-02-12 12:55 am",
"eventstatus": true,
"userPhone": "44444444",
"userId": "54334def7e85de48638d1069",
"createdBy": "four",
"eventName": "real tea",
"__v": 0,
"friends": [
{
"phoneNumber": "11111111",
"userName": "one",
"userId": "54310801e2659ecc3650100b",
"status": 0
},
{
"phoneNumber": "22222222",
"userName": "two",
"userId": "54310814e2659ecc3650100c",
"status": 1
}
]
}
我嘗試了很多,我不知道我錯過了什麼內部的特定對象。
event.update(
function(err, eventSaved) {
if(err) {
res.json({'err':err});
}
})
我得到一個錯誤的響應
err: {
name: "MongoError"
code: 66
err: "After applying the update to the document {_id: ObjectId('543e2ecb74d70100001545ad') , ...}, the (immutable) field '_id' was found to have been altered to _id: ObjectId('543e2f8e769ac100008777d0')"
}
我也試過
event.update({'friends.userId': req.param.friendId}, {'$set': {
'friends.$.status': status }},
function(err, eventSaved, eve) {
if(err) {
}
})
同樣的錯誤。
請幫我找出我錯過了什麼,
我想即使是這樣
Events.findOneAndUpdate({_id: req.params.eventId}, {$set: {'friends[keyValue].status': 7 }}, {upsert: true, "new": false}).exec(function(err, thing) {
console.dir(thing);
});
沒有什麼工作。請幫我找出這個問題
感謝, 巴魯
從錯誤,似乎你正在試圖改變'_id' – avi 2014-10-17 07:54:52
不,我不是編號的ID。 – 2014-10-17 07:55:58
您的查詢對我有效 – Disposer 2014-10-17 08:16:22