π Perform a βreadβ operation on the MongoDB by using the Filter input sectionπ Find a document by its _idπ Find documents based on other document propertiesπ Find documents based on sort, projection, etc.π’ Find documents and limit the result to a certain number of documents
π 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
off5c949ac-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 -
TheTechCruise.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β.- We can also run the below query to get the same results from
mongosh
TheTechCruise.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
, andskip
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
-
TheTechCruise.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}
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 -
TheTechCruise.com Pyodide Terminal
// Find documents and limit the result to 10 documents
db.supplier.find().limit(10)
About Authors
Sai Manasa Ivaturi
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.
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
Certifications and badges
Srinivas vaddi
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 π€.
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 Aug 23, 2021 β Dec 17, 2022
Bachelors Degree in Computer Science Gitam University (Deemed to be) Jun 1, 2014 β Apr 1, 2018