在mongo聚合结果上应用$ match-filter

时间:2017-02-23 13:36:45

标签: php mongodb aggregation-framework

我创建了一个小的mongo聚合管道,我从PHP编写并运行。

聚合方法如下所示:

$results = $mongoCollection->aggregate([
    ['$group' => ['_id' => ['FoobarID' => '$FoobarID'], 'count' => ['$sum' => 1]]],
    ['$match' => ['count' => ['$gte' => 10]]],
    ['$sort' => ['count' => -1]]
]);

结果的(前几个)记录如下:

[0] => Array
    (
        [_id] => Array
            (
                [FoobarID] => 0
            )
        [count] => 836
    )
[1] => Array
    (
        [_id] => Array
            (
                [FoobarID] => MongoInt64 Object
                    (
                        [value] => 967298418588464438
                    )
            )
        [count] => 105
    )
[2] => Array
    (
        [_id] => Array
            (
                [FoobarID] => MongoInt64 Object
                    (
                        [value] => 3397585370383372261
                    )
            )
        [count] => 101
    )

...

问题1

现在我正在尝试删除第一个聚合结果,即FoobarID = 0的那个。我无法使其工作。

如您所见,“count”上的$ match有效,但我无法在结果中过滤FoobarID字段。

我尝试了以下内容:

['$match' => ['_id' => ['$ne' => 0]]], // also tried '0' and null

['$match' => ['FoobarID' => ['$ne' => 0]]], // also tried '0' and null

['$match' => ['_id' => ['FoobarID' => ['$ne' => 0]]]], // also tried '0' and null

我做错了什么?我错过了什么吗?

问题2

如何访问MongoInt64对象中的'value'键?这有可能吗?例如。使用FoobarID MongoInt64删除聚合结果值= 967298418588464438?

非常感谢任何评论和帮助!非常感谢你提前!

1 个答案:

答案 0 :(得分:0)

问题1

试试这个。

['$match' => ['_id.FoobarID' => ['$ne' => 0]]]

问题2

['$match' => ['_id.FoobarID' => ['$eq' => 967298418588464438]]]