Search code examples
mongodbgeolocationdatabasenosql

Mongodb: Query with a field value, but also another unknown unique field


Lets say my documents are this format(random data):

{"_id" : 30, "lat": 36.1212111, "lon" : 120.2312112 "uuid" : 123123123}
{"_id" : 31, "lat": 36.1212111, "lon" : 120.2345112 "uuid" : 123123123}
{"_id" : 32, "lat": 36.1212111, "lon" : 120.2378112 "uuid" : 123123123}
{"_id" : 33, "lat": 36.1212111, "lon" : 120.2378112 "uuid" : 123123123}
{"_id" : 34, "lat": 36.1212111, "lon" : 120.2423112 "uuid" : 123123123}
{"_id" : 35, "lat": 36.1212111, "lon" : 120.2467112 "uuid" : 123123123}
{"_id" : 36, "lat": 36.1212111, "lon" : 127.2312112 "uuid" : 999999999}
{"_id" : 37, "lat": 36.1212111, "lon" : 127.9817112 "uuid" : 999999999}
{"_id" : 38, "lat": 36.1212111, "lon" : 128.2312112 "uuid" : 999999999}
{"_id" : 39, "lat": 36.1212111, "lon" : 128.8312112 "uuid" : 999999999}

Lets say this is a trail of a vehicle GPS data.
I need to query mongo. I have only start and end "lat" and "lon" values. For e.g., here for uuid - 123123123, start values are "lat": 36.1212111, "lon" : 120.2312112 and end values are "lat": 36.1212111, "lon" : 120.2467112. But, when I query mongo, I do not want to use the uuid. I want the results of documents, which belong to the same uuid.
In short, if I have location trail data of multiple vehicles, I need to query with start and end points for a unique vehicle without knowing its uuid.


Solution

  • What you are looking for is the aggregation framework. So to get the expected result out of your data, you need to execute an aggregation pipeline against it. Here is the solution pipeline for your data. Assuming your collection name is gps:

    db.gps.aggregate([
                        {$match: {
                                    lat: {$gte: 36}, 
                                    lon: {$gte: 120}, 
                                    lat: {$lte: 37}, 
                                    lon: {$lte: 130}
                                 }
                        },
                        {$group: {_id: '$uuid', data: {$push: 
                                                               {
                                                                   _id: '$_id', 
                                                                   lat: '$lat', 
                                                                   lon: '$lon'
                                                               }
                                                      }
                                 }
                        }, 
                        {$project: 
                                    {
                                       uuid: '$_id', 
                                       data: 1, 
                                       _id: 0
                                    }
                        }
                     ])
    

    And here is the result for this example aggregation query:

    {
        "data" : [
            {
                "_id" : 36,
                "lat" : 36.1212111,
                "lon" : 127.2312112
            },
            {
                "_id" : 37,
                "lat" : 36.1212111,
                "lon" : 127.9817112
            },
            {
                "_id" : 38,
                "lat" : 36.1212111,
                "lon" : 128.2312112
            },
            {
                "_id" : 39,
                "lat" : 36.1212111,
                "lon" : 128.8312112
            }
        ],
        "uuid" : 999999999
    }
    {
        "data" : [
            {
                "_id" : 30,
                "lat" : 36.1212111,
                "lon" : 120.2312112
            },
            {
                "_id" : 31,
                "lat" : 36.1212111,
                "lon" : 120.2345112
            },
            {
                "_id" : 32,
                "lat" : 36.1212111,
                "lon" : 120.2378112
            },
            {
                "_id" : 33,
                "lat" : 36.1212111,
                "lon" : 120.2378112
            },
            {
                "_id" : 34,
                "lat" : 36.1212111,
                "lon" : 120.2423112
            },
            {
                "_id" : 35,
                "lat" : 36.1212111,
                "lon" : 120.2467112
            }
        ],
        "uuid" : 123123123
    }