- Reference >
- Query, Update, Projection, and Aggregation Operators >
- $uniqueDocs
$uniqueDocs¶
-
$uniqueDocs
¶ New in version 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 offalse
for$uniqueDocs
, MongoDB will return multiple instances of a single document.Example
Given an
addressBook
collection with a document in the following form:The following query would return the same document multiple times:
The following query would return each matching document, only once:
You cannot specify
$uniqueDocs
with$near
or haystack queries.Changed in version 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.
Note
A geospatial index must exist on a field and the field must hold coordinates before you can use any of the geolocation query operators.