Docs Menu

Docs HomeMongoDB Manual

Bulk.insert()

On this page

  • Description
  • Behavior
  • Example

Tip

Starting in version 3.2, MongoDB also provides the db.collection.bulkWrite() method for performing bulk write operations.

Bulk.insert(<document>)

Adds an insert operation to a bulk operations list.

Bulk.insert() accepts the following parameter:

Parameter
Type
Description
doc
document
Document to insert. The size of the document must be less than or equal to the maximum BSON document size.

Even if you encounter a server error during an insert, some documents may have been inserted.

After a successful insert, the system returns BulkWriteResult.nInserted, the number of documents inserted into the collection. If the insert operation is interrupted by a replica set state change, the system may continue inserting documents. As a result, BulkWriteResult.nInserted may report fewer documents than actually inserted.

The following initializes a Bulk() operations builder for the items collection and adds a series of insert operations to add multiple documents:

var bulk = db.items.initializeUnorderedBulkOp();
bulk.insert( { item: "abc123", defaultQty: 100, status: "A", points: 100 } );
bulk.insert( { item: "ijk123", defaultQty: 200, status: "A", points: 200 } );
bulk.insert( { item: "mop123", defaultQty: 0, status: "P", points: 0 } );
bulk.execute();

Tip

See also:

←  Bulk.getOperations()Bulk.tojson() →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.