Add multiple documents to a collection

Asked
Active3 hr before
Viewed126 times

6 Answers

multipledocumentscollection
90%

My objective is to add multiple docs(objects) to a collection at once, Currently, I have this:, Stack Overflow Public questions & answers ,Thanks for contributing an answer to Stack Overflow!,This triggers an update on other devices for each update made to the collection

Example_snippet/controller/utility/_multiple.js/ var db = firebase.firestore();. . .
var db = firebase.firestore();
var batch = db.batch()
Step 2 continued with array.forEach((doc) => { var. . .
array.forEach((doc) => {
   var docRef = db.collection("col").doc(); //automatically generate unique id
   batch.set(docRef, doc);
});
Step 3 continued with batch.commit() . . .
batch.commit()
load more v
88%

In this example, we are inserting multiple documents in the student collection with _id field using db,collection

Example_snippet/controller/utility/_multiple.js/ db.collection.insertMany( [. . .
db.collection.insertMany(
   [  <Document1>, <Document2>, <Document3> ... ],
   {
      writeConcern: <Document>,
      order: <Boolean> 
   }
)
load more v
72%

In this Node,js Mongoose Tutorial, we have learnt to insert multiple documents to MongoDB using Mongoose

Example_snippet/controller/utility/_documents.js/ Model.collection.insert(docs, . . .
Model.collection.insert(docs, options, callback)
load more v
65%

You can insert multiple documents into an existing collection in MongoDB using the insertMany() method,,How to update an existing document in MongoDB collection using Java?,How to delete document from a collection in MongoDB using deleteOne() method?,How to insert new documents into a MongoDB collection in your database?

Example_snippet/controller/utility/_documents.js/ db.coll.insert(docArray). . .
db.coll.insert(docArray)
load more v
75%

To retrieve the document that you just inserted, query the collection:,Insert DocumentsInsert Methods,This also applies to documents inserted through update operations with upsert: true,,If the collection does not currently exist, insert operations will create the collection

Example_snippet/controller/utility/_documents.js/ _id. . .
_id
load more v
40%

Example_snippet/controller/utility/_documents.js/ var db = firebase.firestore();. . .
var db = firebase.firestore();
var batch = db.batch()

save(docs: any[]) {
   docs.forEach((doc) => {
      var docRef = db.collection("col").doc(); //automatically generate unique id
      batch.set(docRef, doc);
   });
   return batch.commit();
}