Table of Contents
Find ()() Method in Mongodb: Query Document in Mongodb is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
The find () method in MongoDB
To query data from Collection in MongoDB, you need to use the find () method in MongoDB.
Syntax
The basic syntax of find () method is as follows:
> db . COLLECTION_NAME . find ()
The find () method will display all Documents in unstructured form (show no structure).
Pretty () method in MongoDB
To display the results in a formatted way, you can use the pretty () method .
Syntax
> db . mycol . find (). pretty ()
For instance,
> db . mycol . find (). pretty () { "_id" : ObjectId ( 7df78ad8902c ), "title" : "MongoDB Overview" , "description" : "MongoDB is no sql database" , "by" : "tutorials point" , "url" : "http://www.tutorialspoint.com" , "tags" : [ "mongodb" , "database" , "NoSQL" ], "likes" : "100" } >
In addition to the find () method, in MongoDB there's a findOne () method that will return only a Document.
Query in MongoDB which is equivalent to the WHERE clause in RDBMS
To query a Document based on certain conditions, you can use the following operations:
Math MethodSample Example WHERE clause equivalentEquality {:} db.mycol.find ({"by": "tutorials point"}). Pretty () where by = 'tutorials point' Less Than {: {$ lt:}} db.mycol.find ({"likes": {$ lt: 50}}). pretty () where likes 50 Greater Than Equals {: {$ gte:}} db.mycol.find ({"likes": {$ gte: 50}}) pretty () where likes> = 50 Not Equals {: {$ ne:} } db.mycol.find ({"likes": {$ ne: 50}}) pretty. () where likes! = 50AND in MongoDB
Syntax
In the find () method, if you pass multiple keys by distinguishing them by commas (,), MongoDB sees it as an AND condition. The basic syntax of AND in MongoDB is as follows:
> db . mycol . find ({ key1 : value1 , key2 : value2 }). pretty ()
For instance,
the example below shows all tutorial series (tutorials) written by 'tutorials point' which has the title 'MongoDB Overview'
> db . mycol . find ({ "by" : "tutorials point" , "title" : "MongoDB Overview" }). pretty () { "_id" : ObjectId ( 7df78ad8902c ), "title" : "MongoDB Overview" , "description" : "MongoDB is no sql database" , "by" : "tutorials point" , "url" : "http://www.tutorialspoint.com" , "tags" : [ "mongodb" , "database" , "NoSQL" ], "likes" : "100" } >
The WHERE clause equivalent to the above example will be 'where by =' tutorials point 'AND title =' MongoDB Overview '' . You can pass any number of key-value pairs in the find clause.
OR in MongoDB
Syntax
To query a Document based on an OR condition, you need to use the keyword $ or . The basic syntax of OR in MongoDB is as follows:
> db . mycol . find ( { $or : [ { key1 : value1 }, { key2 : value2 } ] } ). pretty ()
For instance,
the example below will show all the tutorial series (tutorials) written by 'tutorials point' or have the title 'MongoDB Overview'
> db . mycol . find ({ $or :[{ "by" : "tutorials point" },{ "title" : "MongoDB Overview" }]}). pretty () { "_id" : ObjectId ( 7df78ad8902c ), "title" : "MongoDB Overview" , "description" : "MongoDB is no sql database" , "by" : "tutorials point" , "url" : "http://www.tutorialspoint.com" , "tags" : [ "mongodb" , "database" , "NoSQL" ], "likes" : "100" } >
Use AND and OR together in MongoDB
For instance,
the example below shows documents that have likes greater than 100 and whose title is either 'MongoDB Overview' or 'tutorials point'. The WHERE clause in the equivalent SQL query is 'where likes> 10 AND (by =' tutorials point 'OR title =' MongoDB Overview ')'
> db . mycol . find ({ "likes" : { $gt : 10 }, $or : [{ "by" : "tutorials point" },{ "title" : "MongoDB Overview" }]}). pretty () { "_id" : ObjectId ( 7df78ad8902c ), "title" : "MongoDB Overview" , "description" : "MongoDB is no sql database" , "by" : "tutorials point" , "url" : "http://www.tutorialspoint.com" , "tags" : [ "mongodb" , "database" , "NoSQL" ], "likes" : "100" } >
FAQ
What should you know about the find () method in MongoDB?
To query data from Collection in MongoDB, you need to use the find () method in MongoDB.
What should you know about pretty () method in MongoDB?
To display the results in a formatted way, you can use the pretty () method.
What should you know about query in MongoDB which is equivalent to the WHERE clause in RDBMS?
To query a Document based on certain conditions, you can use the following operations:
Reader Comments 0
Sign in with email or Google to join the discussion.