कीवर्ड $near और $nearSphere के बीच अंतर करने के लिए sphere
है।
जैसा कि आप जानते हैं, $nearSphere
गोलाकार ज्यामिति का उपयोग करके दूरी की गणना करने के लिए कहा गया है। यह पृथ्वी map projection (distortion) से संबंधित है। जहां MongoDB 2d indexesCartesian और MongoDB 2dsphere indexes पर आधारित है Geodesic पर आधारित है।
पर्याप्त सिद्धांत, चलो कुछ उदाहरणों का उपयोग करें। मान लीजिए कि हम नीचे के रूप में दो दस्तावेजों करते हैं:
db.map.insert({ "_id": "Westfield London", "location": [ -0.22157, 51.507176 ] });
db.map.insert({ "_id": "Green Lanes Shopping Centre", "location": [ -0.098092, 51.576198 ] });
दोनों ऑपरेटरों के लिए मैनुअल निर्दिष्ट है कि हम का उपयोग कर सकते हैं:
इंडेक्स: 2dsphere, प्रश्न: GeoJSON
db.map.createIndex({"location": "2dsphere"});
db.map.find({"location":{"$nearSphere":{"$geometry":{"type":"Point", "coordinates":[ -0.127748, 51.507333 ] }}}});
db.map.find({"location":{"$near":{"$geometry":{"type":"Point", "coordinates":[ -0.127748, 51.507333 ]}}}});
इस मामले में, दोनों प्रश्नों क्योंकि सूचकांक 2dsphere
में संग्रहित है एक ही परिणाम वापस आ जाएगी।
परिणाम:
[ /* $nearSphere */
{"_id" : "Westfield London"},
{"_id" : "Green Lanes Shopping Centre"}
]
[ /* $near */
{"_id" : "Westfield London"},
{"_id" : "Green Lanes Shopping Centre"}
]
सूचकांक: 2 डी, क्वेरी: विरासत निर्देशांक
db.map.createIndex({"location": "2d"});
db.map.find({"location":{"$nearSphere":[ -0.127748, 51.507333 ]}});
db.map.find({"location":{"$near":[ -0.127748, 51.507333 ]}});
यह जहां भेद होता है $nearSphere
के लिए परिणाम सूचकांक के बावजूद स्फेरिकली गणना की जाती है, जबकि $near
फ्लैट प्रक्षेपण में गणना की जाती है।
परिणाम:
[ /* $nearSphere */
{"_id" : "Westfield London"},
{"_id" : "Green Lanes Shopping Centre"}
]
[ /* $near */
{"_id" : "Green Lanes Shopping Centre"},
{"_id" : "Westfield London"}
]
ऊपर के उदाहरण के gist: JS test script देखें। यह MongoDB v3.4.4 का उपयोग करके परीक्षण किया गया था।
भी Geospatial Indexes and Queries देखें।
मेरे पास बिल्कुल वही पूछताछ है। क्या तुम्हें कुछ मिला ? – Gp2mv3