The find() Method
To query data from MongoDB collection, you need to use MongoDB's find() method.
> db.democol.find()
Result:
{ "_id" : ObjectId("52963c2a6f63f810a98b7a98"), "title" : "Learn mongo", "by" :
"mani", "likes" : 100 }
{ "_id" : ObjectId("52963ce96f63f810a98b7a99"), "title" : "Learn SQL", "by" : "s
ubramanian", "likes" : 20, "comments" : [ { "user" : "Tiara", "message" : "Wo
rth to read" } ] }
The pretty() Method
To display the results in a formatted way, you can use pretty() method.
> db.democol.find().pretty()
Result:
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
{
"_id" : ObjectId("52963ce96f63f810a98b7a99"),
"title" : "Learn SQL",
"by" : "subramanian",
"likes" : 20,
"comments" : [
{
"user" : "Tiara",
"message" : "Worth to read"
}
]
}
AND in MongoDB
In the find() method if you pass multiple keys by separating them by ',' then MongoDB treats it AND condition. Basic syntax of AND is shown below:
> db.democol.find({by:"mani"}).pretty()
Result:
> db.democol.find({by:"mani"}).pretty()
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
> db.democol.find({by:"mani",title:"Learn mongo"}).pretty()
Result:
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
OR in MongoDB
To query documents based on the OR condition, you need to use $or keyword. Basic syntax of OR is shown below:
> db.democol.find({$or:[{by:"mani"},{title:"Learn SQL"}]}).pretty()
Result:
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
{
"_id" : ObjectId("52963ce96f63f810a98b7a99"),
"title" : "Learn SQL",
"by" : "subramanian",
"likes" : 20,
"comments" : [
{
"user" : "Tiara",
"message" : "Worth to read"
}
]
}
To query data from MongoDB collection, you need to use MongoDB's find() method.
> db.democol.find()
Result:
{ "_id" : ObjectId("52963c2a6f63f810a98b7a98"), "title" : "Learn mongo", "by" :
"mani", "likes" : 100 }
{ "_id" : ObjectId("52963ce96f63f810a98b7a99"), "title" : "Learn SQL", "by" : "s
ubramanian", "likes" : 20, "comments" : [ { "user" : "Tiara", "message" : "Wo
rth to read" } ] }
The pretty() Method
To display the results in a formatted way, you can use pretty() method.
> db.democol.find().pretty()
Result:
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
{
"_id" : ObjectId("52963ce96f63f810a98b7a99"),
"title" : "Learn SQL",
"by" : "subramanian",
"likes" : 20,
"comments" : [
{
"user" : "Tiara",
"message" : "Worth to read"
}
]
}
AND in MongoDB
In the find() method if you pass multiple keys by separating them by ',' then MongoDB treats it AND condition. Basic syntax of AND is shown below:
> db.democol.find({by:"mani"}).pretty()
Result:
> db.democol.find({by:"mani"}).pretty()
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
> db.democol.find({by:"mani",title:"Learn mongo"}).pretty()
Result:
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
OR in MongoDB
To query documents based on the OR condition, you need to use $or keyword. Basic syntax of OR is shown below:
> db.democol.find({$or:[{by:"mani"},{title:"Learn SQL"}]}).pretty()
Result:
{
"_id" : ObjectId("52963c2a6f63f810a98b7a98"),
"title" : "Learn mongo",
"by" : "mani",
"likes" : 100
}
{
"_id" : ObjectId("52963ce96f63f810a98b7a99"),
"title" : "Learn SQL",
"by" : "subramanian",
"likes" : 20,
"comments" : [
{
"user" : "Tiara",
"message" : "Worth to read"
}
]
}