Find () method
To query data from a MongoDB collection, you need to use MongoDB'sfind()method.
Grammar
find()The basic syntax of the command is as follows:
>db.COLLECTION_NAME.find(document)
Shell
find()Method will display all documents in an unstructured manner.
Pretty () method
To display the results in a formatted manner, you can use thepretty()method.
Grammar
> db.mycol.find().pretty()
Example
>db.mycol.find().pretty()
{
"_id": 100,
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "yiibai tutorials",
"url": "http://www.yiibai.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
In additionfind()to the method, there is afindOne()method that returns only one document.
the equivalent Where clause for MongoDB and RDBMS
To query a document based on some criteria, you can use the following actions.
Operation |
Grammar |
Example |
RDBMS equivalent Statement |
Equal |
{<key>:<value>} |
db.mycol.find({"by":"yiibai"}).pretty() |
where by = ‘yiibai‘ |
Less than |
{<key>:{$lt:<value>}} |
db.mycol.find({"likes":{$lt:50}}).pretty() |
where likes < 50 |
Less than or equal |
{<key>:{$lte:<value>}} |
db.mycol.find({"likes":{$lte:50}}).pretty() |
where likes <= 50 |
Greater than |
{<key>:{$gt:<value>}} |
db.mycol.find({"likes":{$gt:50}}).pretty() |
where likes > 50 |
Greater than or equal |
{<key>:{$gte:<value>}} |
db.mycol.find({"likes":{$gte:50}}).pretty() |
where likes >= 50 |
Not equal to |
{<key>:{$ne:<value>}} |
db.mycol.find({"likes":{$ne:50}}).pretty() |
where likes != 50 |
Here we will show you all the actions in the table above-
and operator in MongoDB
Grammar
In afind()method, MongoDB treats it as a condition if it is passed by using ',separate ' multiple keysAND. The following isANDthe basic syntax-
>db.mycol.find(
{
$and: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
The following example shows all theyiibai tutorialstutorials written by "" and titled "MongoDB Overview."
> db.mycol.find({$and:[{"by":"yiibai tutorials"},{"title": "MongoDB Overview"}]}).pretty()
{
"_id" : 100,
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "yiibai tutorials",
"url" : "http://www.yiibai.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>
For the example given above, the equivalent SQLwhereclause is-
SELECT * FROM mycol where by =‘yiibai tutorials‘ AND title =‘MongoDB Overview‘
Sql
You canfindpass any number of key values in the clause.
the OR operator in MongoDB
Grammar
You need to use keywords when you want to query a document based on aORcondition$or. The following is theORbasic syntax for a condition-
>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
The following example displaysyiibai tutorialsall tutorials written or titled "MongoDB Overview".
>db.mycol.find({$or:[{"by":"yiibai tutorials"},{"title": "MongoDB Overview"}]}).pretty()
{
"_id": 100,
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "yiibai tutorials",
"url": "http://www.yiibai.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
using and and or conditions together
Example
The following example displayslikes10all documents that are greater than and are titled "MongoDB Overviewor"yiibai tutorials. The equivalent SQL WHERE clause is-
SELECT * FROM mycol where likes> 10 AND(by =‘yiibai tutorials‘ OR title =‘MongoDB Overview‘)
Sql
>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "yiibai tutorials"},
{"title": "MongoDB Overview"}]}).pretty()
{
"_id": 100,
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "yiibai tutorials",
"url": "http://www.yiibai.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
querying embedded/nested documents
Here's how to usedb.collection.find()the: method for an example of a query operation on an embedded/nested document. The samples on this page use theinventorycollection. To populate the inventory (inventory) collection to prepare some data, run the following command:
db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
Matching embedded/nested documents
To specify equality criteria on a field that is an embedded/nested document, use the query filter document{<field>:<value>}, which<value>is the document that you want to match.
For example, the following querysizeselects{ h: 14, w: 21, uom: "cm" }All documents that have a field equal to:
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
An equality match in an entire embedded document requires an exact match to the specified<value>document, including the order of the fields.
For example, the following queryinventorydoes not match any of the documents in the Inventory () collection:
db.inventory.find( { size: { w: 21, h: 14, uom: "cm" } } )
Querying nested fields
To specify a query condition on a field in an embedded/nested document, use the dot symbol ("field.nestedField").
Specify equals match on nested fields
The following example selectssizeall documents that are nested in fields equal to "uomin":
db.inventory.find( { "size.uom": "in" } )
Use the query operator to specify a match
Query filter documents can be specified by using the query operator, such as the following form of conditions:
{ <field1>: { <operator1>: <value1> }, ... }
The following query uses thesizehless-than operator () in fields that are embedded in fields$lt:
db.inventory.find( { "size.h": { $lt: 15 } } )
Specifying and Conditions
The following query selectshAll documents that have a nested field that is less than15, and the nested fielduomequals "in" and thestatusfield equals "D":
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )
MongoDB (6): Querying documents