WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

MongoDB - Query Document


MongoDB - Query Document how to query document from MongoDB collection.

The find() Method

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

Syntax

There are following basic syntax of find() method
>db.COLLECTION_NAME.find()
find() method will display all the documents in a non-structured way.

The pretty() Method

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

Syntax

>db.mycol.find().pretty()

Example

>db.mycol.find().pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "welookups",
   "url": "http://www.welookups.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
>

findOne() method, that returns only one document.

RDBMS Where Clause Equivalents in MongoDB

To query the document on the basis of some condition, you can use following operations.
Operation Syntax Example RDBMS Equivalent
Equality {<key>:<value>} db.mycol.find({"by":"welookups"}).pretty() where by = 'welookups'
Less Than {<key>:{$lt:<value>}} db.mycol.find({"likes":{$lt:50}}).pretty() where likes < 50
Less Than Equals {<key>:{$lte:<value>}} db.mycol.find({"likes":{$lte:50}}).pretty() where likes <= 50
Greater Than {<key>:{$gt:<value>}} db.mycol.find({"likes":{$gt:50}}).pretty() where likes > 50
Greater Than Equals {<key>:{$gte:<value>}} db.mycol.find({"likes":{$gte:50}}).pretty() where likes >= 50
Not Equals {<key>:{$ne:<value>}} db.mycol.find({"likes":{$ne:50}}).pretty() where likes != 50

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()

Example

There are Following example will show all the tutorials written by 'welookups' or whose title is 'MongoDB Overview'.
>db.mycol.find({$or:[{"by":"welookups"},{"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "welookups",
   "url": "http://www.welookups.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
>

AND in MongoDB

Syntax

In the find() method, if you pass multiple keys by separating them by ',' then MongoDB treats it as AND condition. Following is the basic syntax of AND
>db.mycol.find(
   {
      $and: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty()

Example

There are Following example will show all the tutorials written by 'welookups' and whose title is 'MongoDB Overview'.
>db.mycol.find({$and:[{"by":"welookups"},{"title": "MongoDB Overview"}]}).pretty() {
   "_id": ObjectId(7df78ad8902c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "welookups",
   "url": "http://www.welookups.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "likes": "100"
}
For the above given example, equivalent where clause will be ' where by = 'welookups' AND title = 'MongoDB Overview' '. You can pass any number of key, value pairs in find clause.



Welookups is optimized for learning.Copy right 2018 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.