mongodb - 如何使 mongodb 使用 countDocuments() 和 $addFields

标签 mongodb mongodb-query aggregation-framework

我有一个具有以下结构的文档:

{
    '_id': '',
    'a': '',
    'b': '',
    'c': [
        {
            '_id': '',
            'd': '',
            'f': [
                {
                    'orderDate': 12345,
                    'orderProfit': 12,
                },
                {
                    'orderDate': 67891,
                    'orderProfit': 12341,
                },
                {
                    'orderDate': 23456,
                    'orderProfit': 474,
                },
            ],
        },
        {
            '_id': '',
            'd': '',
            'f': [
                {
                    'orderDate': 14232,
                    'orderProfit': 12222,
                },
                {
                    'orderDate': 643532,
                    'orderProfit': 4343,
                },
                {
                    'orderDate': 33423,
                    'orderProfit': 5555,
                },
            ],
        },
    ],
}

orderDate 是一个 int64,表示下订单的日期

orderProfit 是一个 int64,表示订单的利润

我需要返回具有最大“orderDate”的文档并检查“orderProfit”是否是我正在寻找的文档。 为此,我使用了这样的查询(在聚合查询中):

[
    {
        '$addFields': {
            'orders': {
                '$map': {
                    'input': '$c',
                    'as': 'c',
                    'in': {
                        'profit': {
                            '$filter': {
                                'input': '$$c.f',
                                'cond': {
                                    '$eq': [
                                        {
                                            '$max': '$$c.f.orderDate',
                                        },
                                        '$$this.orderDate',
                                    ],
                                },
                            },
                        },
                    },
                },
            },
        },
    },
    {
        '$match': {
            '$or': [
                { 'orders.profit.orderProfit': 500 },
            ],
        },
    },
];

它工作正常。

当尝试将此查询添加到 countDocuments() 查询以获取文档总数时,问题就出现了。

需要使用 countDocuments()

我似乎无法让它工作......

$addFields 作为未知顶级运算符抛出。如果我删除 $addFields 那么我就无法将查找最大日期的查询添加到 countDocuments() 中。如果我完全删除它 $match 是一个未知的运算符。

db.getCollection('orders').countDocuments(
    {
"orders.profit.orderProfit" : {"Query that was shown previously"}

})

最佳答案

你不能。

countDocuments收到查找查询,您不能将整个管道附加到它。

但是 countDocuments 只是一个包装器。

Returns the count of documents that match the query for a collection or view. The method wraps the $group aggregation stage with a $sum expression to perform the count and is available for use in Transactions.

基本上这个方法只是执行以下聚合:

db.collection.aggregate([
    {
        $match: yourQuery
    },
    {
        $group: {
            _id: null,
            sum: {$sum: 1}
        }
    }
])

然后根据结果返回results[0].sum0

因此您可以只使用自己的管道并在末尾添加这个阶段,这实际上是相同的复杂性。

db.collection.aggregate([
     ...
     your entire pipeline
     ...
    {
        $group: {
            _id: null,
            sum: {$sum: 1}
        }
    }
])

如果您有任何其他具体原因不想使用聚合框架,请告诉我,也许有解决方法。

关于mongodb - 如何使 mongodb 使用 countDocuments() 和 $addFields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67252012/

相关文章:

java - 在 Java 中使用 get 方法时 BSONObjects 返回什么?

javascript - Mongoose - 如何将结果保存到数组中

java - 如何使用 Java 运行 .group()

带有可插拔 MongoDB 存储问题的 Django

node.js - mongoose:如何临时存储光标?

MongoDB $project : $filter sub-array

MongoDB - 在多个字段中查找所有搜索字符串单词

mongodb - 将查找对象 id 连接到数组中

javascript - 计算键值的出现次数

mongodb - 如何有条件地连接mongodb聚合中的字段