@@ -6,23 +6,113 @@ $uniqueDocs
6
6
7
7
.. operator:: $uniqueDocs
8
8
9
- When using the :dbcommand:`geoNear`, if document contains more than
10
- one field with coordinate values, MongoDB will return the same
11
- document multiple times. When using the :operator:`$within`,
12
- however, MongoDB provides opposite behavior: if a document contains
13
- more than one field with coordinate values, MongoDB will only
14
- return the document once.
15
-
16
- The :operator:`$uniqueDocs` operator overrides these default
17
- behaviors.
18
-
19
- By specifying ``$uniqueDocs: false`` in a :operator:`$within`
20
- query, will cause the query to return a single document multiple
21
- times if there is more than one match.
22
-
23
- By contrast, if you specify ``uniqueDocs: true`` as an option to
24
- the a :dbcommand:`geoNear` command, then :dbcommand:`geoNear` only
25
- returns a single document even if there are multiple matches.
9
+ .. versionadded:: 2.0
10
+
11
+ For :term:`geospatial` queries, MongoDB may reuturn a single
12
+ document more than once for a single query, because geospatial
13
+ indexes may include multiple coordinate pairs in a single
14
+ document, and therefore return the same document more than once.
15
+
16
+ The :operator:`$uniqueDocs` operator inverts the default behavior
17
+ of these queres. By default, :dbcommand:`geoNear` will always
18
+ return multiple instances of the same document, while the
19
+ :operator:`$within` operator will *never* return the same document
20
+ more than once. Consider the following:
21
+
22
+ - For :dbcommand:`geoNear` queries, the default
23
+ :operator:`$uniqueDocs` setting is ``false``. If you specify a
24
+ value of ``true`` for :operator:`uniqueDocs`, MongoDB will
25
+ return multiple instances of a single document.
26
+
27
+ - For :operator:`$within` queries, the default
28
+ :operator:`$uniqueDocs` setting is ``true``. If you specify a
29
+ value of ``false`` for :operator:`uniqueDocs`, MongoDB will
30
+ return multiple instances of a single document.
31
+
32
+ .. example::
33
+
34
+ Given a document in the following form:
35
+
36
+ .. code-block:: javascript
37
+
38
+ { addresses: [ { name: "Home", loc:[55.5,42.3]}, {name:"Work",loc:[32.3,44.2]}]}
39
+
40
+ The following queries would return the same document multiple
41
+ times:
42
+
43
+ .. code-block:: javascript
44
+
45
+ > db.runCommand( { geoNear: "geo", near: [55,44], uniqueDocs: false })
46
+
47
+ > db.geoExample.find( {"addresses.loc":{"$within": {"$box": [ [0,0],[100,100] ], $uniqueDocs:false } }}).pretty()
48
+
49
+ The following queries would return each matching document, only
50
+ once:
51
+
52
+ .. code-block:: javascript
53
+
54
+ > db.runCommand( { geoNear: "geo", near: [55,44], uniqueDocs: true })
55
+
56
+ > db.geo.find( {"address.loc":{"$within": {"$box": [ [0,0],[100,100] ], $uniqueDocs:true } }}).pretty()
57
+
58
+ ..
59
+ The following example commands in the :program:`mongo` shell
60
+ illustrate this feature:
61
+
62
+ .. code-block:: javascript
63
+
64
+ > db.geoExample.insert( )
65
+ > db.geoExample.ensureIndex({"addresses.loc":"2d"})
66
+
67
+ /* this will return the entry once - default for $within */
68
+ > db.geoExample.find( {"addresses.loc":{"$within": {"$box": [ [0,0],[100,100] ] } }})
69
+ /* this will return the entry twice */
70
+ > db.geoExample.find( {"addresses.loc":{"$within": {"$box": [ [0,0],[100,100] ], $uniqueDocs:false } }}).pretty()
71
+
72
+
73
+ .. Above code output:
74
+
75
+ .. code-block:: javascript
76
+
77
+ > db.geoExample.find( {"addresses.loc":{"$within": {"$box": [ [0,0],[100,100] ], $uniqueDocs:false } }}).pretty()
78
+ {
79
+ "_id" : ObjectId("504900870826ac3f09e25db6"),
80
+ "addresses" : [
81
+ {
82
+ "name" : "Home",
83
+ "loc" : [
84
+ 55.5,
85
+ 42.3
86
+ ]
87
+ },
88
+ {
89
+ "name" : "Work",
90
+ "loc" : [
91
+ 32.3,
92
+ 44.2
93
+ ]
94
+ }
95
+ ]
96
+ }
97
+ {
98
+ "_id" : ObjectId("504900870826ac3f09e25db6"),
99
+ "addresses" : [
100
+ {
101
+ "name" : "Home",
102
+ "loc" : [
103
+ 55.5,
104
+ 42.3
105
+ ]
106
+ },
107
+ {
108
+ "name" : "Work",
109
+ "loc" : [
110
+ 32.3,
111
+ 44.2
112
+ ]
113
+ }
114
+ ]
115
+ }
26
116
27
117
You cannot specify :operator:`$uniqueDocs` with :operator:`$near`
28
118
or haystack queries.
0 commit comments