Starting with MongoDB Basic queries

MongoDB is a popular NoSQL database that uses a document-oriented data model. One of the key advantages of MongoDB is its flexible query language, which enables users to query data in a variety of ways.

Once your database is up and running, You can use the following method to login to the database. In case you do not know how to create your own database on cloud for free then please refer to my previous post here.

Open the MongoDB shell: You can open the MongoDB shell by running the mongo command in your terminal or command prompt.

Switch to the user of the database having admin rights:
In the MongoDB shell, you need to switch to the admin database by running the command use admin

Below are some of the basic queries that you can start with:

1. Run the show databases command: Once you are in the admin database, you can run the show databases command to see a list of all the databases in your MongoDB instance.
Here is an example of what the output of the show databases command might look like:



2. To switch to a different database in MongoDB, you can use the use command in the MongoDB shell. Here are the steps to switch to a different database:

In the MongoDB shell, you can switch to a different database by running the use command followed by the name of the database you want to switch to. Please see the screenshot below:

3. To show a list of collections in a MongoDB database, you can use the show collections method. Here are the steps to use this command:

Switch to the database: In the MongoDB shell, you need to switch to the database that you want to list collections for. You can do this by running the use command followed by the name of the database.

Run the show collections command: Once you are in the correct database, you can run the show collections command to see a list of all the collections in that database.Here is an example of what the output of the show collections command might look like:

4. The count() method in MongoDB is used to count the number of documents in a collection that match a specified filter. The syntax of the count() method is as follows:

db.collection.count(query, options)

Here is a brief explanation of the two parameters:

  • query (optional): Specifies the filter for which documents to count. This can be a query document that uses the same syntax as a find() method.
  • options (optional): Specifies additional options for the operation, such as a limit on the number of documents to count.

If no filter is provided, the count() method will return the total number of documents in the collection.

Here is an example of how to use the count() method to count the number of documents in a collection:

This will return the total number of documents in the “users” collection.

If you want to count the number of documents that match a specific filter, you can pass that filter as the first parameter to the count() method. For example, the following query will count the number of documents in the “users” collection where the age field is greater than 18:

This will return the number of documents in the “users” collection that have an age field greater than 18.

5. findOne() is a method in MongoDB that allows you to retrieve a single document from a collection that matches a specified set of criteria. The method returns the first document that matches the query criteria. Here is the syntax for using findOne():

db.collection.findOne(query, projection)

This query will return the first document from the employee collection. Note that we have not used any query condition here.

If you want to include only specific fields in the result, you can use the projection parameter like this:

This query will return only the department field for the first document where the firstName field is “Lokesh”. The _id field is always returned by default, but you can exclude it by setting its value to 0 in the projection parameter.


6. In MongoDB, the sort() method is used to sort the results of a query in ascending or descending order based on one or more fields. The sort() method can be used with the find() method to sort the results of a query.

Here is the syntax for using the sort() method:

db.collection.find(query, projection).sort(sorting_criteria)

  • query: a document that specifies the selection criteria for the document to be returned.
  • projection: an optional document that specifies which fields should be returned in the resulting document. By default, all fields are returned.
  • sorting_criteria: a document that specifies the field or fields to sort on and the order in which to sort them. The sorting criteria can be specified using the 1 for ascending order or -1 for descending order.

For example, the above query will return 1 document from employee collection sorted in descending order on firstName.

Note that the sort() method can have a performance impact on large collections, so it’s important to use it judiciously and only when necessary.

7. In MongoDB, the skip() method is used to skip a specified number of documents from the beginning of a query result. The skip() method is often used in conjunction with the find() method to implement pagination of query results.
Here’s the basic syntax of the skip() method

db.collection.find().skip(num)

where collection is the name of the collection and num is the number of documents to skip.

Here’s an example of how to use the skip() method to skip the first 10 documents in a collection and return the remaining documents:
db.mycollection.find().skip(10)

This will return all the documents in the mycollection collection, except for the first 10.

You can also use the limit() method in conjunction with skip() to specify the maximum number of documents to return. Here’s an example:
db.mycollection.find().skip(10).limit(5)

This will skip the first 10 documents in the mycollection collection and return the next 5 documents.

Note that the skip() method can have a performance impact on large collections, so it’s important to use it judiciously and only when necessary.

Here is a video demonstrating all of the above in an easy to understand manner.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.