Exploring MongoDB 7.0 with MongoDB Compass: A Comprehensive Guide - Part 4 Filtering Data in MongoDB
Exploring MongoDB 7.0 with MongoDB Compass: A Comprehensive Guide - Part 4 Filtering Data in MongoDB

Exploring MongoDB 7.0 with MongoDB Compass: A Comprehensive Guide - Part 4 Filtering Data in MongoDB

πŸ”Ž Perform a β€œread” operation on the MongoDB by using the Filter input section

The Filter input section in MongoDB Compass helps in performing read operations on the collection by specifying a query in JSON format. You can use various query operators and expressions to filter the documents that match your criteria. Here are some examples of how to use the Filter input section:
  • To perform a read operation on the collection, you can use the Filter input box on the top left corner of the collection view. This will allow you to enter a query in JSON format and filter the documents that match the query. You can also use optional arguments such as sort, projection, limit, and skip to modify the query results. For example:
    • notion image
  • We can also use the find() method. This method takes a filter object as an argument and returns a cursor object that allows us to iterate over the matching documents. We can also use optional arguments such as sort, projection, limit, and skip to modify the query results. For example:
    • notion image

πŸ”‘ Find a document by its _id

  • To find a document by its _id, you can use the _id field and provide the value of the _id that you are looking for. For example, to find the document with the _id of f5c949ac-a31c-4e53-95fc-a3310edf96da, you can enter the following query in the Filter input section:
    • This will return only one document that matches the _id value.
      {"_id" : "f5c949ac-a31c-4e53-95fc-a3310edf96da"}
      notion image
  • We can find documents with their ids or their primary keys even using mongosh and for that, we could run the below -
    • Idea IconTheTechCruise.com Pyodide Terminal
      // Find one document by its _id
      db.supplier.find({"_id" : "f5c949ac-a31c-4e53-95fc-a3310edf96da"})

πŸ“„ Find documents based on other document properties

  • To find documents based on other document properties, you can use the field names and provide the values or conditions that you are looking for. For example, to find documents that have a contact name starting with β€˜L’, you can enter the following query in the Filter input section:
    • {"contact_name" : /^L/}
      This will return all documents that have a contact name that begins with β€˜L’. The /^L/ is a regular expression that matches any string that starts with β€˜L’.
      notion image
  • We can also run the below query to get the same results from mongosh
    • Idea IconTheTechCruise.com Pyodide Terminal
      // Find documents that have contact_name starting with 'L'
      db.supplier.find({"contact_name" : /^L/})

πŸ“Š Find documents based on sort, projection, etc.

  • To find documents based on sort, projection, etc., you can use optional arguments such as sort, project, limit, and skip to modify the query results. These arguments are entered as key-value pairs after a comma in the Filter input section. For example, to find documents and sort them by name in ascending order, and only return the name and contact email fields, you can enter the following query in the Filter input section:
    • {"contact_name" : /^L/}, {sort: {"name" : 1}, project: {"name" : 1, "contact_email" : 1}}
      notion image
      This will return all documents in the collection that match the criteria, but only show the name and contact email fields, and sort them by name in ascending order. The sort: {"name" : 1} means sort by name in ascending order (use -1 for descending order). The project: {"name" : 1, "contact_email" : 1} means only show the name and contact email fields (use 0 to exclude fields).
  • We can also accomplish the same by using mongosh -
    • Idea IconTheTechCruise.com Pyodide Terminal
      // Find documents and sort them by name in ascending order
      db.supplier.find().sort({"name" : 1})
      
      // Find documents and only return the name and contact_email fields
      db.supplier.find({"contact_name" : /^L/}, {"name" : 1, "contact_email" : 1})
      

