-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcursor.returnKey.txt
149 lines (113 loc) · 3.21 KB
/
cursor.returnKey.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
==================
cursor.returnKey()
==================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: cursor.returnKey()
.. include:: /includes/fact-mongosh-shell-method.rst
.. tip::
:expression:`$meta` supports the keyword ``"indexKey"`` to return index
key metadata if an index is used. The use of
:expression:`{ $meta: "indexKey" } <$meta>` is preferred over
:method:`cursor.returnKey()`.
Modifies the cursor to return index keys rather than the documents.
The :method:`cursor.returnKey()` has the following form:
.. code-block:: javascript
cursor.returnKey()
:returns:
The :term:`cursor` that :method:`~cursor.returnKey()` is attached to
with a modified result set. This allows for additional cursor modifiers
to be chained.
Compatibility
-------------
This method is available in deployments hosted in the following environments:
.. include:: /includes/fact-environments-atlas-only.rst
.. include:: /includes/fact-environments-atlas-support-all.rst
.. include:: /includes/fact-environments-onprem-only.rst
Behavior
--------
If the query does not use an index to perform the read operation, the
cursor returns empty documents.
Example
-------
The ``restaurants`` collection contains documents with the following schema:
.. code-block:: javascript
{
"_id" : ObjectId("564f3a35b385149fc7e3fab9"),
"address" : {
"building" : "2780",
"coord" : [
-73.98241999999999,
40.579505
],
"street" : "Stillwell Avenue",
"zipcode" : "11224"
},
"borough" : "Brooklyn",
"cuisine" : "American ",
"grades" : [
{
"date" : ISODate("2014-06-10T00:00:00Z"),
"grade" : "A",
"score" : 5
},
{
"date" : ISODate("2013-06-05T00:00:00Z"),
"grade" : "A",
"score" : 7
}
],
"name" : "Riviera Caterer",
"restaurant_id" : "40356018"
}
The collection has two indexes in addition to the default ``_id`` index:
.. code-block:: javascript
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "guidebook.restaurant"
},
{
"v" : 1,
"key" : {
"cuisine" : 1
},
"name" : "cuisine_1",
"ns" : "guidebook.restaurant"
},
{
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "name_text",
"ns" : "guidebook.restaurant",
"weights" : {
"name" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}
The following code uses the :method:`cursor.returnKey()` method to return
only the indexed fields used for executing the query:
.. code-block:: javascript
var csr = db.restaurant.find( { "cuisine" : "Japanese" } )
csr.returnKey()
This returns the following:
.. code-block:: javascript
{ "cuisine" : "Japanese" }
{ "cuisine" : "Japanese" }
{ "cuisine" : "Japanese" }
{ "cuisine" : "Japanese" }
...