Grammar
The syntax format for MongoDB query data is as follows:
>db. Collection_name.find ()
The Find () method displays all documents in an unstructured manner.
If you need to read the data in an easy-to-read way, you can use the pretty () method, which has the following syntax:
>db.col.find (). Pretty ()
The pretty () method displays all documents in a formatted manner.
Instance
The following examples let us query the data in the set col:
> Db.col.find (). Pretty () { "_id": ObjectId ("56063f17ade2f21f36b03133"), "title": "MongoDB Tutorial", " Description ":" MongoDB is a Nosql database ", " by ":" W3cschool ", " url ":" http://www.w3cschool.cn ", " tags ": [
"MongoDB", "database", " NoSQL", " likes": 100}
In addition to the find () method, there is a FindOne () method that returns only one document.
MongoDB versus RDBMS Where statement comparison
If you are familiar with regular SQL data, the following table provides a better understanding of MongoDB conditional statement queries:
Operation |
format |
Example |
similar statements in an RDBMS |
Equals |
{<key>:<value> } |
db.col.find({"by":"w3cschool"}).pretty() |
where by = ‘w3cschool‘ |
Less than |
{<key>:{$lt:<value>}} |
db.col.find({"likes":{$lt:50}}).pretty() |
where likes < 50 |
Less than or equal to |
{<key>:{$lte:<value>}} |
db.col.find({"likes":{$lte:50}}).pretty() |
where likes <= 50 |
Greater than |
{<key>:{$gt:<value>}} |
db.col.find({"likes":{$gt:50}}).pretty() |
where likes > 50 |
Greater than or equal to |
{<key>:{$gte:<value>}} |
db.col.find({"likes":{$gte:50}}).pretty() |
where likes >= 50 |
Not equal to |
{<key>:{$ne:<value>}} |
db.col.find({"likes":{$ne:50}}).pretty() |
where likes != 50 |
MongoDB and condition
The MongoDB find () method can pass in multiple keys (key), each separated by commas, and the regular SQL and condition.
The syntax format is as follows:
>db.col.find ({key1:value1, key2:value2}). Pretty ()
Instance
The following example uses the by and title keys to query data for MongoDB tutorials in w3cschool
> Db.col.find ({"By": "W3cschool", "title": "MongoDB Tutorial"}). Pretty () { "_id": ObjectId ("56063f17ade2f21f36b03133 "), " title ":" MongoDB Tutorial ", " description ":" MongoDB is a Nosql database ", " by ":" W3cschool ", " url ":" http://w Ww.w3cschool.cn ", " tags ": [ " MongoDB ", " database ", " NoSQL " ], " likes ": 100}
The above example is similar to the WHERE statement:where by= ' W3cschool ' and title= ' MongoDB tutorial '
MongoDB OR Condition
The MongoDB OR conditional statement uses the keyword $orin the following syntax format:
>db.col.find ( { $or: [ {key1:value1}, {key2:value2} ] }). Pretty ()
Instance
In the following example, we demonstrate that the query key by value is W3cschool or the key title value for the MongoDB tutorial document.
>db.col.find ({$or: [{"By": "W3cschool"},{"title": "MongoDB Tutorial"}]}). Pretty () { "_id": ObjectId (" 56063f17ade2f21f36b03133 "), " title ":" MongoDB Tutorial ", " description ":" MongoDB is a Nosql database ", " by ":" W3cschool ", " url ":" http://www.w3cschool.cn ", " tags ": [ " MongoDB ", " database ", " NoSQL " ], "likes": 100}>
Combined with and OR
The following examples demonstrate the combined use of and and or, similar to the general SQL statement: ' where likes>50 and (by = ' W3cschool ' or title = ' MongoDB tutorial ') '
>db.col.find ({"likes": {$gt: $}, $or: [{"By": "W3cschool"},{"title": "MongoDB Tutorial"}]}). Pretty () { "_id": ObjectId ("56063f17ade2f21f36b03133"), "title": "MongoDB Tutorial", "description": "MongoDB is a Nosql database", "by ":" W3cschool ", " url ":" http://www.w3cschool.cn ", " tags ": [ " MongoDB ", " database ", " NoSQL " ], "likes": 100}
MongoDB Query Document