Navigation

Bulk Write

Overview

The code snippets on this page demonstrate how to perform a bulk write operation with the collection.bulkWrite() function.

Note

Data Lake data sources do not support write operations.

Snippet Setup

To use a code snippet in a function, you must first instantiate a MongoDB collection handle:

exports = function() {
  const mongodb = context.services.get("mongodb-atlas");
  const itemsCollection = mongodb.db("store").collection("items");
  const purchasesCollection = mongodb.db("store").collection("purchases");
  // ... paste snippet here ...
}

Examples

The following example inserts two documents into the store.purchases collection:

exports = async function(arg){
    const doc1 = { "name": "velvet elvis", "quantity": 20, "reviews": [] };
    const doc2 = { "name": "mock turtleneck",  "quantity": 30, "reviews": [] };

    var collection = context.services.get("mongodb-atlas")
        .db("store")
        .collection("purchases");

    return await collection.bulkWrite(
        [{ insertOne: doc1}, { insertOne: doc2}],
        {ordered:true});
};
←   Transactions Query Roles  →