Exploring MongoDB 7.0 with MongoDB Compass: A Comprehensive Guide - Part 4 Filtering Data in MongoDB
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 exp…

🔎 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:

- 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:

🔑 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"}
- We can find documents with their ids or their primary keys even using mongosh and for that, we could run the below -
// 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’.

- We can also run the below query to get the same results from mongosh
// 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}}
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 -
// 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}
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 -
// Find documents and limit the result to 10 documents
db.supplier.find().limit(10)