【发布时间】:2021-07-01 12:02:52
【问题描述】:
我有如下收藏:
{ "_id" : ObjectId("5716617f4af77ca97a9614bd"), "flag": { "status": true, "userId": ObjectId"606b0e9f00bece43471baa50")}, "text" : "Main Comment 1", "reply" : [ { "_id" : ObjectId("571661cd4af77ca97a9614c1"), "flag": { "status": true }, "text" : "Main Comment 1 reply 1" }, { "_id" : ObjectId("571661cd4af77ca97a9614c2"), "flag": { "status": true, "userId": ObjectId("606b0e9f00bece43471baa48")}, "text" : "Main Comment 1 reply 2" } ] }
预期结果是,
{ "_id" : ObjectId("5716617f4af77ca97a9614bd"), "text" : "Main Comment 1", reply: { } }
{ "_id" : ObjectId("5716617f4af77ca97a9614bd"), "text" : "Main Comment 1", reply: { "_id" : ObjectId("571661cd4af77ca97a9614c2"), "text" : "Main Comment 1 reply 1" } }
{ "_id" : ObjectId("5716617f4af77ca97a9614bd"), "text" : "Main Comment 1", reply: { "_id" : ObjectId("571661cd4af77ca97a9614c2"), "text" : "Main Comment 1 reply 2" } }
这是我正在使用的查询:
db.comments.aggregate([
{ $unwind: { path: "$reply", preserveNullAndEmptyArrays: true } },
{ $match: { $or: [ { 'flag.status': true }, { 'reply.flag.status': true }]} },
])
通过使用此代码,我没有得到预期的结果。 请告诉我该怎么做?
【问题讨论】:
标签: mongodb mongoose mongodb-query aggregation-framework