Pradipta Das
Pradipta Das

Reputation: 97

Query Mongodb collection with geospatial index and field

I have a mongodb collection which has a location field with coordinates. While doing a filter search I wanted to get the documents within a range and a specific name, storeName in my case. Any help in this regard ? Below is my collection.

{
    "uuid" : "1cf92c0a-5fd6-11e5-b541-9fed3d5db33a",
    "type" : "store",
    "created" : 1442780891840,
    "modified" : 1442780891840,
    "address" : {
      "address" : "1285 Lincoln Ave",
      "city" : "San Jose",
      "state" : "CA",
      "zip" : "95125",
      "country" : "US"
    },
    "hours" : "Mon-Sat 7:00 am- 10:00 pm, Sun 8:00 am- 9:00 pm",
    "location":{"coordinates":[-121.8990201,37.3049792],"type":"Point"},
    "nid" : "18",
    "phoneNumber" : "(408) 280-5124",
    "storeName" : "CVS Pharmacy - Photo",
    "storeTag" : "pharmacy",
    "website" : "http://www.cvs.com/store-locator/cvs-pharmacy-address/1285+Lincoln+Avenue-San%20Jose-CA-95125/storeid=9559?WT.mc_id=LS_GOOGLE_9559"
  }
  {
    "uuid" : "1bd7414a-5fd6-11e5-bfb7-6d8e86e17b6b",
    "type" : "store",
    "created" : 1442780889940,
    "modified" : 1442780889940,
    "address" : {
      "address" : "1130 BIRD AVE",
      "city" : "San Jose",
      "state" : "CA",
      "zip" : "95125",
      "country" : "US"
    },
    "hours" : "Mon-Sun 7:00 am- 12:00 am",
    "location":{"coordinates":[-121.8943393,37.3108209],"type":"Point"},
    "nid" : "14",
    "phoneNumber" : "(408)-295-7768",
    "storeName" : "Walgreens",
    "storeTag" : "pharmacy",
    "website" : "http://www.walgreens.com/locator/walgreens-1130+bird+ave-san+jose-ca-95125/id=2265"
  }
  {
    "uuid" : "1c22c93a-5fd6-11e5-b9e3-35f3c41288ef",
    "type" : "store",
    "created" : 1442780890435,
    "modified" : 1442780890435,
    "address" : {
      "address" : "2181 Monterey Hwy",
      "city" : "San Jose",
      "state" : "CA",
      "zip" : "95125",
      "country" : "US"
    },
    "hours" : "Mon-Sat 6:00 am- 10:00 pm, Sun 7:00 am- 8:00 pm",
    "location":{"coordinates":[-121.8683031,37.3029936],"type":"Point"},
    "nid" : "15",
    "phoneNumber" : "(408) 971-4890",
    "storeName" : "The Home Depot",
    "storeTag" : "hardware",
    "website" : "http://www.homedepot.com/l/San-Jose-ge/CA/San-Jose/95125/1861"
  }

Here is the query so far.

db.storelist.find({ location: { $nearSphere: { $geometry: { type: "Point", coordinates: [ -121.8683031,37.3029936 ] }, $maxDistance: 2000 } } })

I get the list of documents within the distance, but could not sort out where I put the other field i.e, storeName.

Any help in this regard will be highly appreciated. Thanks in advance.

Upvotes: 1

Views: 56

Answers (1)

Alex
Alex

Reputation: 21766

You can add additional queries inside the {...} like this:

db.storelist.find({
  "storeName": "The Home Depot",
  location: {
    $nearSphere: {
      $geometry: {
        type: "Point",
        coordinates: [-121.8683031, 37.3029936]
      },
      $maxDistance: 2000
    }
  }
})

Upvotes: 1

Related Questions