MongoDB Bytes #2: Querying Complex data with MQL

Hello, Folks! :wave:

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

Do most people send letters and applications by traditional means? If so, how many envelopes are being purchased?
  • 3309
  • 3319
  • 3329
  • 3339

0 voters

Similarly, how many people have purchased pens from the Denver store (specifically)?
  • 1000
  • 1005
  • 1010
  • 1015

0 voters

Now we want to know the trends of online shopping among certain demographics in Denver. So, how many customers aged 40 or older purchase their office supplies online?
  • 309
  • 319
  • 329
  • 339

0 voters

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> ] } } )
We want to know how many schools in Denver are purchasing their office supplies from “In Store”?
  • 828
  • 838
  • 848
  • 858

0 voters

Now, we will use the movie’s collections from the sample_mflix database, as you have earlier used while learning basic CRUD operations.

Using the $in operator, name any movies that starred “John Ott” and “Charles Kayser”.
  • A Corner in Wheat
  • Blacksmith Scene
  • The Poor Little Rich Girl
  • Now or Never

0 voters

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> } } );

Could you tell us the name of the movie that has been released in 10 different languages and has won 30+ awards?
  • Eega
  • Pina
  • Hostel
  • The Adventures of Picasso

0 voters