Difference between revisions of "MongoDB QueryData"

From mi-linux
Jump to navigationJump to search
Line 6: Line 6:
  
 
The format is:
 
The format is:
 +
 +
<pre style="color: blue">
 +
db.collectionName.find(optional_find_criteria)
 +
</pre>
 +
 +
Where the find_criteria follows a pattern:
 +
 
<pre style="color: blue">
 
<pre style="color: blue">
 
  db.collectionName.find({"fieldName": "value"})
 
  db.collectionName.find({"fieldName": "value"})
Line 11: Line 18:
  
 
The fieldName must be in quotes, the value needs quotes if it is a string or date value.
 
The fieldName must be in quotes, the value needs quotes if it is a string or date value.
 +
 +
=== Find all documents ===
  
 
For example, show all the data so far in the deptCollection:
 
For example, show all the data so far in the deptCollection:
Line 18: Line 27:
  
 
  db.deptCollection.find().pretty()
 
  db.deptCollection.find().pretty()
 +
 +
=== Find One document ===
  
 
To find just one document - department 10:
 
To find just one document - department 10:
Line 34: Line 45:
  
 
$elemMatch limits the contents of the employees array to contain only the first element matching the $elemMatch condition.
 
$elemMatch limits the contents of the employees array to contain only the first element matching the $elemMatch condition.
 
_id is a unique value automatically generated by MongoDB (like a Primary Key). To see it:
 
 
db.deptCollection.find({"deptno":20}, 
 
  { employees: {$elemMatch: {empno: 7902}}}).pretty()
 
  
 
This is akin to a SQL query:
 
This is akin to a SQL query:
Line 46: Line 52:
 
</pre>
 
</pre>
  
However, could you just query using the empno?
+
_id is a unique value automatically generated by MongoDB (like a Primary Key, except it is unique for the whole database).
 +
 
 +
To see it:
 +
 
 +
db.deptCollection.find({"deptno":20}, 
 +
  { employees: {$elemMatch: {empno: 7902}}}).pretty()
 +
 
 +
More about _ids in the next section.
  
 
== Next Step ==
 
== Next Step ==
  
 
[[MongoDB_Update|Updating]] the collection
 
[[MongoDB_Update|Updating]] the collection

Revision as of 12:40, 21 October 2016

Main Page >> MongoDB >>MongoDB Workbook >> Querying Collections

Querying a collection

The find() function can be used to query the documents.

The format is:

 db.collectionName.find(optional_find_criteria)

Where the find_criteria follows a pattern:

 db.collectionName.find({"fieldName": "value"})

The fieldName must be in quotes, the value needs quotes if it is a string or date value.

Find all documents

For example, show all the data so far in the deptCollection:

db.deptCollection.find()

The data comes back messy. The pretty() function can be used to improve the layout::

db.deptCollection.find().pretty()

Find One document

To find just one document - department 10:

db.deptCollection.find({"deptno":10}).pretty()

Finding an employee means using the array name too:

db.deptCollection.find({"employees.empno":7902}).pretty()

However, this does mean you get back all the employees in the department they were found in!

Since version 2.2 MongoDB's new $elemMatch can avoid this:

db.deptCollection.find({"deptno":20},  
  { _id: 0, employees: {$elemMatch: {empno: 7902}}}).pretty()

$elemMatch limits the contents of the employees array to contain only the first element matching the $elemMatch condition.

This is akin to a SQL query:

 SELECT * FROM Emp WHERE deptno=20 AND empno = 7902

_id is a unique value automatically generated by MongoDB (like a Primary Key, except it is unique for the whole database).

To see it:

db.deptCollection.find({"deptno":20},  
  { employees: {$elemMatch: {empno: 7902}}}).pretty()

More about _ids in the next section.

Next Step

Updating the collection