geoNear mongodb始终返回空结果

时间:2018-01-13 22:35:41

标签: mongodb geospatial

我使用geoNear mongodb方法根据我的样本数据集查询最近的餐馆列表。

当我查询最近的餐厅结果时,即使我有可用的数据,我也会得到空的结果。

这是我的查询数据

   db.runCommand(
   {
     geoNear: "restaurantCollection",
     near: { type: "Point", coordinates: [ -84.369605, 33.822451 ] },
     spherical: true,
     query: { cusine: "Thai" },
     minDistance: 0,
     maxDistance: 10000
   }
)

以下是我的样本json餐厅,其中列出了基于地理坐标的数据。

[
    {
        "_id": "1",
        "name": "Ameer's Mediterranean Grill",
        "cuisine": "Halal, Mediterranean, Middle Eastern",
        "iconPath": "Ameers-Mediterranean.png",
        "contact": {
            "phone": "4049820666",
            "formattedPhone": "(404)982-0666",
            "twitter": "",
            "email": "",
            "faxNumber": ""
        },
        "address": {
            "street": "2168 Briarcliff Rd NE",
            "crossStreet": "2168 Briarcliff Rd NE",
            "city": "Atlanta",
            "state": "GA",
            "zipcode": "30329",
            "country": "usa",
            "formattedAddress": [
                "2168 Briarcliff Rd NE",
                "Atlanta, GA, 30329",
                "United States"
            ],  
            "lat": "33.816845",
            "lng": "-84.335677",
            "location": { 
                  "type " : "Point",
                   "coordinates" :[-84.335677, 33.816845]
            }
        },
        "rating": "",
        "hours": "11:00AM - 9:30PM"
    },
    {
        "_id":"2",
        "name": "26 Thai Kitchen & Bar",
        "cuisine": "Thai",
        "iconPath": "26ThaiKitchenBar_541_Atlanta_GA.png",
        "contact": {
            "phone": "4044005995",
            "formattedPhone": " (404) 400-5995",
            "twitter": "",
            "email": "",
            "faxNumber": ""
        },
        "address": {
            "street": "541 Main St NE",
            "crossStreet": "541 Main St NE",
            "city": "Atlanta",
            "state": "GA",
            "zipcode": "30324",
            "country": "usa",
            "formattedAddress": [
                "541 Main St NE",
                "Atlanta, GA, 30324",
                "United States"
            ],
            "lat": "33.822451",
            "lng": "-84.369605",
            "location": {
                "type": "Point",
                "coordinates":[-84.369605, 33.822451]
            }
        },
        "rating": "",
        "hours": "3:00PM - 9:15PM"
    },
    {
        "_id": "3",
        "name": "Cabo Cantina Atlanta",
        "cuisine": "Latin American, Spanish, Tacos",
        "iconPath": "CaboCantinaAtlanta.png",
        "contact": {
            "phone": "4049172620",
            "formattedPhone": "(404) 917-2620",
            "twitter": "",
            "email": "",
            "faxNumber": ""
        },
        "address": {
            "street": "264 Pharr Road Northeast",
            "crossStreet": "264 Pharr Road Northeast",
            "city": "Atlanta",
            "state": "GA",
            "zipcode": "30305",
            "country": "usa",
            "formattedAddress": [
                "264 Pharr Road Northeast",
                "Atlanta, GA, 30305",
                "United States"
            ],
            "lat": "33.837200",
            "lng": "-84.378011",
            "location": {
                "type": "Point",
                "coordinates":[-84.378011, 33.837200]
            }
        },
        "rating": "",
        "hours": "11:30AM - 9:00PM"
    }]

如果有人可以帮我解决问题,我将不胜感激。这是我在mongo shell上看到的内容。

"waitedMS" : NumberLong(0),
"results" : [ ],
"stats" : {
    "nscanned" : 0,
    "objectsLoaded" : 0,
    "maxDistance" : 0,
    "time" : 0
},
"ok" : 1

1 个答案:

答案 0 :(得分:0)

geoNear command要求某个集合最多只有一个2dsphere和/或只有一个2d index。根据您的示例,请参阅2dsphere index以创建集合的索引。

例如:

db.restaurantCollection.createIndex({"address.location":"2dsphere"})

值得一提的是Views(在MongoDB v3.4 +中可用)不支持geoNear操作。