Skip to content
Studio 3T - The professional GUI, IDE and client for MongoDB
  • Tools
    • Aggregation Editor
    • IntelliShell
    • Visual Query Builder
    • Export Wizard
    • Import Wizard
    • Query Code
    • SQL Query
    • Connect
    • Schema Explorer
    • Compare
    • SQL ⇔ MongoDB Migration
    • Data Masking
    • Task Scheduler
    • Reschema
    • More Tools and Features
  • Solutions
  • Resources
    • Knowledge Base
    • MongoDB Tutorials & Courses
    • Tool/Feature Documentation
    • Blog
    • Community
    • Testimonials
    • Whitepapers
    • Reports
  • Contact us
    • Contact
    • Sales Support
    • Feedback and Support
    • Careers
    • About Us
  • Store
    • Buy Now
    • Preferred Resellers
    • Team Pricing
  • Download
  • My 3T
search

Academy 3T

  • Explore our courses
    • MongoDB 101: Getting Started
    • MongoDB 201: Querying MongoDB Data
    • MongoDB 301: Aggregation
  • Get certified

Lesson 5, Exercise 1: Using the $elemMatch operator to query embedded documents

MongoDB 201: Querying MongoDB Data Querying Embedded Documents in MongoDB Arrays Lesson 5, Exercise 1: Using the $elemMatch operator to query embedded documents

In this exercise, you’ll use IntelliShell to add several documents to the customers collection. Each document includes an Array field that’s made up of several embedded documents. After you add the documents, you’ll use IntelliShell to run multiple queries based on values in the embedded documents.

You’ll be working with the customers collection from the sales database we created in a previous section. Refer to that section for details on how to set them up if you haven’t done so already.

To run the queries in IntelliShell

1. Launch Studio 3T and connect to MongoDB Atlas.

Don’t have a MongoDB Atlas instance? Here’s how to set up a free cluster, or connect to an existing MongoDB database or localhost instead.

2. In the Connection Tree, right-click the sales database node and click Open IntelliShell. Studio 3T opens IntelliShell in its own tab in the main window.

3. On the IntelliShell toolbar, you’ll see that both the Auto-Completion button (the lightning bolt icon) and Query Assist button are already enabled by default. Make sure that both buttons are enabled. These will make it easier to view the results returned by the mongo shell commands.

4. At the command prompt in the IntelliShell editor, replace any existing code with the following insertMany statement:

db.customers.insertMany( [
  {
    _id: 1,
    first : "Maria",  
    travel : [
      { country: "Canada", visits: 3, rating: 7 },
      { country: "Poland", visits: 1, rating: 8 },
      { country: "Thailand", visits: 2, rating: 9 } ]
  },
  {
    _id: 2,
    first : "Chen",  
    travel : [
      { country: "Thailand", visits: 3, rating: 7 },
      { country: "Canada", visits: 2, rating: 9 },
      { country: "Costa Rica", visits: 4, rating: 8 } ]
  },
  {
    _id: 3,
    first : "Gladys",  
    travel : [
      { country: "Canada", visits: 1, rating: 8 },
      { country: "Thailand", visits: 2, rating: 9 },
      { country: "Australia", visits: 3, rating: 10 } ]
  } ] );

The statement adds three simple documents to the customers collection. All three documents include the travel field, an array made up of embedded documents.

Each embedded document lists a country that the customer has visited, along with the number of times the customer visited and how the customer rated the country, based on a scale of 1 through 10.

5. You now want to find those customers who have visited Australia three times. At the command prompt in the IntelliShell editor, replace the existing code with the following find statement:

db.customers.find({ 
  travel: { country: "Australia", visits: 3 } } );

The statement searches the travel array to find documents with a country value that equals Australia and a visits value that equals 3. 

6. Execute this statement by clicking the Run Entire Script button. Studio 3T displays the results in the bottom window.

In this case, the query does not return any results even though the Gladys document (the third document you added above) appears to match the search criteria.

The statement doesn’t return any documents because the search engine is looking for an exact match. The query specifies the country and visits elements, but not the rating element, so an exact match is not possible.

7. To fix this, replace the existing code with the following find statement:

db.customers.find({ travel: 
  { country: "Australia", visits: 3, rating: 10 } } );

The new statement is just like the preceding one, except that it now includes the rating element as a search condition.

8. On the IntelliShell toolbar, click the Run entire script button.

Studio 3T runs the find statement and returns the results in the bottom window, displaying them in the Find Query tab. The query now returns the Gladys document.

Table View is selected by default. Switch to JSON View to view the document in full.

Although this approach works fine when you know the exact combination of fields and values, this is often not the case.

