nerdexam
MongoDB

C100DBA · Question #113

You perform the following operation in the shell: db.foo.insert( { } ); What gets inserted?

The correct answer is C. A document with an _id assigned to be an Objectld. When you insert {} into MongoDB, the database automatically generates and appends an _id field with a unique ObjectId value before writing the document - making C correct. MongoDB requires every document to have an _id, so if you don't supply one, it creates it for you. Why the d

MongoDB Fundamentals

Question

You perform the following operation in the shell: db.foo.insert( { } ); What gets inserted?

Options

  • AA document will be inserted with the same _id as the last document inserted
  • BA document that matches the collection's existing schema, but with null fields
  • CA document with an _id assigned to be an Objectld
  • DAn empty document
  • ENo document will be inserted; an error will be raised

How the community answered

(63 responses)
  • A
    5% (3)
  • B
    3% (2)
  • C
    90% (57)
  • D
    2% (1)

Explanation

When you insert {} into MongoDB, the database automatically generates and appends an _id field with a unique ObjectId value before writing the document - making C correct. MongoDB requires every document to have an _id, so if you don't supply one, it creates it for you.

Why the distractors fail:

  • A - MongoDB never reuses or copies a previous document's _id; each ObjectId is globally unique by design.
  • B - MongoDB is schemaless; collections have no enforced schema to match, and there is no concept of "null fields" derived from other documents.
  • D - Tempting, but not quite right: the stored document is { _id: ObjectId("...") }, not truly empty.
  • E - MongoDB accepts empty documents without complaint; no error is raised.

Memory tip: Think of MongoDB as a "stamp machine" - every document that enters the collection gets an _id stamp automatically if you don't bring your own, so an empty {} always comes out as { _id: ObjectId(...) }.

Topics

#insert#ObjectId#document creation

Community Discussion

No community discussion yet for this question.

Full C100DBA Practice