【发布时间】:2016-04-12 10:58:33
【问题描述】:
我有一个看起来像这样的猫鼬模型:
"question": {
"_id": "1234",
"title": "Hi",
"content": "Hello",
"viewed": 34425,
"answers": [{
"vote": 0,
"date": "2015-12-21T13:03:14.334Z",
"author": "john",
"content": "hello",
"comments": []
}, {
"vote": 0,
"date": "2015-12-21T13:05:27.411Z",
"author": "patrick",
"content": "bonjour",
"comments": []
}]
}
我想将评论推送到 question.answers[1].comment,但我无法使用 findOneAndUpdate 做到这一点。
这是我尝试过的:
MySchema.findOneAndUpdate({_id: questionId},
{$push:
{'answers.$.comments': {
'$each': [comment],
'$position': 1}
}
}, function(err, doc) {...
但我收到此错误:MongoError: exception: The positional operator did not find the match needed from the query. Unexpanded update: answers.$.comments。
【问题讨论】:
-
我刚刚编辑了问题,添加了错误。
标签: node.js mongodb mongoose mongodb-query