发布于 2015-09-14 15:02:51 | 113 次阅读 | 评论: 0 | 来源: 网络整理
2.0 新版功能.
For geospatial queries, MongoDB may return a single document more than once for a single query, because geospatial indexes may include multiple coordinate pairs in a single document, and therefore return the same document more than once.
The $uniqueDocs operator inverts the default behavior of the $within operator. By default, the $within operator returns the document only once. If you specify a value of false for $uniqueDocs, MongoDB will return multiple instances of a single document.
Example
Given an addressBook collection with a document in the following form:
{ addresses: [ { name: "Home", loc: [55.5, 42.3] }, { name: "Work", loc: [32.3, 44.2] } ] }
The following query would return the same document multiple times:
db.addressBook.find( { "addresses.loc": { "$within": { "$box": [ [0,0], [100,100] ], $uniqueDocs: false } } } )
The following query would return each matching document, only once:
db.addressBook.find( { "address.loc": { "$within": { "$box": [ [0,0], [100,100] ], $uniqueDocs: true } } } )
You cannot specify $uniqueDocs with $near or haystack queries.
在 2.2.3 版更改: Before 2.2.3, a geospatial index must exist on a field holding coordinates before using any of the geolocation query operators. After 2.2.3, applications may use geolocation query operators without having a geospatial index; however, geospatial indexes will support much faster geospatial queries than the unindexed equivalents.
注解
A geospatial index must exist on a field and the field must hold coordinates before you can use any of the geolocation query operators.