1. Home
  2. MongoDB
  3. MongoDB Tutorial
  4. MongoDB – Query Document

MongoDB – Query Document

MongoDB Query Document

Use db.collection.find() method to query collection for available documents.

Syntax

> db.COLLECTION_NAME.find(condition)  

Search All Documents

Execute find() function on the collection without any condition to get all the availabel documents in collection.

> db.users.find()

You can also use pretty() function with above command to show formatted output.

> db.users.find().pretty();

Output

{
        "_id" : ObjectId("59a01b102427abe3470644db"),
        "id" : 1001,
        "user_name" : "rahul",
        "name" : [
                {
                        "first_name" : "Rahul"
                },
                {
                        "middle_name" : ""
                },
                {
                        "last_name" : "Kumar"
                }
        ],
        "email" : "[email protected]",
        "designation" : "Founder and CEO",
        "location" : "India"
}

Search Specific Documents

You can define conditions to select documents matches the specified condition.

> db.users.find({"id": 1001})

The above command will show all the fields from the document having {"id": 1001}.

Tags , ,
Was this article helpful to you? Yes 5 No
Advertisement