MongoDB Query Document

MongoDB Query Document

 MongoDB Query Document


Use db.collection.find() method to query collection for available documents. You can use the show collections command to view the available collections in your database.

Syntax

> db.COLLECTION_NAME.find(condition)  

Search All Documents

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

> db.users.find()

You can also use pretty() function with the 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" : "rahul@tecadmin.net",
        "designation" : "Founder and CEO",
        "location" : "India"
}

Search Specific Documents

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

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

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

Reactions

Post a Comment

0 Comments

close