Navigation

collection.insertMany()

Definition

collection.insertMany()

Insert one or more documents into a collection and return a list of the _id values for each inserted document.

Usage

Example

To call the collection.insertMany() action from a Function, get a collection handle with database.collection() then call the handle’s insertMany() method.

const doc1 = { "name": "basketball", "category": "sports", "quantity": 20, "reviews": [] };
const doc2 = { "name": "football",   "category": "sports", "quantity": 30, "reviews": [] };

return itemsCollection.insertMany([doc1, doc2])
  .then(result => {
    console.log(`Successfully inserted ${result.insertedIds.length} items!`);
    return result
  })
  .catch(err => console.error(`Failed to insert documents: ${err}`))

To call the collection.insertMany() action from a JavaScript SDK, use the RemoteMongoCollection.insertMany() method.

const doc1 = { "name": "basketball", "category": "sports", "quantity": 20, "reviews": [] };
const doc2 = { "name": "football",   "category": "sports", "quantity": 30, "reviews": [] };

itemsCollection.insertMany([doc1, doc2])
  .then(result => {
    console.log(`Successfully inserted ${result.insertedIds.length} items!`);
  })
  .catch(err => console.error(`Failed to insert documents: ${err}`))

To call the collection.insertMany() action from the Java/Android SDK, use the RemoteMongoCollection.insertMany() method.

Document doc1 = new Document()
    .append("name", "basketball")
    .append("category", "sports")
    .append("quantity", 20)
    .append("reviews", Arrays.asList());

Document doc2 = new Document()
    .append("name", "football")
    .append("category", "sports")
    .append("quantity", 30)
    .append("reviews", Arrays.asList());

List<Document> docs = Arrays.asList(doc1, doc2);

final Task <RemoteInsertManyResult> insertTask = itemsCollection.insertMany(docs);
insertTask.addOnCompleteListener(new OnCompleteListener <RemoteInsertManyResult> () {
    @Override
    public void onComplete(@NonNull Task <RemoteInsertManyResult> task) {
        if (task.isSuccessful()) {
            Log.d("app",
                String.format("successfully inserted %d items with ids: %s",
                    task.getResult().getInsertedIds().size(),
                    task.getResult().getInsertedIds().toString()));
        } else {
            Log.e("app", "failed to inserts document with: ", task.getException());
        }
    }
});

To call the collection.insertMany() action from the Swift/iOS SDK, use the RemoteMongoCollection.insertMany() method.

// Insert two documents into the items collection
let doc1: Document = [
    "name": "basketball",
    "category": "sports",
    "quantity": 20,
    "reviews": []
];

let doc2: Document = [
    "name": "football",
    "category": "sports",
    "quantity": 30,
    "reviews": []
];

itemsCollection?.insertMany([doc1, doc2]) { result in
    switch result {
    case .success(let result):
        print("Successfully inserted items with _ids: \(result.insertedIds))");
    case .failure(let error):
        print("Failed to insert items: \(error)");
    }
}

Parameters

The collection.insertMany() action has the following form:

insertMany(documents)
Parameter Description

Insert Document

documents: Array<document>
An array of documents to insert into the collection.

The insertMany() method has the following form:

insertMany(documents)
Parameter Description

Insert Document

documents: Array<document>
An array of documents to insert into the collection.

The insertMany() method has the following form:

insertMany​(List<? extends DocumentT> documents)
Parameter Description

Insert Document

documents: Array<document>
An array of documents to insert into the collection.

The insertMany() method has the following form:

insertMany(documents)
Parameter Description

Insert Document

documents: Array<document>
An array of documents to insert into the collection.

Return Value

The collection.insertMany() action returns a Promise that resolves to a document that describes the insert operation.

Promise<result: document>

The RemoteMongoCollection.insertMany() method returns a Promise that resolves to a RemoteInsertManyResult object that describes the insert operation.

Promise<result: RemoteInsertManyResult>

The RemoteMongoCollection.insertMany() method returns a Task that resolves to a RemoteInsertManyResult object that describes the insert operation.

Task<RemoteInsertManyResult result>

The RemoteMongoCollection.insertMany() method returns a StitchResult enum that resolves to a RemoteInsertManyResult object that describes the insert operation.

enum StitchResult {
  case success(result: RemoteInsertManyResult)
  case failure(error: StitchError)
}
Value Description

Inserted IDs

result.insertedIds: Array<ObjectID>
result.insertedIds: Document<Number, ObjectID>
result.insertedIds: Map<Long, ObjectID>
result.insertedIds: Document<Int64, ObjectID>

An array that contains the _id values for all documents that the insert operation added to the collection in the order that they were passed to the action.

A document that contains the _id values for all documents that the insert operation added to the collection. Each field name is an integer that matches the index of the document that the _id represents.

A Map that contains the _id values for all documents that the insert operation added to the collection. Each key is an integer that matches the index of the document that the _id represents.

A document that contains the _id values for all documents that the insert operation added to the collection. Each field name is an integer that matches the index of the document that the _id represents.