The MongoDB query document uses the Find () method. The Find () method displays all documents in an unstructured manner.
Grammar
Db.collection.find (query, projection)
query: Optional, use the query operator to specify the query criteria;
projection: Optional, use the projection operator to specify the returned key. Returns all the key values in the document when queried, just omit the parameter.
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 ()
Pretty () method to display all documents in a formatted manner.
Example
> Db.col.find (). Pretty ()
{
"_id": ObjectId ("56063f17ade2f21f36b03133"),
"title": "MongoDB Tutorial",
"description": "MongoDB is a Nosql database",
"by": "Rookie Tutorial",
"url": "Http://www.runoob.com",
"tags": [
"Mong ODB ",
" database ",
" NoSQL "
],
" likes ":
In addition to the find () method, there is a FindOne () method that returns only one document
MongoDB versus RDBMS Where statement comparison
MongoDB and Condition
The MongoDB find () method can pass in multiple keys (key), each separated by commas, which is the and condition of the regular SQL.
The syntax format is as follows:
>db.col.find ({key1:value1, key2:value2}). Pretty ()
Example
> Db.col.find ({"By": "Rookie Tutorial", "title": "MongoDB Tutorial"}). Pretty ()
{
"_id": ObjectId ("56063f17ade2f21f36b03133 "),
" title ":" MongoDB Tutorial ",
" description ":" MongoDB is a Nosql database ",
" by ":" Rookie Tutorial ",
" url ":" Http://www.ru Noob.com ",
" tags ": [
" MongoDB ",
" database ",
" NoSQL "
],
" likes ":
MongoDB OR Condition
>db.col.find (
{
$or: [
{key1:value1}, {key2:value2}
]
}
). Pretty ()
Example
>db.col.find ({$or: [{"By": "Rookie Tutorial"},{"title": "MongoDB Tutorial"}]}). Pretty ()
{
"_id": ObjectId (" 56063f17ade2f21f36b03133 "),
" title ":" MongoDB Tutorial ",
" description ":" MongoDB is a Nosql database ",
" by ":" Rookie Tutorial "," c5/> "url": "Http://www.runoob.com",
"tags": [
"MongoDB",
"database",
"NoSQL"
],
"likes ":
>
combined with and or
>db.col.find ({"likes": {$gt: $}, $or: [{"By": "Rookie Tutorial"},{"title": "MongoDB Tutorial"}]}). Pretty ()
{
"_id": ObjectId ("56063f17ade2f21f36b03133"),
"title": "MongoDB Tutorial",
"description": "MongoDB is a Nosql database",
"By": "Rookie Tutorial",
"url": "Http://www.runoob.com",
"tags": [
"MongoDB",
"database",
"NoSQL"
],
"likes":