MongoDB Questions and Answers – Generate Test Data

This set of MongoDB Multiple Choice Questions & Answers (MCQs) focuses on “Generate Test Data”.

1. Which of the following will implicitly create the collection testData?
a)

 
while (var i = 1; i <= 25; i++) 
{
     db.testData.insert( { x : i } )
} 

b)

 
for (var i = 1; i <= 25; i++) 
{
   db.testData.insert( { x : i } )
} 

c)

 
for (var i = 1; i <= 25; i++)
{
   db.testData.inserts( { x : i } )
} 

d) None of the mentioned
View Answer

Answer: b
Explanation: From the mongo shell, use the for loop. If the testData collection does not exist, MongoDB will implicitly create the collection.

advertisement
advertisement

2. Point out the correct statement.
a) Collection is a group of MongoDB documents
b) MongoDB is an open-source document database, and leading NoSQL database
c) Collections do not enforce a schema
d) All of the mentioned
View Answer

Answer: d
Explanation: MongoDB is an open-source document-oriented database that stores data in collections made up of related documents. MongoDB collections do not enforce a schema, allowing for greater flexibility in data modeling. Therefore, all of the statements are true.

3. Which of the following method returns a cursor?
a) find
b) sort
c) skip
d) all of the mentioned
View Answer

Answer: a
Explanation: The find() method returns a cursor. To iterate the cursor and return more documents, type it in the mongo shell.

Note: Join free Sanfoundry classes at Telegram or Youtube

4. Which of the following function is valid for creation of new data inside MongoDB Shell?
a)

function insertData(dbName, colName, num) 
{

  var col = db.getSiblingDB(dbName).getCollection(colName);

  for (i = 0; i < num; i++) 
  {
      col.insertData({x:i});
  }

  print(col.count());

} 

b)

advertisement
function insertData(dbName, colName, num) 
{

  var col = db.getSiblingDB(dbName).getCollection(colName);

  for (i = 0; i < num; i++) 
  {
    col.insert({x:i});
  }

  print(col.count());

} 

c)

 
insertData(dbName, colName, num) 
{

  var col = db.getSiblingDB(dbName).getCollection(colName);

  for (i = 0; i < num; i++) 
  {
    col.insert({x:i});
  }

  print(col.count());

} 

d) None of the mentioned
View Answer

Answer: b
Explanation: insertData() JavaScript function that follows creates new data for use in testing or training by either creating a new collection or appending data to an existing collection.

advertisement

5. Point out the wrong statement.
a) Database is a physical container for collections
b) A single MongoDB server typically has single databases
c) Collection is the equivalent of an RDBMS table
d) None of the mentioned
View Answer

Answer: b
Explanation: A single MongoDB server typically has multiple databases.

6. _id is a ________ bytes hexadecimal number which assures the uniqueness of every document.
a) 13
b) 16
c) 12
d) 10
View Answer

Answer: c
Explanation: You can provide _id while inserting the document.

7. Which of the following statement will insert 400 documents in to the testData collection?
a) insertData("test", "testData", 200)
b) insertData("test", "testData", 400)
c) insertData("test", "testData", 600)
d) all of the mentioned
View Answer

Answer: b
Explanation: If the collection and database do not exist, MongoDB creates them implicitly before inserting documents.

8. The mongo shell loads and parses the ___________ file on startup.
a) mongo.js
b) mongoc.js
c) mongorc.js
d) all of the mentioned
View Answer

Answer: c
Explanation: Store the function in your .mongorc.js file.

9. To iterate the cursor and return more documents, type _________ in the mongo shell.
a) cursor
b) it
c) next
d) none of the mentioned
View Answer

Answer: b
Explanation: The db.collection.find() method returns a cursor. To access the documents, you need to iterate the cursor.

10. Which of the following key is used to denote uniqueness in the collection?
a) _id
b) id
c) id_
d) none of the mentioned
View Answer

Answer: a
Explanation: _id is the default key provided by mongodb itself.

Sanfoundry Global Education & Learning Series - MongoDB.

Here's the list of Best Books in MongoDB.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.