Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在数组对象MongoDB中计算值状态等于false_Arrays_Mongodb - Fatal编程技术网

Arrays 如何在数组对象MongoDB中计算值状态等于false

Arrays 如何在数组对象MongoDB中计算值状态等于false,arrays,mongodb,Arrays,Mongodb,我有这样一个JSON,我想按状态false计数 我想获取状态为false的所有邮件的计数 范例 { "memberId" : "ff9e28ec-4f6f-42e2-9e66-43b888267fe5" "statusCount" : 2 } 我尝试了这个查询,结果是 { "_id" : ObjectId("5d207957e887785f2c0c745e"), "count" : 4.0 } 这是你想要的吗 我尝试了这个查询,结果是 { "_id" : ObjectId("5d207

我有这样一个JSON,我想按状态false计数

我想获取状态为
false
的所有邮件的计数

范例

{
 "memberId" : "ff9e28ec-4f6f-42e2-9e66-43b888267fe5"
 "statusCount" : 2
}
我尝试了这个查询,结果是

{
"_id" : ObjectId("5d207957e887785f2c0c745e"),
"count" : 4.0
}
这是你想要的吗

我尝试了这个查询,结果是

{
"_id" : ObjectId("5d207957e887785f2c0c745e"),
"count" : 4.0
}
这是您想要的吗?

像这样解决查询

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}

    }

])
像这样解决查询

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}

    }

])

不清楚你问什么。这是任何数据库查询的输出吗?因此,您想计算
消息
数组中状态为
false
的所有元素吗?普通查询无法执行此操作。您需要使用聚合管道,使用
$unwind
获取单个消息,然后手动执行计数(例如,使用
$group
$sum
运算符)。不清楚您的要求。这是任何数据库查询的输出吗?因此,您想计算
消息
数组中状态为
false
的所有元素吗?普通查询无法执行此操作。您需要使用聚合管道,使用
$unwind
获取单个消息,然后手动执行计数(例如,使用
$group
$sum
操作符)。很好,非常感谢,我修改了这样的查询,很高兴听到这个消息。伟大的作品!似乎我必须锻炼一下mongodbnice,非常感谢,我修改了这样的查询,很高兴听到这个。伟大的作品!看来我得锻炼一下mongodb了