Tags: overview structure syntax ... pretty ODB strong embedded--
Find () methodTo query data from a MongoDB collection, you need to use MongoDB's find()
method.
Grammar
find()
The basic syntax of the command is as follows:
Shell>db.COLLECTION_NAME.find(document)
find()
Method will display all documents in an unstructured manner.
To display the results in a formatted manner, you can use the pretty()
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 addition find()
to the method, there is a findOne()
method that returns only one document.
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 MongoDBGrammar
In a find()
method, MongoDB treats it as a condition if it is passed by using ' ,
separate ' multiple keys AND
. The following is AND
the basic syntax-
>db.mycol.find( { $and: [ {key1: value1}, {key2:value2} ] }).pretty()
Example
The following example shows all the yiibai tutorials
tutorials 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 SQL where
clause is-
SqlSELECT * FROM mycol where by =‘yiibai tutorials‘ AND title =‘MongoDB Overview‘
You can find
pass any number of key values in the clause.
Grammar
You need to use keywords when you want to query a document based on a OR
condition $or
. The following is the OR
basic syntax for a condition-
>db.mycol.find( { $or: [ {key1: value1}, {key2:value2} ] }).pretty()
Example
The following example displays yiibai tutorials
all tutorials written or titled "MongoDB Overview".
using and and or conditions together>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"}>
Example
The following example displays likes
10
all documents that are greater than and are titled " MongoDB Overview
or" yiibai tutorials
. The equivalent SQL WHERE clause is-
SqlSELECT * FROM mycol where likes> 10 AND(by =‘yiibai tutorials‘ OR title =‘MongoDB Overview‘)
querying embedded/nested documents>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"}>
Here's how to use db.collection.find()
the: method for an example of a query operation on an embedded/nested document. The samples on this page use the inventory
collection. 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 query size
selects { 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 query inventory
does 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 selects size
all documents that are nested in fields equal to " uom
in
":
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 the size
h
less-than operator () in fields that are embedded in fields $lt
:
db.inventory.find( { "size.h": { $lt: 15 } } )
Specifying and Conditions
The following query selects h
All documents that have a nested field that is less than 15
, and the nested field uom
equals " in
" and the status
field equals " D
":
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )
MongoDB (6): Querying documents