MongoDB Tutorial - MongoDB Query Document








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

Syntax

The following code shows the syntax of find() method

>db.COLLECTION_NAME.find()

find() method returns all the documents.

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

>db.mycol.find().pretty()
>db.mycol.find().pretty()
{
   "_id": ObjectId(2df22ad2222c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "url": "http://www.java2s.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "comments": "100"
}
>

findOne() method returns only one document.





Where conditions

The following table shows how to do query to a document with conditions.

OperationSyntaxExampleRDBMS Equivalent
Equality {<key>:<value>} db.mycol.find({"by":"java2s"}).pretty() where by = 'java2s'
Less Than {<key>:{$lt:<value>}} db.mycol.find({"comments":{$lt:50}}).pretty() where comments < 50
Less Than Equals {<key>:{$lte:<value>}} db.mycol.find({"comments":{$lte:50}}).pretty() where comments <= 50
Greater Than {<key>:{$gt:<value>}} db.mycol.find({"comments":{$gt:50}}).pretty() where comments > 50
Greater Than Equals {<key>:{$gte:<value>}} db.mycol.find({"comments":{$gte:50}}).pretty() where comments >= 50
Not Equals {<key>:{$ne:<value>}} db.mycol.find({"comments":{$ne:50}}).pretty() where comments != 50




AND in MongoDB

To use AND logic in MongoDB, pass multiple keys by separating them by ',' in the find() method as follows.

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

The following code returns the documents whose by field is java2s and title field is

>db.mycol.find({"by":"java2s","title": "MongoDB"}).pretty()
{
   "_id": ObjectId(2df22ad2222c),
   "title": "MongoDB", 
   "description": "MongoDB is no sql database",
   "by": "java2s",
   "url": "http://www.java2s.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "comments": "100"
}
>

It equalient where clause is where by='java2s' AND title='MongoDB Overview'.

OR in MongoDB

To query documents based on the OR condition, use $or keyword, as follows:

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

The following query returns documents whose by field is 'java2s' or title field is 'MongoDB Overview'.

>db.mycol.find({$or:[{"by":"java2s"},{"title": "MongoDB Overview"}]}).pretty()
{
   "_id": ObjectId(2df22ad2222c),
   "title": "MongoDB Overview", 
   "description": "MongoDB is no sql database",
   "by": "java2s",
   "url": "http://www.java2s.com",
   "tags": ["mongodb", "database", "NoSQL"],
   "comments": "100"
}
>

Using AND and OR together

The following example returns documents whose comments are greater than 100 and whose title is either 'MongoDB Overview' or by is 'java2s'.

>db.mycol.find("comments": {$gt:10}, $or: [{"by": "java2s"}, {"title": "MongoDB Overview"}] }).pretty()
>

Equivalent sql is 'where comments>10 AND (by = 'java2s' OR title = 'MongoDB Overview')'.

MongoDB Projection

MongoDB projection is to select only necessary data rather than whole document.

MongoDB's find() method accepts second optional parameter that is list of fields to return. By default, MongoDB find() method returns all fields in a document. To select only required fields, set list of fields with value 1 or 0. 1 value shows the filed while 0 is used to hide the field.

The syntax of find() method with projection is as follows.

>db.COLLECTION_NAME.find({},{KEY:1})

The following example displays the title of the document.

>db.mycol.find({},{"title":1,_id:0})
>