我有这样的反对意见:
{
"_id": {
"$oid": "654625943108039f4960125b"
},
"text": "c1",
"replies": [{
"parentCommentId": {
"$oid": "654625943108039f4960125b"
},
"replyId": "f2ff36d9-ce4f-468c-98dd-0e6a0acf85c0",
"text": "c1-r1-r1-r3",
"immediateParent": "b0b1dda0-493a-473b-bc11-7efc9935e4fe",
"ancestors": [{
"parentReplyId": "b0b1dda0-493a-473b-bc11-7efc9935e4fe",
"commentId": "654625943108039f4960125b"
}]
}, {
"parentCommentId": {
"$oid": "654625943108039f4960125b"
},
"replyId": "5ecd2ce9-5347-4722-86d4-e35e52fbb3ef",
"text": "sample1",
"immediateParent": "312e2000-2793-4be7-8a20-f782d32401cc",
"ancestors": [{
"parentReplyId": "312e2000-2793-4be7-8a20-f782d32401cc",
"commentId": "654625943108039f4960125b"
}]
}
我想在Mongo 从上面的物体中找到replyId: 5ecd2ce9-5347-4722-86d4-e35e52fbb3ef
个.
我try 这样做,但它返回所有对象:
const replies = await genericDb.collection('comments').findOne(
{ _id: new ObjectId(commentId), 'replies.replyId': replyId },
{ 'ancestors.$': 1 }
);
最新情况:
try 了聚合管道,但仍未成功
const ancestors = await genericDb.collection('comments').aggregate([
{ $match: { _id: new ObjectId(commentId), "replies.replyId": replyId } },
{ $unwind: "$replies.ancestors" },
]).toArray();