MongoDB – Query Document

MongoDB provides you with reading operations to retrieve documents from the collection or query a collection for a document. You can perform read operations using the db.collection.find() method. This method selects or views the documents from the collection and returns the cursor to the selected document. 

find() is a mongo shell method, which can be used in multi-document transactions. The documents displayed by this method are in non-structured form. If you want to get data in a structured form, then use pretty() method with find() method. 
 

db.collection.find().pretty()

This method iterates the cursor automatically to display the first 20 documents of the collection. If you want this method will display more than 20 documents, then type it to continue the iteration. 
 

Syntax: 
 

db.collection.find(filter, projection)

Parameters: 
 

  • filter: It is a completely optional parameter. It specifies the selection filter using query operators. If you want to get all of the documents in the collection, leave these parameters out or pass an empty document into the method. This parameter has the type Document.
  • projection: It is an optional parameter. It specifies that only those fields return to the document that matches the given query filter. And if you want to get all the fields in the document, then omit this parameter.

Return: This method returns a cursor to the documents that match the specified query criteria. When you use find() method, it returns documents which means the method is actually returning the cursor to the documents.

Examples: 

In the following examples, we are working with: 
 

Database: GeeksforGeeks
Collection: contributor
Document: five documents that contain the details of the contributors in the form of field-value pairs.

Selecting all the documents:

In this example, we are selecting all the documents of the contributor collection and displaying on the screen using db.collection.find() method. 

Syntax: 
 

db.contributor.find()

Select all documents in organized form:

In this example, we are selecting all the documents of the contributor collection and display them in the organized form using db.collection.find() method with pretty() method. 

Syntax: 
 

db.contributor.find().pretty()

Select documents that satisfy the given condition:

In this example, we are selecting only those documents that satisfy the given condition, i.e, language: “C#”. Or in other words, we are selecting only those contributors who are working with C# language. 

Syntax: 
 

db.collection.find({field: value})

Select documents that satisfy the given condition (Using Query operators):

In this example, we are selecting only those documents that satisfy the given condition, here the condition is created using query operators. Or in words, we are selecting only those contributors who are working with C# or Java language. 

Syntax: 
 

db.collection.find({field: {operator: value}})

Here, we are using $in operator. This operator is used to matches any of the values specified in the given array. 
 

db.contributor.find({language: {$in:[ "Java", "C#"]}}).pretty()

Select documents that satisfy the given condition (Using AND condition):

In this example, we’re making a compound query in which we use logical AND to specify the condition for more than one field. The logical AND connects the clauses of a compound query in this case, so that the query selects only those documents in the collection that meet all of the conditions. In this example, we are retrieving only those contributors from the CSE branch who are working with the Java language.

Select documents that satisfy the given condition (Using OR condition):

In this example, we’re building a compound query and specifying the condition with the $or operator. The $or operator connects the clauses of a compound query in this case, so that the query selects only those documents in the collection that meet at least one condition. In other words, in this example, we only retrieve contributors from the CSE branch or those who work with the Java language.

Select documents that satisfy the given condition (Using both AND and OR conditions):

In this example, we are setting filter using both AND and OR operators. or in other words, in this example, we are retrieving only those contributors that are from the ECE branch and either age is 23 or working with the Java language. 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *