我有这样的JSON,我想按状态false进行计数 JSON img
{
"chats": [
{
"memberId": "ff9e28ec-4f6f-42e2-9e66-43b888267fe5",
"messages": [
{
"messagesId": "54174c03-c669-44fb-872a-75ff5c52166f",
"user": {
"userId": "4dd8495a-5704-435d-bee2-6b474e25cbbf",
"name": "Event",
"profilePicture": "png"
},
"createdAt": "",
"title": "belajar",
"image": "minio.png",
"messages": "belajar",
"status": false
}
]
}
]
}
我想获取状态为false
的所有邮件的计数。
示例
{
"memberId" : "ff9e28ec-4f6f-42e2-9e66-43b888267fe5"
"statusCount" : 2
}
答案 0 :(得分:1)
db.getCollection('inbox').aggregate([
{ "$unwind": "$chats" },
{
"$match": {
"chats.messages.status": false
}
},
{ "$unwind": "$chats.messages" },
{ "$group": {
"_id": "$chats.memberId",
"count": { "$sum": 1 }
}}])
我尝试此查询,结果为
{
"_id" : ObjectId("5d207957e887785f2c0c745e"),
"count" : 4.0
}
这是您想要的吗?
答案 1 :(得分:1)
像这样解决查询
db.inbox.aggregate([
{ $unwind: "$chats" },
{ $unwind: "$chats.messages" },
{
$match: {
"chats.messages.status": false
}
},
{
$group: {
_id: { inboxId: "$inboxId", memberId: "$chats.memberId" },
count: { $sum :1 }
}
},
{
$project : {inboxId : '$_id.inboxId', memberId : '$_id.memberId', count : '$count', _id : 0}
}
])