MongoDB Queries

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:02:00

What is a MongoDB query?

MongoDB Query is a means to retrieve data from the MongoDB database. MongoDB queries simplify retrieving data from the database, similar to SQL queries in SQL Database language. During a query procedure, one can apply parameters or conditions to retrieve specific data from the database. 

Field Selection

The find() method presents the database collection in Non-Structured form ({<Key>: <value>}), including the auto-created <key> "id" by MongoDB and collection data inserted by the user or admin.

Find a single document

In MongoDB, we may discover a single document using the findOne() method, which returns the first document that meets the filter query phrase.

Displaying documents in an organized manner

The pretty() method in MongoDB allows us to show documents from a specified collection in a well-formatted manner. 

What is Query Selector?

Query selectors are used in MongoDB queries to filter documents based on certain conditions.

Basic Operators for Query Selectors

  • $eq: Finds values equal to a given value.
  • $gt: Matches values greater than a given value.
  • $lt: Matches values that are less than a given value.
  • $gte: Finds values greater than or equal to a given value.
  • $lte: Returns values that are less than or equal to a given value.
  • $ne: Matches any value not equal to the specified value.

Logical Query Selectors

  • $and: Combines query clauses using a logical AND.
  • $or: Combines query clauses using a logical OR.
  • $not: Reverses the effect of a query expression, returning documents that do not match it.
  • $nor: Joins query clauses using a logical NOR and selects documents that fail all query expressions.

Element Query Selectors

  • $exists: Matches documents with the specified field.
  • $type: Selects documents whose fields are of the provided type.

Array Query Selectors

  • $in: Matches any of the values in the array.
  • $nin: Does not match any of the values supplied in an array.
  • $all: Matches arrays containing all of the elements supplied in the query.
  • $size: Matches arrays that have a certain number of elements.
  • $elemMatch: Returns documents that have an array field with at least one element that meets all of the specified query requirements.
Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
Accept cookies & close this