Bulk.find.upsert()

Tip

Starting in version 3.2, MongoDB also provides thedb.collection.bulkWrite() method for performing bulkwrite operations.

Description

  • Bulk.find.upsert()

New in version 2.6.

Sets the upsert option to true for an update or areplacement operation and has the following syntax:

  1. Bulk.find(<query>).upsert().update(<update>);
  2. Bulk.find(<query>).upsert().updateOne(<update>);
  3. Bulk.find(<query>).upsert().replaceOne(<replacement>);

With the upsert option set to true, if no matching documentsexist for the Bulk.find() condition, then the update orthe replacement operation performs an insert. If a matching documentdoes exist, then the update or replacement operation performs thespecified update or replacement.

Use Bulk.find.upsert() with the following write operations:

Behavior

The following describe the insert behavior of various write operationswhen used in conjunction with Bulk.find.upsert().

Insert for Bulk.find.replaceOne()

The Bulk.find.replaceOne() method accepts, as its parameter,a replacement document that only contains field and value pairs:

  1. var bulk = db.items.initializeUnorderedBulkOp();
  2. bulk.find( { item: "abc123" } ).upsert().replaceOne(
  3. {
  4. item: "abc123",
  5. status: "P",
  6. points: 100,
  7. }
  8. );
  9. bulk.execute();

If the replacement operation with the Bulk.find.upsert()option performs an insert, the inserted document is the replacementdocument. If neither the replacement document nor the query documentspecifies an _id field, MongoDB adds the _id field:

  1. {
  2. "_id" : ObjectId("52ded3b398ca567f5c97ac9e"),
  3. "item" : "abc123",
  4. "status" : "P",
  5. "points" : 100
  6. }

Insert for Bulk.find.updateOne()

The Bulk.find.updateOne() method accepts, as its parameter,an update document that contains only field and value pairs oronly update operator expressions.

Field and Value Pairs

If the update document contains only field and value pairs:

  1. var bulk = db.items.initializeUnorderedBulkOp();
  2. bulk.find( { status: "P" } ).upsert().updateOne(
  3. {
  4. item: "TBD",
  5. points: 0,
  6. inStock: true,
  7. status: "I"
  8. }
  9. );
  10. bulk.execute();

Then, if the update operation with the Bulk.find.upsert()option performs an insert, the inserted document is the updatedocument. If neither the update document nor the query documentspecifies an _id field, MongoDB adds the _id field:

  1. {
  2. "_id" : ObjectId("52ded5a898ca567f5c97ac9f"),
  3. "item" : "TBD",
  4. "points" : 0,
  5. "inStock" : true,
  6. "status" : "I"
  7. }

Update Operator Expressions

If the update document contains contains only updateoperator expressions:

  1. var bulk = db.items.initializeUnorderedBulkOp();
  2. bulk.find( { status: "P", item: null } ).upsert().updateOne(
  3. {
  4. $setOnInsert: { defaultQty: 0, inStock: true },
  5. $currentDate: { lastModified: true },
  6. $set: { points: "0" }
  7. }
  8. );
  9. bulk.execute();

Then, if the update operation with the Bulk.find.upsert()option performs an insert, the update operation inserts a document withfield and values from the query document of theBulk.find() method and then applies the specified update fromthe update document. If neither the update document nor the query documentspecifies an _id field, MongoDB adds the _id field:

  1. {
  2. "_id" : ObjectId("52ded68c98ca567f5c97aca0"),
  3. "item" : null,
  4. "status" : "P",
  5. "defaultQty" : 0,
  6. "inStock" : true,
  7. "lastModified" : ISODate("2014-01-21T20:20:28.786Z"),
  8. "points" : "0"
  9. }

Insert for Bulk.find.update()

When using upsert() with the multiple documentupdate method Bulk.find.update(), if no documents match thequery condition, the update operation inserts a single document.

The Bulk.find.update() method accepts, as its parameter, anupdate document that contains onlyupdate operator expressions:

  1. var bulk = db.items.initializeUnorderedBulkOp();
  2. bulk.find( { status: "P" } ).upsert().update(
  3. {
  4. $setOnInsert: { defaultQty: 0, inStock: true },
  5. $currentDate: { lastModified: true },
  6. $set: { status: "I", points: "0" }
  7. }
  8. );
  9. bulk.execute();

Then, if the update operation with the Bulk.find.upsert()option performs an insert, the update operation inserts a singledocument with the fields and values from the query document ofthe Bulk.find() method and then applies the specified updatefrom the update document. If neither the update documentnor the query document specifies an _id field, MongoDB addsthe _id field:

  1. {
  2. "_id": ObjectId("52ded81a98ca567f5c97aca1"),
  3. "status": "I",
  4. "defaultQty": 0,
  5. "inStock": true,
  6. "lastModified": ISODate("2014-01-21T20:27:06.691Z"),
  7. "points": "0"
  8. }

See also