简体   繁体   English

在php MongoDB查询中使用distinct

[英]Use distinct in php MongoDB query

In my cars_positions database and positions collection I have this kind of data : 在我的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"}}

I'm trying to use a distinct into my mongodb query. 我正在尝试使用一个独特的我的mongodb查询。 Here is my current query where I need to iterate all the data to create a proper array with only the last position: 这是我当前的查询,我需要迭代所有数据以创建一个只有最后位置的正确数组:

    $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);

Original post with the original query : http://pastebin.com/BEC7nUa7 原始帖子与原始查询: http//pastebin.com/BEC7nUa7

What I would like to achieve is to have a distinct on the car.ref and only get the latest location of any car in the $cars array with the date range. 我想要实现的是在car.ref上有一个独特的,并且只获得带有日期范围的$cars数组中任何汽车的最新位置。

So I could have a final array like this : 所以我可以得到这样的最终数组:

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 =>

Any idea ? 任何的想法 ? Thanks ! 谢谢 !

You will need aggregation. 你需要聚合。 Try something like below. 尝试类似下面的内容。

<?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);

    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM