简体   繁体   English

如何计算数组mongo db查询中假值的数量

[英]How to count number of false values in an array mongo db query

_id:5e4d18bd10e5482eb623c6e4
notification_obj:
 0  notification_text:"Welcome to the app and your account is created hello."
    open:false
    type:"just_click"

 1  notification_text:"Sebal started following you."
    open:true
    type:"open_profile"

 2  notification_text:"Hella started following you."
    open:false
    type:"open_profile"

So here I have an array 'notification_obj' array in a document of mongo database, I want to search the record with _id and in that record I want to count 'How many 'open:false' values are there.所以在这里我在 mongo 数据库的文档中有一个数组 'notification_obj' 数组,我想用 _id 搜索记录,在该记录中我想计算 'open:false' 值有多少。 I want to count in this array that open:false how many times.我想在这个数组中计算 open:false 多少次。 Please help with a "query" in mongo db.请帮助在 mongo db 中进行“查询”。

I think this code help you.我认为这段代码可以帮助你。

db.getCollection('your_collection').aggregate([
  {
    $match: { _id: ObjectId("5a544.............") }
  },
  {
    $unwind: '$notification_obj'
  },
  {
    $match: { 'notification_obj.open': false }
  },
  {
    $count: 'total'
  }
]);

Output:输出:

{
    "total" : 1
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM