MongoDB Interview Questions

MongoDB Interview Questions

List of most frequently asked MongoDB Interview Questions. Questions from basics to advance level with the solution.

Let’s cover MongoDB today. In this article, we will focus basically on the syntax of how to create collections, insert documents, update and delete documents, creating users, and some of the other fundamentals of MongoDB.

Mongo is a NoSQL database, there are different types of NoSQL databases and hence to be more specific mongo is also called a document database. Data or the records are stored in the form of documents with JSON-like syntax. The major advantage of mongo databases is scaling, it is super easy to scale compared to SQL databases. They are also fast in most types of operations.

Now, let’s jump on the topic of MongoDB Interview Questions.

1. Create a new user for the database.

db.createUser({
user:"Tony Stark",
pwd:"12345",
roles:[ "readWrite" , "dbAdmin" ]
});

 

2. Create a new collection with name customers.

db.createCollection('customers');

 

3. Insert a document into the collection.

db.customers.insert({
first_name:"Jhon",
last_name:"Wick"
});

 

4. How to see the list of documents in a collection?

db.customers.find();

 

5. Insert multiple documents in a collection.

db.customers.insert([{first_Name:”Thanos”, last_Name:”Singh”},
{first_Name:”Mukesh”, last_Name:”Kumar”},
{first_Name:”Donald”, last_Name:”Trump”, gender:”Male”}

]);

 

6. Update the above document and add gender for Thanos.

db.customers.update({first_Name:”Thanos”},
{first_Name:”Thanos”, last_Name:”Singh”,gender:”Male”});

 

7. Update the above document and add age for Mukesh using Set.

db.customers.update({first_Name:”Mukesh”},
{$Set: {age:45}});

 

8. Update the above document and increment the age of Mukesh by 5.

db.customers.update({first_Name:”Mukesh”},
{$inc: {age:5}});

 

9. Remove the field age from Mukesh

db.customers.update({first_Name:”Mukesh”},
{$unset: {age:1}});

 

10. Use upsert to update a new record if it does not exist in the document.

db.customers.update({{first_Name:”Doctor”}, 
{first_Name:”Doctor”, last_Name:”Strange”, gender:”Male”},
{upsert:true});

 

11. Rename the field gender to sex for Donald.

db.customers.update({first_Name:”Donald”},
{$rename: {“gender”:sex}});

 

12. Query to remove a document.

db.customers.remove({first_name:”Thanos”});

 

13. How to find any document (say for Donald)  in a collection?

db.customers.find({first_Name:”Donald”});

 

14. How to find documents of the first name of Donald and Thanos in a collection?

db.customers.find({$or[{first_Name:”Donald”},
{first_Name:”Thanos”}]});

 

15. Find customers of age less than 45.

db.customers.find({age:{$lt:45}});

 

16. Find customers of age greater than 30.

db.customers.find({age:{$gt:30}});

 

17. How to sort the document in ascending order on the basis of their last name.

db.customers.find().sort({last_name:1});

 

18. How to sort the document in descending order on the basis of their last name.

db.customers.find().sort({last_name:-1});

 

19. How to find out the count of documents?

db.customers.find().count();

 

20. How to find out the count of documents with a gender female?

db.customers.find(gender:”female”).count();

 

Above are the most asked MongoDB Interview Questions.

Check Database Testing Interview Questions

Wikipedia MongoDB

Leave a Comment

Your email address will not be published.