For example, the collection might include other customers who have visited Australia three times, but who have provided a different “rating”, in which case they would not be included in the results.

Another limitation with this approach is that you must specify the document elements in the exact order they’re saved to the database, or they will not be considered an exact match.

To test this out, return to the command prompt in the IntelliShell editor and replace the existing code with the following find statement:

db.customers.find({ travel: 
  { visits: 3, rating: 10, country: "Australia" } } );

The statement works just like the preceding one, except that the elements are in a different order.

9. Click the Run entire script button. As expected, the statement returns no documents because the element order is not an exact match.

10. Another approach you might take to find customers who have visited Australia is to recast the find statement so that each search condition uses dot notation to specify the array and its elements.

At the command prompt in the IntelliShell editor, replace the existing code with the following find statement:

db.customers.find({
  "travel.country": "Australia", "travel.visits": 2 } );

Each search condition includes the array field name (travel), followed by the specific element (country and visits, respectively). When you use the dot notation, you must enclose the fully qualified name in quotes. 

For this statement, the visits value is 2, rather than 3. This was done to help further explain issues that can arise when querying embedded documents.

11. Run the entire script. This time the statement returns the Gladys document, even though she visited Australia three times, not two.

In this case, the query engine returns any documents that include a country value of Australia and a visits value of 2, even if they’re in different embedded documents in the array.

To test this out further, you can change the field values used in the search conditions. At the command prompt in the IntelliShell editor, replace the existing code with the following find statement:

db.customers.find({
  "travel.country": "Canada", "travel.visits": 3 } );

The statement uses the same logic as the preceding one, only this time the country value is Canada and the visits value is 3.

12. Click the Run entire script button. Now the statement returns all three documents, although Maria is the only customer to have visited Canada three times.

The problem this time is that all three documents include Canada as a country value, and all three include 3 as a visits value, even though they might not be in the same embedded document. 

You have to be very careful constructing your queries when working with embedded documents. If this were an extremely large dataset, such a statement could return a massive number of documents, with few matching the intended criteria.

To get the results you want, you need to use the $elemMatch operator.

13. At the command prompt in the IntelliShell editor, replace the existing code with the following find statement:

db.customers.find(
  { travel: { $elemMatch: 
    { country: "Canada", visits: 3 } } } );

Because the statement uses the $elemMatch operator, the query engine will return only those documents in which at least one embedded document in the travel array matches the specified search criteria.

14. Run the entire script. The statement returns only the Maria document because it is the only one whose travel array includes an embedded document with a country value of Canada and a visits value of 3.

15. At the command prompt in the IntelliShell editor, replace the existing code with the following find statement:

db.customers.find(
  { travel: { $elemMatch: 
    { country: "Thailand", visits: 2, rating: 9 } } } );

Like the previous statement, this one also uses the $elemMatch operator, only this time, it’s looking for customers who have visited Thailand twice and who have assigned a rating of 9 to the country.

16. On the IntelliShell toolbar, click the Run entire script button. The statement returns the Maria and Gladys documents because they were the only ones to meet the search criteria.

Although the Chen document includes all these values, they are not within the same embedded document, so the document is not returned.

Note that you can achieve the same results without using the $elemMatch operator:

db.customers.find({ travel: 
  { country: "Thailand", visits: 2, rating: 9  } } );

This approach works only if you specify the elements in the exact order as they’re stored within the database and you match the specified elements exactly. Otherwise, the query engine will not return those documents.

The $elemMatch operator ensures that you can retrieve the documents you want regardless of the number of fields or their order.

17. Leave IntelliShell open for the next exercise.

