我需要在嵌套数组中推送一个数组。
我正在收集帖子,里面有注释数组,并且在注释数组里面回复数组就在那里。我需要在回复数组
中存在的likes数组中的推送值我的收藏结构是
"_id" : ObjectId("5a8dc1f67498b619070ed5bb"),
"comments" : [
{
"created_at" : ISODate("2018-02-21T19:24:15.977Z"),
"replies" : [
{
"created_at" : ISODate("2018-02-21T19:25:48.533Z"),
"likes" : [
{
"created_at" : ISODate("2018-02-25T06:24:20.111Z"),
"_id" : 1
}
],
"_id" : 1,
"content" : "check 2nd"
},
{
"created_at" : ISODate("2018-02-25T06:10:21.597Z"),
"likes" : [
{
"created_at" : ISODate("2018-02-25T06:50:51.076Z"),
"_id" : 2
}
],
"_id" : 2,
"content" : "check reply reply"
}
],
"_id" : ObjectId("5a8dc75f1dc5601cb4e3fe5d"),
"content" : "check 1st inner"
},
{
"created_at" : ISODate("2018-02-21T19:26:04.569Z"),
"likes" : [ ],
"replies" : [
{
"created_at" : ISODate("2018-02-21T19:26:14.308Z"),
"likes" : [ ],
"_id" : ObjectId("5a8dc7d63ed5a01dbaa4c3b9"),\
"content" : "check 3rd inner"
}
],
"_id" : ObjectId("5a8dc7cc3ed5a01dbaa4c3b8"),
"content" : "check 3rd"
}
],
我需要在其回复_id为'2'的数组中推送值,
我已经尝试过这段代码,
module.exports.replyLike = function(body, userId, callback){
const query = {
_id: body.postId,
"comments._id": body.commentId,
"comments.replies._id":body.replyId
};
Post.update(query, {$push: {"comments.$.replies.$.likes": {userId: userId} } }, callback);
}
但它不起作用,
如果我给它$push:{"comments.$.replies.1.likes"}
它有效,但希望它能动态添加。
有人在我的代码中指出了问题并给出了解决方案。