我正在尝试将文档推送到 mongoose 的嵌套数组中。我也成功地将文档保存在数组中。但我想要的是新保存的文档应该在回调函数中返回(因为它有我需要的 ObjectId)。下面是我的代码:
SocialPost.updateOne({_id: req.body._id, "comments._id": req.body.commentId},
{$push :{'comments.$.replies': {replyAuthor: req.body.replyAuthor, reply: req.body.reply,
authorProfile: req.body.authorProfile, dateOfReply: req.body.dateOfReply}}},
function(err,result){
if(err)
res.json({err});
else
{console.log(result);res.json({});}
})
控制台输出如下:
{
n: 1,
nModified: 1,
opTime: {
ts: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1624684305 },
t: 8
},
electionId: 7fffffff0000000000000008,
ok: 1,
'$clusterTime': {
clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1624684305 },
signature: { hash: [Binary], keyId: [Long] }
},
operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1624684305 }
}
我在上面的输出中看不到新插入的文档。
我的问题是如何检索新插入的文档(甚至是 repiles 数组或注释数组),以便将文档返回给用户?
谢谢!
编辑: 如果有人需要它,下面是架构:
const PostSchema=mongoose.Schema({
author: String,
uid: String,
email: String,
title: String,
userProfile: String,
description: String,
file: String,
fileType: String,
subGratis: String,
dateTime: String,
likes: Number,
hearts: Number,
comments:[
{
commentAuthor: String,
uid: String,
comment: String,
userProfile: String,
dateOfComment: String,
replies: [{
replyAuthor: String,
authorProfile: String,
reply: String,
dateOfReply: String
}]
}
]
},{collection: 'SocialPost'});