mongodb中单个查询的多个计数 [英] Multiple Counts with single query in mongodb

查看:29
本文介绍了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" }
  ]
})

推荐答案

你可以试试下面的$facet 聚合

You can try below $facet aggregation

$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天全站免登陆