【问题标题】:Get count of documents matching different conditions获取符合不同条件的文档数
【发布时间】:2022-11-24 03:32:42
【问题描述】:

我有 collection: bookSchema 作为:

[
    {
      _id: ObjectId("637d05dc32428ed75ea08d09"),
      book_details: {
          book_subscription: null,
          book_auth: "Amber"
      },
      book_name: "random123"
    },
    {
      _id: ObjectId("637d0673ce0f17f6c473dee2"),
      book_details: {
        book_subscription: ObjectId("637d06a545a17f0e686ecf3a"),
        book_auth: "Shurya"
      },
      book_name: "random321"
    },
    {
      _id: ObjectId("637d069a3d597c8458ebe4ec"),
      book_details: {
        book_subscription: ObjectId("637d06ac82f0760b03fdea0d"),
        book_auth: "Aman"
      },
      book_name: "random676"
    },
    {
      _id: ObjectId("637d06c05b32d503007bcb54"),
      book_details: {
        book_subscription: null,
        book_auth: "Saurav"
      },
      book_name: "random999"
    },
  ]

所需的 O/P 显示为:

{
  score_ambr: 3,
  score_saurabh: 1
}

为此,我试过:

db.bookSchema.aggregate([
  {
    "$group": {
      "_id": {
        "$eq": [
          "$book_details.book_auth",
          "Amber"
        ]
      },
      "score_ambr": {
        "$sum": 1
      }
    },

  },
  {
    "$group": {
      "_id": {
        "$eq": [
          "$book_details.book_auth",
          "Saurav"
        ]
      },
      "score_saurabh": {
        "$sum": 1
      }
    },

  }
])

我尝试使用 $group 来因为我想将所有匹配的文档组合成一个并使用 $count 来给出匹配文档的计数但它似乎不起作用并将 O/P 指定为

订单:

[
  {
    "_id": false,
    "score_sau": 2
  }
]

MongoDB游乐场:https://mongoplayground.net/p/cZ64KwAmwlv

【问题讨论】:

  • 3 和 1 是什么意思,您是否总是使用 score_sau 和 score_abr 对查询进行硬编码

标签: javascript mongodb mongoose mongodb-query


【解决方案1】:

看起来你想要的是:

db.bookSchema.aggregate([
  {
    $group: {
      _id: "$book_details.book_auth",
      count: {
        $sum: 1
      }
    }
  },
  {
    "$group": {
      "_id": 0,
      data: {
        $push: {
          k: {
            "$concat": [
              "score_",
              {
                "$toLower": "$_id"
              }
            ]
          },
          v: {
            "$sum": "$count"
          }
        }
      }
    }
  },
  {
    "$replaceRoot": {
      "newRoot": {
        "$arrayToObject": "$data"
      }
    }
  }
])

查看它在playground example 上的工作原理

【讨论】:

    【解决方案2】:

    我不知道你的例子中31是什么意思,但如果我理解正确,你可以试试这个查询:

    这里的技巧是使用$facet 在聚合中创建“两种方式”。一个选项将按Amber 过滤,另一个按Saurav 过滤。

    然后,随着值被过滤,您只需要知道生成的数组的大小。

    db.collection.aggregate([
      {
        "$facet": {
          "score_ambr": [
            {
              "$match": {
                "book_details.book_auth": "Amber"
              }
            }
          ],
          "score_saurabh": [
            {
              "$match": {
                "book_details.book_auth": "Saurav"
              }
            }
          ]
        }
      },
      {
        "$project": {
          "score_ambr": {
            "$size": "$score_ambr"
          },
          "score_saurabh": {
            "$size": "$score_saurabh"
          }
        }
      }
    ])
    

    例子here

    请注意,通过这种方式您可以避免使用$group

    【讨论】:

      猜你喜欢
      • 2017-04-24
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      相关资源
      最近更新 更多