Hello, Folks!
In this article, we will learn how to query complex data using MQL and a few operators.
So, here we will be using the sales collection from the sample_supplies
database. You can use the same Atlas Cluster that you set up for university courses, if you haven’t loaded the sample data yet, fear not! You can do so by following the steps: Load Sample Data in Atlas.
The sample_supplies
database contains data from a mock office supply company. The company tracks customer information and sales data and has several store locations throughout the world. These documents contain fields like salesDate, list of items, storeLocation, customer details, and other details related to office sales. You can read more about the collection and see a sample document from the documentation.
Please feel free to comment if you face any issues or doubts when trying the queries. I’ll post detailed answers to the questions next week!
Querying Embedded/Nested Documents:
Embedded data exists in a single document and can be accessed using dot notation
"<embedded document>.<field>"
is the syntax for accessing a field within an embedded document
- 3309
- 3319
- 3329
- 3339
- 1000
- 1005
- 1010
- 1015
- 309
- 319
- 329
- 339
Querying an Array - use of $in, $size
Array in a single document and can be accessed using dot notation
"<array>.<index>"
is the syntax for array element access- Additionally, the
$in
operator selects the documents whose values equal any value in an array- Syntax:
db.collections.find( { field: { $in: [<value1>, <value2>, ... <valueN> ] } } )
- 828
- 838
- 848
- 858
Now, we will use the movie’s collections from the sample_mflix
database, as you have earlier used while learning basic CRUD operations.
- A Corner in Wheat
- Blacksmith Scene
- The Poor Little Rich Girl
- Now or Never
Let’s take a look at the $size
operator, which matches arrays with the specified number of elements.
Syntax:
db.collection.find( { field_name: { $size: <number> } } );
- Eega
- Pina
- Hostel
- The Adventures of Picasso