Navigation
This version of the documentation is archived and no longer supported.

$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 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.

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.

←   $type $where  →