MongoDB interview questions & answers

    0 Votes

34) Why to use “Object ID” datatype in MongoDB?

“Object ID” datatype is used to store document’s ID.
35) How to insert a Document in a Collection?
To insert a Document in Collection we can use below commands –
  • Insert - db.CollectionName.Insert(document)
  • Save - db.CollectionName.Save(document)

 

36) What is “ObjectID” is composed of?
ObjectID is composed of following things –
  • Time Stamp
  • Client Machine ID
  • Client Process ID
  • 3 byte incremented counter
37) What are Indexes in MongoDB?

Indexes are used to execute the query efficiently. Without Indexes, MongoDB will perform scan i.e, scanning all the documents in the collection.

38) How to give Indexes in MongoDB?

Indexes can be used like below –

db.COLLECTION_NAME.ensureIndex ( {ColumnName:1} )
ensureIndex() is used for creating an Index and “1” for ascending order, “-1” for descending order.

39) What are the alternatives to MongoDB?

Below are the alternatives to MongoDB –

  • Cassandra
  • Redis
  • Riak
  • CouchDB

40) How to query documents in a Collection in MongoDB?

We can find a required Document in a Collection using command – “find()”

Db.CollectionName.find()

41) What is the method used to format the result in MongoDB?

"pretty()" method is used to format the results.
Db.CollectionName.find().pretty()

42) Give me an example of how we can apply “AND” or “OR” condition while querying a document in a Collection?

We can use “AND” or “OR’’ queries like below –

db. CollectionName.find({$and:[{"Name":"AAA"},{"Address": "Test Addr"}]}).pretty()
db. CollectionName.find({$or:[{"Name":"AAA"},{"Address": "Test Addr"}]}).pretty()

43) How to update Documents in MongoDB?

For updating documents, we need to use below method –

db. CollectionName.update({filter criteria}, {$set: {new data}}, {mutli:true})

By default MongoDB update only one Document. To update multiple Documents we need to specify - {mutli:true}.

44) How to delete Documents in MongoDB?

For deleting documents, we need to use below method –

db. CollectionName.remove({filter criteria})

45) Why to use the “Skip()” method in MongoDB?

“Skip()” method is used to skip number of Documents as given in the method.

Skip(3) - Skips 3 Documents.

46) How we can sort the Documents in MongoDB?

To sort the Documents we can use – “Sort()” method.

db. CollectionName.find({filter criteria }).sort({ColumnName:1})

Sort() method accepts the field on which the sorting has to be done and the sorting order –

“1” – Ascending.
"-1" – Descending.

47) What is Aggregation in MongoDB?

Aggregation is used to process data and return the computed results. Aggregation can perform a variety of operations on the grouped data and finally returns the single result. It is like SQL Server Aggregators. 

Page 3 of 3

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

SQL Queries Interview Questions and Answers
Database & SQL Interview Questions
Oracle Interview Questions and Answers on SQL Queries and Database Theory
Advanced SQL Interview Questions and Answers
SQL Server Interview Questions and Answers