我正在尝试将元素推到现有文档的数组中。 该文档具有以下结构:
{
"_id" : ObjectId("5b72d3445e5f180b6cfeca41"),
"mediaarray" : [],
"description" : "",
"name" : "Test name",
"type" : "7",
"comments" : [
{
"_id" : ObjectId("5b8830a1df9c832473540014"),
"comment" : "Test!",
"createdBy" : "5b486e310760ebebe20e6537",
"updatedBy" : "5b486e310760ebebe20e6537",
"created" : ISODate("2018-08-30T18:00:01.232Z"),
"updated" : ISODate("2018-08-30T18:00:01.232Z")
}
]
}
我可以在“ comments”数组中添加评论,但是不能添加对该评论的回复。我认为我想要达到的目标是这样的:
{
"_id" : ObjectId("5b72d3445e5f180b6cfeca41"),
"mediaarray" : [],
"description" : "",
"name" : "Test name",
"type" : "7",
"comments" : [
{
"_id" : ObjectId("5b8830a1df9c832473540015"),
"comment" : "Test again!",
"createdBy" : "5b486e310760ebebe20e6537",
"updatedBy" : "5b486e310760ebebe20e6537",
"created" : ISODate("2018-08-30T18:00:01.232Z"),
"updated" : ISODate("2018-08-30T18:00:01.232Z"),
"replies" : [
{
"_id" : ObjectId("5b8830a1df9c832473540014"),
"comment" : "reply!",
"createdBy" : "5b486e310760ebebe20e6537",
"updatedBy" : "5b486e310760ebebe20e6537",
"created" : ISODate("2018-08-30T18:00:01.232Z"),
"updated" : ISODate("2018-08-30T18:00:01.232Z")
}
]
}
]
}
我正在尝试使用这种mongo方法,但是它似乎没有用。它会删除文档上的所有信息(媒体数组,描述,名称,类型),并且仅保留注释,而不会添加新添加的回复。
document.update({
"_id" : documentid,
"comments" : { "$elemMatch" : { "_id" : { "$eq" : parent } } }
},{'$push':{'comments':{replyobj}},function(err,doc){
if(err){
console.log(err);
}else{
console.log("NO error: "+doc);
}
}
});