Previous Lesson
Back to Lesson
Next Topic
  • Course Home Expand All
    Performing MongoDB CRUD Operations
    4 Topics | 1 Quiz
    Lesson 1, Exercise 1: Adding a document to a collection
    Lesson 1, Exercise 2: Viewing a document in a collection
    Lesson 1, Exercise 3: Updating a document in a collection
    Lesson 1, Exercise 4: Deleting a document from a collection
    Test your skills: Performing CRUD Operations
    Building MongoDB find() Queries
    4 Topics | 1 Quiz
    Lesson 2: The MongoDB find method
    Lesson 2, Exercise 1: Using IntelliShell to build and run find statements
    Lesson 2, Exercise 2: Using Visual Query Builder to build and run find statements
    Lesson 2, Exercise 3: Using Query Code and IntelliShell to modify and run a find statement
    Test your skills: Building MongoDB find() Queries
    Working with the MongoDB Aggregation Pipeline
    6 Topics | 1 Quiz
    Lesson 3: Introducing the MongoDB aggregate method
    Lesson 3, Exercise 1: Filtering the documents in the aggregation pipeline
    Lesson 3, Exercise 2: Grouping the documents in the aggregation pipeline
    Lesson 3, Exercise 3: Adding and removing fields in the aggregation pipeline
    Lesson 3, Exercise 4: Changing the field order in the aggregation pipeline
    Lesson 3, Exercise 5: Sorting the documents in the aggregation pipeline
    Test your skills: Working with the MongoDB Aggregation Pipeline
    Querying Arrays Using MongoDB $elemMatch
    4 Topics | 1 Quiz
    Lesson 4, Exercise 1: Using IntelliShell to query single and multiple values in an array
    Lesson 4, Exercise 2: Using Visual Query Builder to query a single array value
    Lesson 4, Exercise 3: Using Visual Query Builder to query multiple array values
    Test your skills: Querying Arrays Using MongoDB $elemMatch
    MongoDB 201 Mid-Course Feedback
    Querying Embedded Documents in MongoDB Arrays
    3 Topics | 1 Quiz
    Lesson 5, Exercise 1: Using the $elemMatch operator to query embedded documents
    Lesson 5, Exercise 2: Using conditional operators to query embedded documents
    Lesson 5, Exercise 3: Using Visual Query Builder to query embedded documents
    Test your skills: Querying Embedded Documents in Arrays
    Querying MongoDB with SQL SELECT Statements
    2 Topics | 1 Quiz
    Lesson 6, Exercise 1: Using the SQL Query tool to run SQL statements
    Lesson 6, Exercise 2: Using the SQL Query tool to aggregate collection data
    Test your skills: Querying MongoDB with SQL
    Working with MongoDB Views
    3 Topics | 1 Quiz
    Lesson 7, Exercise 1: Creating a MongoDB view
    Lesson 7, Exercise 2: Querying a MongoDB view
    Lesson 7, Exercise 3: Modifying and deleting a MongoDB view
    Test your skills: Working with MongoDB Views
    Course Extras
    Return to MongoDB 201: Querying MongoDB Data
  • Studio 3T

    MongoDB Enterprise Certified Technology PartnerSince 2014, 3T has been helping thousands of MongoDB developers and administrators with their everyday jobs by providing the finest MongoDB tools on the market. We guarantee the best compatibility with current and legacy releases of MongoDB, continue to deliver new features with every new software release, and provide high quality support.

    Find us on FacebookFind us on TwitterFind us on YouTubeFind us on LinkedIn

    Education

    • Free MongoDB Tutorials
    • Connect to MongoDB
    • Connect to MongoDB Atlas
    • Import Data to MongoDB
    • Export MongoDB Data
    • Build Aggregation Queries
    • Query MongoDB with SQL
    • Migrate from SQL to MongoDB

    Resources

    • Feedback and Support
    • Sales Support
    • Knowledge Base
    • FAQ
    • Reports
    • White Papers
    • Testimonials
    • Discounts

    Company

    • About Us
    • Blog
    • Careers
    • Legal
    • Press
    • Privacy Policy
    • EULA

    © 2023 3T Software Labs Ltd. All rights reserved.

    • Privacy Policy
    • Cookie settings
    • Impressum

    We value your privacy

    With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data.

    By clicking “Accept all”, you consent to the storage of cookies and the processing of personal data for these purposes, including any transfers to third countries. By clicking on “Decline all”, you do not give your consent and we will only store cookies that are necessary for our website. You can customize the cookies we store on your device or change your selection at any time - thus also revoking your consent with effect for the future - under “Manage Cookies”, or “Cookie Settings” at the bottom of the page. You can find further information in our Privacy Policy.
    Accept all
    Decline all
    Manage cookies
    ✕

    Privacy Preference Center

    With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data. Please choose for which purposes you wish to give us your consent and store your preferences by clicking on “Accept selected”. You can find further information in our Privacy Policy.

    Accept all cookies

    Manage consent preferences

    Essential cookies are strictly necessary to provide an online service such as our website or a service on our website which you have requested. The website or service will not work without them.

    Performance cookies allow us to collect information such as number of visits and sources of traffic. This information is used in aggregate form to help us understand how our websites are being used, allowing us to improve both our website’s performance and your experience.

    Google Analytics

    Google Ads

    Bing Ads

    Facebook

    LinkedIn

    Quora

    Hotjar

    Functional cookies collect information about your preferences and choices and make using the website a lot easier and more relevant. Without these cookies, some of the site functionality may not work as intended.

    HubSpot

    Social media cookies are cookies used to share user behaviour information with a third-party social media platform. They may consequently effect how social media sites present you with information in the future.

    Accept selected