在php MongoDB查询中使用distinct

时间:2017-02-02 11:31:57

标签: php mongodb

在我的cars_positions数据库和positions集合中,我有这样的数据:

db.positions.find()

{ "_id" : ObjectId("536a5cd9e9df25cd8609d286"), "car" : { "type" : "sport", "ref" : "nameOfTheCar1" }, "position" : { "dateTime" : "2014-05-06T06:42:10", "latitude" : "0000.1111", "hemesphere" : "S", "longitude" : "05563.1254"}}
{ "_id" : ObjectId("536a5cd9e9df25cd8609d287"), "car" : { "type" : "sport", "ref" : "nameOfTheCar2" }, "position" : { "dateTime" : "2014-05-06T06:43:11", "latitude" : "0000.1111", "hemesphere" : "S", "longitude" : "05563.1254"}}

我正在尝试使用一个独特的mongodb查询。这是我当前的查询,我需要迭代所有数据以创建只有最后位置的正确数组:

    $pipeline = 
        [
            [
                '$match' => 
                    [
                      'car.ref' => ['$in' => $cars],
                      'position.dateTime'  => 
                            [
                                '$gte' => $period['start'],
                                '$lte' => $period['end'],
                            ],
                    ],
            ],
            [
                '$sort' => 
                    [
                      'position.dateTime' => 1,
                    ],
            ],
            [
                '$group' => 
                    [
                      '_id' => ['car.ref'],
                      'lastposition' => ['$last' => '$position'],
                    ],
            ],
        ];

        $command = new \MongoDB\Driver\Command($pipeline);
        $cursor = $mongo->executeCommand('mybase.positions', $command);

包含原始查询的原始帖子:http://pastebin.com/BEC7nUa7

我想要实现的是在car.ref上有一个独特的,并且只获取$cars数组中具有日期范围的任何汽车的最新位置。

所以我可以拥有这样的最终数组:

array (size=1)
  car1 => 
    array (size=8)
      'car.ref' => string 'car1' (length=7)
      'dateTime' => string '2017-01-18T00:00:09' (length=19)
      'latitude' => string '0000.1111' (length=9)
      'hemesphere' => string 'S' (length=1)
      'longitude' => string '05563.1254' (length=10)
   car2 =>
   car3 =>

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:2)

您需要汇总。尝试类似下面的内容。

<?php

    $mongo = new MongoDB\Driver\Manager("mongodb://localhost:27017");

    $cars = ['nameOfTheCar1', 'nameOfTheCar2'];

    $start = new \MongoDB\BSON\UTCDateTime(strtotime('2012-05-06T06:42:10') * 1000);
    $end = new \MongoDB\BSON\UTCDateTime(strtotime('2014-05-06T06:42:10') * 1000);   

    $pipeline = 
        [
            [
                '$match' => 
                    [
                      'car.ref' => ['$in' => $cars],
                      'position.dateTime'  => 
                            [
                                '$gte' => $start,
                                '$lte' => $end,
                            ],
                    ],
            ],
            [
                '$sort' => 
                    [
                      'position.dateTime' => 1,
                    ],
            ],
            [
                '$group' => 
                    [
                      '_id' => '$car.ref',
                      'lastposition' => ['$last' => '$position'],
                    ],
            ],
        ];

    $command = new \MongoDB\Driver\Command([
        'aggregate' => 'positions', 
        'pipeline' => $pipeline
        ]);

    $cursor = $mongo->executeCommand('mybase', $command);

    foreach($cursor as $key => $document) {
        var_dump($document);

    }
?>