Creating documents in MongoDB

Date : Jul 14, 2025

Creating documents in MongoDB

Creating documents in MongoDB is a fundamental aspect of using this NoSQL database, which stores data in flexible, JSON-like BSON (Binary JSON) format. MongoDB stores data in collections, which are analogous to tables in relational databases. Each record within a collection is called a document, and it is represented in BSON format. A document typically consists of key-value pairs.

Example of a BSON document

{
    "_id": ObjectId("60b621de57d3c8b5e3a793df"),
    "name": "Tom",
    "age": 25,
    "address": {
        "street": "123 Main St",
        "city": "Paris",        
        "zipcode": "123-456"
    },
    "interests": ["coding", "reading", "traveling"]
}

Inserting Single Document

The db.collection.insertOne() method is used to insert a single document.

Example:

db.users.insertOne({
    "name": "Alex",
    "age": 25,
    "email": "alex@example.com"
});

Output:

{
  acknowledged: true,
  insertedId: ObjectId('6846ef545b420fb0bab5f899')
}

insertOne() returns a document that includes the newly inserted document’s _id field value.

Inserting Multiple Documents

db.collection.insertMany() can insert multiple documents into a collection by passing an array of documents.

Example.

db.users.insertMany([
    { "name": "Alice", "age": 30, "email": "alice@example.com" },
    { "name": "Bob", "age": 28, "email": "bob@example.com" }
])

Output:

{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId('6846ef885b420fb0bab5f89a'),
    '1': ObjectId('6846ef885b420fb0bab5f89b')
  }
}

If the documents do not specify an _id field, MongoDB adds the _id field with an ObjectId value to each document. And, if the documents do specify an _id field in insertOne() or insertMany() methods, MongoDB will add the provided _id to the documents and return the same _id value.

Exmaple

 db.users.insertOne({
        "_id" : 100, 
        "name" : "Ben", 
        "age" : 26, 
        "email" : "ben@example.com"
})

Ourput:

{ 
    acknowledged: true, 
    insertedId: 100 
}

Important Mongo DB Insert Behaviour

  • If the collection does not exists, insert operation will create the collection.
  • Each document stored in a collection requires a unique _id field that acts as a primary key and if an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field. This also applies to documents inserted through update operations with upsert:true.
   db.users.updateOne(
      { "name" : "Rob" },
      { $set: {"_id" : 200, "age" : 20, "email" : "rob@example.com" } },
      { upsert: true }
   );

Output

  {
    acknowledged: true,
    insertedId: 200,
    matchedCount: 0,
    modifiedCount: 0,
    upsertedCount: 1
  }
  • All write operations in MongoDB are atomic at the level of the document.

Additional Methods for Inserts

The following methods can also add new documents to a collection.

  • db.collection.updateOne() when used with the upsert: true option.
  • db.collection.updateMany() when used with the upsert: true option.
  • db.collection.findAndModify() when used with the upsert: true option.
  • db.collection.findOneAndUpdate() when used with the upsert: true option.
  • db.collection.findOneAndReplace() when used with the upsert: true option.
  • db.collection.bulkWrite().

References

https://www.mongodb.com/docs/manual/tutorial/insert-documents/

https://www.mongodb.com/docs/manual/reference/insert-methods/

First published on : Jul 14, 2025