πŸ”’ Find documents and limit the result to a certain number of documents

  • To find documents and limit the result to a certain number of documents, you can use the limit argument to specify how many documents you want to return. For example, to find documents and limit the result to 10 documents, you can enter the following query in the Filter input section:
    • {}, {limit: 10}
      notion image
      This will return only 10 documents from the collection. The {} is an empty filter object that matches all documents. The limit: 10 means only return 10 documents.
  • We can find documents with their ids or their primary keys even using mongosh and for that, we could run the below -
    • Idea IconTheTechCruise.com Pyodide Terminal
      // Find documents and limit the result to 10 documents
      db.supplier.find().limit(10)

      About Authors

      Sai Manasa Ivaturi

      πŸ‘‰πŸ»GitHub
      πŸ‘‰πŸ»GitHub
      πŸ‘‰πŸ»Medium
      πŸ‘‰πŸ»Medium

       πŸ‘‰πŸ»Email
      πŸ‘‰πŸ»Email
      πŸ‘‰πŸ»Linkedin
      πŸ‘‰πŸ»Linkedin

      I'm a Software Development Engineer based in Atlanta, Georgia with 5+ years of experience in the software industry. My focus area has been Backend development and full-stack development.
      I'm a Software Development Engineer based in Atlanta, Georgia with 5+ years of experience in the software industry. My focus area has been Backend development and full-stack development.
       View my Resume here.
      View my Resume here.
      Masters Degree in Computer Science Indiana University, Bloomington
Jan 22 - May 23
      Masters Degree in Computer Science Indiana University, Bloomington Jan 22 - May 23
      Bachelors Degree in Computer Science  Pragati Engineering College, India
Aug 14 - April 18
      Bachelors Degree in Computer Science Pragati Engineering College, India Aug 14 - April 18
      Certifications and badges
      Certifications and badges
      πŸ‘‰πŸ»Verify
      πŸ‘‰πŸ»Verify
      πŸ‘‰πŸ»Verify
      πŸ‘‰πŸ»Verify
      πŸ‘‰πŸ»Verify
      πŸ‘‰πŸ»Verify

      Srinivas vaddi


      πŸ‘‰GitHub
      πŸ‘‰GitHub
      πŸ‘‰Medium
      πŸ‘‰Medium
       πŸ‘‰Email
      πŸ‘‰Email
      πŸ‘‰Linkedin
      πŸ‘‰Linkedin

      Hi! I’m a recent master’s graduate from Indiana University Bloomington (IUB) πŸŽ“ and a Software Development Engineer with 4+ years of experience. Looking for #jobs!
      Hi! I’m a recent master’s graduate from Indiana University Bloomington (IUB) πŸŽ“ and a Software Development Engineer with 4+ years of experience. Looking for #jobs!
      My areas of expertise are Software Development, DevOps, Testing, Integration, Data Engineering and Data Analytics. Mostly worked on Python, Django/Flask, Apache Airflow, Apache Spark, AWS, and DevOps. I have a versatile background & a β€˜can do’ attitude πŸ€“.
      My areas of expertise are Software Development, DevOps, Testing, Integration, Data Engineering and Data Analytics. Mostly worked on Python, Django/Flask, Apache Airflow, Apache Spark, AWS, and DevOps. I have a versatile background & a β€˜can do’ attitude πŸ€“.
      πŸ‘¨πŸ»β€πŸ’»
      I like blogging and sharing knowledge. I’ve built a server at home from scratch! I used it to learn various technologies and to contribute to the open-source. I love tech, philosophy, literature, and history. My favorite books πŸ“š of all time are β€˜The Alchemist’ and β€˜Chanakya Neeti’ πŸ™Œ.
      Masters Degree in Computer Science  Indiana University, Bloomington
      Masters Degree in Computer Science Indiana University, Bloomington Aug 23, 2021 β†’ Dec 17, 2022
      Bachelors Degree in Computer Science  Gitam University (Deemed to be)
      Bachelors Degree in Computer Science Gitam University (Deemed to be) Jun 1, 2014 β†’ Apr 1, 2018
      Certifications and badges
      Certifications and badges
      Β 
      notion image
      πŸ‘† verify
      Β 
      Β 
      notion image
      πŸ‘†Β verify
      Β 
      Β 
      Β 
      notion image
      πŸ‘† verify
      notion image
      πŸ‘† verify
      notion image
      Β  πŸ‘† verify
      Β 
Buy us a coffeeBuy us a coffee