Header Ads Widget

Querying

Querying:

find() Method

To query data from MongoDB collection, you need to use MongoDB's find() method.

Syntax

The basic syntax of find() method is as follows −

>db.COLLECTION_NAME.find()

find() method will display all the documents in a non-structured way.

pretty() Method

To display the results in a formatted way, you can use pretty() method.

Syntax

>db.COLLECTION_NAME.find().pretty()

findOne() method

Apart from the find() method, there is findOne() method, that returns only one document.

Syntax

>db.COLLECTIONNAME.findOne()

AND in MongoDB

Syntax

To query documents based on the AND condition, you need to use $and keyword. Following is the basic syntax of AND −

>db.mycol.find({ $and: [ {<key1>:<value1>}, { <key2>:<value2>} ] })

OR in MongoDB

Syntax

To query documents based on the OR condition, you need to use $or keyword. Following is the basic syntax of OR −

>db.mycol.find(
  {
      $or: [
        {key1: value1}, {key2:value2}
      ]
  }
).pretty()

 

NOR in MongoDB

Syntax

To query documents based on the NOR condition, you need to use the $not keyword. Following is the basic syntax of NOR −

>db.COLLECTION_NAME.find(
    {
        $nor: [
            {key1: value1}, {key2:value2}
        ]
    }
)

 

NOT in MongoDB

Syntax

To query documents based on the NOT condition, you need to use the $not keyword following is the basic syntax of NOT −

>db.COLLECTION_NAME.find(
    {
        $NOT: [
            {key1: value1}, {key2:value2}
        ]
    }
).pretty()

Post a Comment

0 Comments