Mongodb query based on item at specific position i

2019-01-12 12:16发布

问题:

I want to run a query where an item at a specific position in an array needs to be compared.

For example, consider the GeoJSON format for storing location data.

//sample document from collection user
{
    name: "Some name",
    location : {
        type: "Point",
        coordinates : [<Longitude>, <Latitude>]
    }
}

How would I query users located at a specific longitude?

I cant seem to find anything in the documentation which can help me do the same.

Queries I have tried:

db.users.find({"location.coordinates[0]" : -73.04303})

回答1:

Change your query to the following

   db.users.find({"location.coordinates.0" : -73.04303})