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)

advertisement
 
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: a
Explanation: Documents within a collection can have different fields.

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.

advertisement

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 Reference Books in MongoDB.

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter