MongoDB Query Documents

In this tutorial, we will see some collections and document operations.

Drop a collection in Mongo DB: The command is “db.collectionName.drop()”.

Example: Write “db.testCollection.drop()”, and then press CTRL + ENTER. It will delete the testCollection collection from the database.

Note: We are using testDB database, and collection as testCollection.


Steps to inserting bulk documents one at a time

1) Open a new command Prompt

2) Enter “mongod“, and press ENTER (it will start your mongodb server. Don’t close it)

3) Open Robo3T.

4)Right click on Connection name.

5)Open shell.

6)Write “use testDB” and press CTRL+ENTER.

7)Write “db.createCollection(“testCollection”)” and press CTRL+ENTER.

8)For inserting write “db.testCollection.insert([{“Name”:”CAR”, “value”:”A”}, {“Name”:”BIKE”, “value”:”B”}, {“Name”:”MOTOCYCLE”, “value”:”C”},])” and press CTRL+ENTER.

It will enter three documents in the testDB under testCollection.


Retrieve Documents

MongoDB Command to retrieve documents from a collection: The command is “db.collectionName.find({})”.

Example: Write “db.testCollection.find({})”, and then press CTRL + ENTER. It will show all the documents in the testCollection.

It displays :

First document as Name : Car, Value: A

Second document as Name: BIKE, Value: B

Third document as Name : MOTORCYCLE, Value: C

find query

If you want to see the documents in the table mode, click on the table mode(see the figure).

table mode

You will see the documents in table format.

table mode


AND Conditional MongoDb Commands

The Mongodb Command is “db.collectionName.find({Condition})

It will find the documents which satisfy the condition.

Example:

1) Retrieve all the documents which has Name as CAR from the testCollection.

Write “db.testCollection.find({“Name”:”CAR”})” in the shell and press CTRL+ENTER.

and 1

It will display only the documents which have Name as CAR.

2) Retrieve all the documents which have Name as CAR and Value as A from the testCollection.

Write “db.testCollection.find({“Name”:”CAR”, Value:”A”})” in the shell and press CTRL+ENTER.

and 2


OR Conditional MongoDb Commands

The Mongodb Command is “db.collectionName.find({$or:[{Condition1},{Condition2}]})

It will find the documents which satisfy the condition 1 or condion 2.

Example:

Retrieve all the documents which have Name as CAR or value as B from the testCollection.

Write “db.testCollection.find({$or:[{Name:"CAR"},{Value:"B"}]})” in the shell and press CTRL+ENTER.

or

It will show the documents which have their name as CAR or have their Value as B.


AND OR Conditional MongoDb Commands Together

The Mongodb Command is “db.collectionName.find({Condition1, $or:[{Condition2},{Condition3}]})

It will find the documents which satisfy the condition1 and condition 2 or condion 3.

Example:

Retrieve all the documents which have Name as CAR and Value as A or value as C from the testCollection.

Write “db.testCollection.find({“Name”:”CAR”, $or:[{Value:"A"},{Value:"C"}]})” in the shell and press CTRL+ENTER.

and or

It will show the documents which have their Name as CAR, Value as A or C. It makes two pairs,

{Name: Car, Value: A} and {Name: Car, Value: C} and then it finds the documents which matches both the pairs.

If you have any queries regarding querying documents in MongoDB, drop a comment below.

Leave a Comment

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