This tutorial will help you to get a clear understanding to use the query operations using the db.collection.find() function.
Sample Collection
I am going to use the below `books` collection for all the sample query.
db.books.insertMany([
{
"name" : "The Little Prince", "author" : { "name" : "Antoine de Saint-Exupery", "born" : 1900, "died" : 1944, "occupation" : ["Aviator", "writer"] }, "language" : ["French"], "year":1943, "sales": 200, "genre": ["fantasy"], "rating": [4.5, 5, 4.6, 4.8], "sales_year": [ {year:1950, sales:10000}, {year:1960, sales:25400}, ]},{ "name" : "The Lord of the Rings ", "author" : { "name" : "J. R. R. Tolkien", "born" : 1892, "died" : 1973, "occupation" : ["Author", "Academic", "philologist", "poet"] }, "language" : ["English"], "year":1954, "sales": 150, "genre": ["fantasy"], "rating": [4, 3, 3.2], "sales_year": [ {year:1960, sales:15000}, {year:1970, sales:75220}, ]},{ "name" : "The Alchemist (O Alquimista)", "author" : { "name" : "Paulo Coelho", "born" : 1947, "occupation" : ["lyricist", "writer"] }, "language" : ["English", "Portuguese"], "year":1988, "sales": 150, "genre": ["fantasy"], "rating": [5, 4.8, 4.5], "sales_year": [ {year:1990, sales:33600}, {year:2000, sales:55590}, ]},{ "name" : "Harry Potter and the Philosopher's Stone", "author" : { "name" : "J. K. Rowling", "born" : 1965, "occupation" : ["Author", "philanthropist", "writer"] }, "language" : ["English"], "year":1997, "sales": 120, "genre": ["fantasy", "mystery"], "rating": [4.8, 4.5], "sales_year": [ {year:2000, sales:85000}, {year:2010, sales:95400}, ]},{ "name" : "The Hobbit", "author" : { "name" : "J. R. R. Tolkien", "born" : 1892, "died" : 1973, "occupation" : ["Author", "Academic", "philologist", "poet"] }, "language" : ["English"], "year":1937, "sales": 100, "genre": ["fantasy"], "rating": [3, 3.5], "sales_year": [ {year:1950, sales:15000}, {year:1960, sales:66840}, ]}]);{ "acknowledged" : true, "insertedIds" : [ ObjectId("5ecd0fa1aa27864f8014d3ee"), ObjectId("5ecd0fa1aa27864f8014d3ef"), ObjectId("5ecd0fa1aa27864f8014d3f0"), ObjectId("5ecd0fa1aa27864f8014d3f1"), ObjectId("5ecd0fa1aa27864f8014d3f2") ]}>
Query Documents:
Using find() function, you can select all the documents from collection.
db.books.find({})
I will share some interesting details here based on mysql query. It will help you to get a great idea about How to use find() in mongo query.
Mongo Query | SQL Query |
---|---|
db.books.find({}) | SELECT * FROM books |
db.books.find({ sales: 100 }) | SELECT * FROM books WHERE sales = 100 |
db.books.find({language:{$in:["French","Portuguese"]}}) | SELECT * FROM books WHERE language IN ("French","Portuguese") |
db.books.find( {sales: 150, year:{$gt: 1980}}) | SELECT * FROM books WHERE sales = 150 AND year > 1980 |
db.books.find( { $or: [{sales: 150}, {year:{$gt: 1980}}]}) | SELECT * FROM books WHERE sales = 150 OR year > 1980 |
db.books.find( {language:'Portuguese', $or: [{sales: 150}, {year:{$gt: 1980}}]}) | SELECT * FROM books WHERE language = "Portuguese" AND ( sales < 150 OR year > 1980) |
Query on Embedded/Nested Documents
{
"name" : "The Little Prince", "author" : { "name" : "Antoine de Saint-Exupery", "born" : 1900, "died" : 1944, "occupation" : ["Aviator", "writer"] }, "language" : ["French"], "year":1943, "sales": 200, "genre": ["fantasy"], "rating": [4.5, 5, 4.6, 4.8], "sales_year": [ {year:1950, sales:10000}, {year:1960, sales:25400}, ]},
Use { <field>: <value> } condition format for below queries to match the element in the documents
db.books.find( { "author.born":1892 } ) |
db.books.find( { "author.born":{$gt:1892} } ) |
db.books.find( { "author.born":{$gt:1892}, "sales":120 } ) |
db.books.find( { "author.occupation":"Author"}); |
db.books.find( { "author.occupation":{$in:["Author","writer"]}}); |
db.books.find( { "author.born":{$gt:1892}, "sales":120, "author.occupation":{$in:["Author","writer"]} ) |
Query an Array
{
"name" : "The Alchemist (O Alquimista)", "author" : { "name" : "Paulo Coelho", "born" : 1947, "occupation" : ["lyricist", "writer"] }, "language" : ["English", "Portuguese"], "year":1988, "sales": 150, "genre": ["fantasy"], "rating": [5, 4.8, 4.5], "sales_year": [ {year:1990, sales:33600}, {year:2000, sales:55590}, ]},
Use { <field>: <value> } condition format for below queries to match the element in the array
Exact match with element count | db.books.find( { language:["English"]} ) |
* Match element * Exact match not required | db.books.find( { language:{$all:["English"]}} ) |
Search the string | db.books.find( { language:"English"} ) |
Condition with operator | db.books.find( { rating: { $gt: 4.9 } } ) |
Multiple Conditions | db.books.find( { rating: { $gt: 4.9, $lt: 5 } } ) db.books.find( { rating: { $gt: 4.9, $lt: 5 } , language:"English"} ) |
Multiple Conditions with elemMatch | db.books.find( { rating: { $elemMatch: { $gte: 4.9, $lte: 5 } } } ) |
Query By Index Position | db.books.find( { "rating.0": { $gte: 4.9 } } ) db.books.find( { "rating.1": { $gte: 4.9 } } ) |
Query By Array Length | db.books.find( { "rating": { $size: 3 } } ) |
Hint:
- Multiple Conditions: In this example, This condition can satisfy the one element greater than 4.9 condition and another element can satisfy the less than 5 condition, or a single element can satisfy both condition.
- Multiple Conditions with elemMatch: This condition can satisfy at least one element for both condition.
- Query By Index Position: The field and nested field must be inside quotation marks When you use dot(.) notation.
- Query By Array Length: using the $size operator you can query by number of elements in the array.
Query an Array of Embedded Documents
{
"name" : "The Little Prince", "author" : { "name" : "Antoine de Saint-Exupery", "born" : 1900, "died" : 1944, "occupation" : ["Aviator", "writer"] }, "language" : ["French"], "year":1943, "sales": 200, "genre": ["fantasy"], "rating": [4.5, 5, 4.6, 4.8], "sales_year": [ {year:1950, sales:10000}, {year:1960, sales:25400}, ]},
Exact match including field order | db.books.find( { "sales_year": { year: 1950, sales: 15000 } } ) |
Exact with no result | db.books.find( { "sales_year": { sales: 15000, year: 1950 } } ) |
Condition | db.books.find( { 'sales_year.sales': { $gte: 10000 } } ) |
Index for a Field | db.books.find( { 'sales_year.0.sales': { $gte: 60000 } } ) |
elemMatch | db.books.find( { "sales_year": { $elemMatch: { sales: 10000, year: 1950 } } } ) db.books.find( { "sales_year": { $elemMatch: { sales: { $gt: 60000, $lte: 70000 } } } } ) |
Compound conditions | db.books.find( { "sales_year.sales": { $gt: 20000, $lte: 50000 } } ) |
Hint:
- Exact Match: While running the match query against the embedded/nested document It requires an exact match of the specified document including the field order and field type.
- Exact with No Result: If you change the element order, you will not get any result.
- Compound Condition: This condition can satisfy the one element greater than 20000 condition and another element can satisfy the less than 50000 condition, or a single element can satisfy both condition.
0 Comments