Queries are another essential element of a database system. When your database is having all the data fed into it, and you want to retrieve the data by executing some command, MongoDB allows you to do that. In this chapter, you will learn about the different ways of how queries can be made using MongoDB.



What Is a Database Query?

A query in a database system is a command that is used for extracting data from a database and display it in a readable form. Every query associated with the database system is associated with any particular language (such as SQL for structured data, MongoDB for unstructured data). Queries can be explained by taking a suitable example:

Let us assume a situation where your database has an employee table, and you wish to track the sales performance ID, so you have to write a query to ask your database to fetch for you the list of all the sales performance with the highest sales data in the top. This is where the queries of a database language become useful.

Methods for Performing Queries in MongoDB

  1. The find() method: This method is used for querying data from a MongoDB collection.
    The basic syntax for using this method is:

    Syntax:

    db.collection_name.find()

    Example:

    db.writers.find()

    Various other options can be used to make the query specific. It will be discussed below.

  2. The pretty() method: This method is used for giving a proper format to the output extracted by the query.
    The basic syntax for using this method is:

    Syntax:

    db.collection_name.find().pretty()

    Example:

    db.writers.find().pretty()

Here is how they can be implemented:

Filtering Criteria in MongoDB Queries

It is also possible to filter your results by giving or adding some specific criteria in which you are interested to. For example, if you wish to see the Gaurav Mandes data, you can add a specific attribute to the find() to fetch the data of Gaurav Mandes from that particular database.

Example:

db.writers.find( { author: "Gaurav Mandes" } )

Output:


MongoDB Query Which Specify "AND" Condition

MongoDB also allows you in specifying data values of the documents holding two or more specified values to be fetched from the query. Here are two examples showing the use of specifying queries using AND.

Example:

db.writers.find( { tools: "Visual Studio", born: 1948} )

MongoDB Query Which Specify "OR" Condition

MongoDB allows users to specify either one or multiple values to be true. According to this, till one of the conditions is true, the document data will get returned. Here is an example showing the use of OR condition:

Example:

db.musicians.find({$or: [ { instrument: "Drums" }, { born: 1945 } ] } )

Output:

$in operator

The $in operator is another special operator used in queries for providing a list of values in the query. When your document holds any of those provided values, it gets returned. Here is an example:

Example:

db.musicians.find( { "instrument": { $in: [ "Keyboards", "Bass" ] } } )

Output:



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram