在mongodb中使用单个查询进行多个计数 [英] Multiple Counts with single query in mongodb

查看:147
本文介绍了在mongodb中使用单个查询进行多个计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Mongo Db的新手,希望能获得一些有关此查询的帮助.在过去的几天里,我一直在梳理这里的头发,以查看是否可以找到与查询有关的任何内容,但没有运气.

I am new to Mongo Db and would appreciate some help with this query. I have been sifting through posts here for the past couple of days tearing my hair to see if I could find anything related to my query but with no luck.

我有一个集合,其中的文档结构类似于以下内容:

I have a collection with documents similar in structure to below :

_id: xyz
Movieid: 123
MovieName: Titanic
ReleaseDate: 2000-01-01

_id: uvw
Movieid: 456
MovieName: Titanic II
ReleaseDate: 2018-01-01

_id: pqr
Movieid: 789
MovieName: Titanic III
ReleaseDate: 

我希望在以下3个单独的列中以总电影,有发行日期的电影和无发行日期的电影的计数形式输出:

I would like to achieve the output as counts for totalmovies, movies with release date, and movies without release date in 3 seperate columns as below:

Total   |   Released  |     UnReleased
 3      |       2     |          1

我能够编写单个查询来执行计数,但是我无法将所有内容成功整合到一个查询中.最终目标是创建一个视图,将这些计数作为输出.我曾尝试使用$ and这样的运算符,但似乎无法使查询按期望的方式工作....据我所知:

I was able to write individual queries to execute the counts, but I am unable to successfully consolidate all that into a single query. The end goal is to create one view producing these counts as output. I have tried using operators such as $and, but can't seem to get the query to work as desired....this is as far as I got :

db.getCollection("Movies").aggregate({
  "$and": [
    { "$match": { "ReleaseDate": { "$exists": true } }},
    { "$count": "Total" },
    { "$match": { "ReleaseDate": { "$exists": true, "$nin": [""] } }},
    { "$count": "Released" },
    { "$match": { "ReleaseDate": { "$exists": true, "$in": [""] } }},
    { "$count": "Unreleased" }
  ]
})

推荐答案

您可以在 $count 聚合只会始终为您提供单个匹配项的计数( $match )条件.因此,您需要将每个计数进一步分为多个部分,这就是 $facet 通过处理在同一阶段的同一组输入文档上的单个阶段中的多个聚合管道.

$count aggregation will always give you the counts for only single matching ($match) condition. So you need to further divide your each count into multiple section and that's what the $facet provides by processes multiple aggregation pipelines within a single stage on the same set of input documents.

db.collection.aggregate([
  { "$facet": {
    "Total": [
      { "$match" : { "ReleaseDate": { "$exists": true }}},
      { "$count": "Total" },
    ],
    "Released": [
      { "$match" : {"ReleaseDate": { "$exists": true, "$nin": [""] }}},
      { "$count": "Released" }
    ],
    "Unreleased": [
      { "$match" : {"ReleaseDate": { "$exists": true, "$in": [""] }}},
      { "$count": "Unreleased" }
    ]
  }},
  { "$project": {
    "Total": { "$arrayElemAt": ["$Total.Total", 0] },
    "Released": { "$arrayElemAt": ["$Released.Released", 0] },
    "Unreleased": { "$arrayElemAt": ["$Unreleased.Unreleased", 0] }
  }}
])

> 输出

[{
    "Total": 3,
    "Released": 2,
    "Unreleased": 1
}]

这篇关于在mongodb中使用单个查询进行多个计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