MongoDB – Query Documents using Mongo Shell
MongoDB provides you read 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 the 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 an optional parameter. It specifies the selection filter with the help of query operators. And if you want to get all the documents present in the collection, then omit these parameters or pass an empty document in the method. The type of this parameter is a 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: w3wiki 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 displaying 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 are creating a compound query in which we specify the condition for more than one field using logical AND. Here the logical AND connects the clauses of a compound query so that the query selects only those documents in the collection that satisfy all the conditions. Or in other words, in this example, we are retrieving only those contributors that are from the CSE branch and working with the Java language.
Select documents that satisfy the given condition (Using OR condition):
In this example, we are creating a compound query in which we specify the condition using $or operator. Here the $or operator connects the clauses of a compound query so that the query selects only those documents in the collection that satisfy at least one condition. Or in other words, in this example, we are retrieving only those contributors that are from the CSE branch or working 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.
Contact Us