Cookies help us deliver our services. By using our services, you agree to our use of cookies. More information

Difference between revisions of "Find"

From NoSQLZoo
Jump to: navigation, search
m
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<pre class=setup>
 
#ENCODING
 
import io
 
import sys
 
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-16')
 
#MONGO
 
from pymongo import MongoClient
 
client = MongoClient()
 
client.progzoo.authenticate('scott','tiger')
 
db = client['progzoo']
 
#PRETTY
 
import pprint
 
pp = pprint.PrettyPrinter(indent=4)
 
#CODE
 
from bson.code import Code
 
</pre>
 
 
{{TopTenTips}}
 
{{TopTenTips}}
<div style="height:25em">
+
<syntaxhighlight lang="JavaScript" inline>find()</syntaxhighlight> is used to filter and project.<br/>
<code>find()</code> is used to query documents. It takes a <code>query</code> parameter and an optional <code>projecetion</code> parameter, and returns a [http://docs.mongodb.org/manual/reference/glossary/#term-cursor cursor].<br/>
+
By default <syntaxhighlight lang="JavaScript" inline>find()</syntaxhighlight> returns a cursor containing all the documents in the collection it is used on. This is the same as <syntaxhighlight lang="JavaScript" inline>find({})</syntaxhighlight>.<br /><br/>
By default <code>find()</code> returns a cursor containing all the documents in the collection it is used on. This is the same as <code>find({})</code><br /><br/>
+
To get all the documents with a certain value, <syntaxhighlight lang="JavaScript" inline>find(<field>:{"$eq": <value>}})</syntaxhighlight> can be used, this can be simplified to <syntaxhighlight lang="JavaScript" inline>find(<field>: <value>})</syntaxhighlight>.<br/>
To get all the documents with a certain value, <code>find(<field>:{"$eq":<value>}})</code> can be used, this can be simplified to <code>find(<field>:<value>})</code><br/>
+
'''$eq''' can be replaced with other conditional operators such as '''$lt''', '''$gt''' to perform less than or greater than comparisons.<br/><br/>
<code>$eq</code> can be replaced with other conditional operators such as <code>$lt</code>, <code>$gt</code> to perform less than or greater than comparisons.<br/><br/>
+
To query nested documents, [http://docs.mongodb.org/manual/core/document/#document-dot-notation dot notation] can be used, e.g. <syntaxhighlight lang="JavaScript" inline>parent.child.field</syntaxhighlight>.<br/><br/>
To query nested documents, [http://docs.mongodb.org/manual/core/document/#document-dot-notation dot notation] can be used, e.g. <code>parent.child.field</code><br/><br/>
+
The second parameter selects what fields to include.<br/>
 +
If the second parameter is not present the default action is to include all the fields in the result documents.<br/><br/>
 +
To hide a field, <syntaxhighlight lang="JavaScript" inline><field>: 0</syntaxhighlight> is used. The other fields won't be removed.<br/>
 +
Using <syntaxhighlight lang="JavaScript" inline><field>: 1</syntaxhighlight> will include that field and hide all the others except the '''_id''' and other fields set to '''1'''.<br/>
 +
Passing an empty (<syntaxhighlight lang="JavaScript" inline>{}</syntaxhighlight>) second parameter hides all fields except the '''_id''' field, to hide '''_id''' it has to be explicitly set to '''0'''.<br/><br/>
 +
<syntaxhighlight lang="JavaScript" inline>.findOne()</syntaxhighlight> limits the result to one document and also applies the <syntaxhighlight lang="JavaScript" inline>.pretty()</syntaxhighlight> operation.
  
The second parameter selects what fields to include. If the second parameter is not present the default action is to include all the fields in the result documents.<br/>
+
<div class="q nonum" data-lang="mongo">
To hide a field, <code><field>:0</code> is used. The other fields won't be removed.<br/>
+
<p class="strong">Show the name and capital for every country of "South America"</p>
Using <code><field>:1</code> will include that field and hide all the others except the <code>_id</code> and other fields set to 1.<br/>
+
<pre class="def"><nowiki>
Passing an empty (<code>{}</code>) second parameter hides all fields except the <code>_id</code> field, to hide <code>_id</code> it has to be explicitly set to 0.
+
db.world.find(
</div>
+
  {continent: "South America"},
 
+
  {"_id": 0, name: 1, capital: 1}
<div class=q data-lang="py3">
+
);</nowiki></pre>
<p class="strong>Show the capital city and population of France</p>
 
<pre class=def>
 
print(db.world.find({"name":"France"},{"_id":0,"population":1}))
 
</pre>
 
 
</div>
 
</div>

Latest revision as of 13:14, 17 October 2018

find() is used to filter and project.
By default find() returns a cursor containing all the documents in the collection it is used on. This is the same as find({}).

To get all the documents with a certain value, find(<field>:{"$eq": <value>}}) can be used, this can be simplified to find(<field>: <value>}).
$eq can be replaced with other conditional operators such as $lt, $gt to perform less than or greater than comparisons.

To query nested documents, dot notation can be used, e.g. parent.child.field.

The second parameter selects what fields to include.
If the second parameter is not present the default action is to include all the fields in the result documents.

To hide a field, <field>: 0 is used. The other fields won't be removed.
Using <field>: 1 will include that field and hide all the others except the _id and other fields set to 1.
Passing an empty ({}) second parameter hides all fields except the _id field, to hide _id it has to be explicitly set to 0.

.findOne() limits the result to one document and also applies the .pretty() operation.

Show the name and capital for every country of "South America"

db.world.find(
  {continent: "South America"},
  {"_id": 0, name: 1, capital: 1}
);