@@ -8,111 +8,38 @@ $uniqueDocs
8
8
9
9
.. versionadded:: 2.0
10
10
11
- For :term:`geospatial` queries, MongoDB may reuturn a single
11
+ For :term:`geospatial` queries, MongoDB may return a single
12
12
document more than once for a single query, because geospatial
13
13
indexes may include multiple coordinate pairs in a single
14
14
document, and therefore return the same document more than once.
15
15
16
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.
17
+ of the :operator:`$within` operator. By default, the
18
+ :operator:`$within` operator returns the document only once. If you
19
+ specify a value of ``false`` for :operator:`uniqueDocs`, MongoDB
20
+ will return multiple instances of a single document.
31
21
32
22
.. example::
33
23
34
- Given a document in the following form:
24
+ Given an ``addressBook`` collection with a document in the following form:
35
25
36
26
.. code-block:: javascript
37
27
38
28
{ addresses: [ { name: "Home", loc:[55.5,42.3]}, {name:"Work",loc:[32.3,44.2]}]}
39
29
40
- The following queries would return the same document multiple
30
+ The following query would return the same document multiple
41
31
times:
42
32
43
33
.. code-block:: javascript
44
34
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()
35
+ db.addressBook.find( {"addresses.loc":{"$within": {"$box": [ [0,0],[100,100] ], $uniqueDocs:false } }}).pretty()
48
36
49
- The following queries would return each matching document, only
37
+ The following query would return each matching document, only
50
38
once:
51
39
52
40
.. code-block:: javascript
53
41
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
- }
42
+ db.addressBook.find( {"address.loc":{"$within": {"$box": [ [0,0],[100,100] ], $uniqueDocs:true } }}).pretty()
116
43
117
44
You cannot specify :operator:`$uniqueDocs` with :operator:`$near`
118
45
or haystack queries.
0 commit comments