我的文档结构如下
{
"_id": {
"$oid": "55ae24016fb73f6ac7c2d640"
},
"Name": "some name",
"District": "some district",
"Stories": [
{
"userId": "105304831528398207103",
"story": "some story",
"Likes": ["user id 1" ..... ],
"_id": {
"$oid": "55c055af1875b0002572cf94"
}
}
]
}
现在,当用户喜欢一个故事时,我想将他的用户 ID 添加到数组Likes
我尝试了以下代码
router.post("/NewStoryLike", function (req, res) {
var mongo = require('mongodb');
var db = require('monk');
var ObjectID = mongo.ObjectID;
req.db.get('clnTemple').findAndModify({
query: { "_id": ObjectID(req.body.postId) ,"Stories._id": ObjectID(req.body.storyId) },
update: { $addToSet: { Likes: req.body.userId } },
upsert: true
});
res.send("Like Added");
});
它没有抛出任何错误,但它没有将用户 ID 添加到喜欢数组,而是在顶层添加一个喜欢,即添加到帖子级别
应该做出什么改变?