douluogu8713 2017-02-04 18:45
浏览 153
已采纳

使用“aggregate”来组合匹配查询的所有子文档的列表?

I'm trying to use a PHP mongo library to "aggregate" on a data structure like this:

{
    "_id": 100,
    "name": "Joe",
    "pets":[
        {
            "name": "Kill me",
            "animal": "Frog"
        },
        {
            "name": "Petrov",
            "animal": "Cat"
        },
        {
            "name": "Joe",
            "animal": "Frog"
        }
    ]
},
{
    "_id": 101,
    "name": "Jane",
    "pets":[
        {
            "name": "James",
            "animal": "Hedgehog"
        },
        {
            "name": "Franklin",
            "animal": "Frog"
        }
}

For example, if I want to get all subdocuments where the animal is a frog. Note that I do NOT want all matching "super-documents" (i.e. the ones with _id). I want to get an ARRAY that looks like this:

    [
        {
            "name": "Kill me",
            "animal": "Frog"
        },
        {
            "name": "Joe",
            "animal": "Frog"
        },
        {
            "name": "Franklin",
            "animal": "Frog"
        }
     ]

What syntax am I supposed to use (in PHP) to accomplish this? I know it has to do with aggregate, but I couldn't find anything that matches this specific scenario.

  • 写回答

1条回答 默认 最新

  • douzhun4124 2017-02-04 21:27
    关注

    You can use below aggregation. $match to find documents where array has a value of Frog and $unwind the pets array. $match where document has Frog and final step is to group the matching documents into array.

    <?php
    
        $mongo = new MongoDB\Driver\Manager("mongodb://localhost:27017");
    
        $pipeline = 
            [
                [   
                    '$match' => 
                        [
                            'pets.animal' => 'Frog',
                        ],
                ],
                [   
                    '$unwind' =>'$pets',
                ],
                [   
                    '$match' => 
                        [
                            'pets.animal' => 'Frog',
                        ],
                ],
                [
                    '$group' => 
                        [
                            '_id' => null,
                            'animals' => ['$push' => '$pets'],
                        ],
                ],
            ];
    
        $command = new \MongoDB\Driver\Command([
            'aggregate' => 'insert_collection_name', 
            'pipeline' => $pipeline
        ]);
    
        $cursor = $mongo->executeCommand('insert_db_name', $command);
    
        foreach($cursor as $key => $document) {
                //do something
        }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探