10 examples of 'mongoose bulkwrite' in JavaScript

Every line of 'mongoose bulkwrite' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
371async createBulk(model, data, options) {
372 if (data.length > 1000) {
373 const chunks = [];
374 let i = 0;
375 while (i < data.length) {
376 chunks.push(data.slice(i, i + 1000));
377 i += 1000;
378 }
379 const ids_all = [];
380 for (const chunk of chunks) {
381 [].push.apply(ids_all, await this.createBulk(model, chunk, options));
382 }
383 return ids_all;
384 }
385 let result;
386 try {
387 result = (await this._collection(model).insertMany(data, { safe: true }));
388 }
389 catch (error) {
390 throw _processSaveError(error);
391 }
392 let error;
393 const ids = result.ops.map((doc) => {
394 const id = _objectIdToString(doc._id);
395 if (id) {
396 delete doc._id;
397 }
398 else {
399 error = new Error('unexpected result');
400 }
401 return id;
402 });
403 if (error) {
404 throw error;
405 }
406 else {
407 return ids;
408 }
409}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

377public async createBulk(model: string, data: any[], options: { transaction?: Transaction }) {
378 if (data.length > 1000) {
379 const chunks = [];
380 let i = 0;
381 while (i < data.length) {
382 chunks.push(data.slice(i, i + 1000));
383 i += 1000;
384 }
385 const ids_all: any = [];
386 for (const chunk of chunks) {
387 [].push.apply(ids_all, await this.createBulk(model, chunk, options));
388 }
389 return ids_all;
390 }
391 let result: any;
392 try {
393 result = (await this._collection(model).insertMany(data, { safe: true }));
394 } catch (error) {
395 throw _processSaveError(error);
396 }
397 let error;
398 const ids = result.ops.map((doc: any) => {
399 const id = _objectIdToString(doc._id);
400 if (id) {
401 delete doc._id;
402 } else {
403 error = new Error('unexpected result');
404 }
405 return id;
406 });
407 if (error) {
408 throw error;
409 } else {
410 return ids;
411 }
412}
132execute(_writeConcern, options, callback) {
133 const ret = this.bulkExecute(_writeConcern, options, callback);
134 if (!ret || isPromiseLike(ret)) {
135 return ret;
136 }
137
138 options = ret.options;
139 callback = ret.callback;
140
141 return executeOperation(this.s.topology, executeBatches, [this, options, callback]);
142}
44bulkAdd(data) {
45 if (data.length === 0) {
46 return
47 }
48
49 const insertStatement = this.db.prepare(
50 `INSERT INTO ${this.table} ` +
51 '(sequence, id, serialized) VALUES ' +
52 '(:sequence, :id, :serialized);',
53 )
54
55 try {
56 this.db.prepare('BEGIN;').run()
57
58 data.forEach(d =>
59 insertStatement.run({
60 sequence: d.sequence,
61 id: d.transaction.id,
62 serialized: Buffer.from(d.transaction.serialized, 'hex'),
63 }),
64 )
65
66 this.db.prepare('COMMIT;').run()
67 } finally {
68 if (this.db.inTransaction) {
69 this.db.prepare('ROLLBACK;').run()
70 }
71 }
72}
62insert(doc) {
63 const cmd = this.ensureCommand('insert', 'documents');
64
65 doc._id = doc._id || oid();
66 cmd.documents.push(doc);
67}
278insertMany(docs: ObjectLiteral[], options?: CollectionInsertManyOptions): Promise<InsertWriteOpResult> {
279 return this.manager.insertMany(this.metadata.tableName, docs, options);
280}
224function testBulkOperations() {
225 let dataStore: Backendless.DataStore = Backendless.Persistence.of('str');
226
227 let resultPromiseListOfString: Promise<Array<string>>;
228 let resultListOfString: Array<string>;
229
230 let resultPromiseString: Promise<string>;
231 let resultString: string;
232
233 resultPromiseListOfString = dataStore.bulkCreate([{}, {}, {}]);
234 resultListOfString = dataStore.bulkCreateSync([{}, {}, {}]);
235
236 resultPromiseString = dataStore.bulkUpdate('where clause string', {foo: 'bar'});
237 resultString = dataStore.bulkUpdateSync('where clause string', {foo: 'bar'});
238
239 resultPromiseString = dataStore.bulkDelete('where clause string');
240 resultPromiseString = dataStore.bulkDelete(['objectId1', 'objectId2', 'objectId3']);
241 resultPromiseString = dataStore.bulkDelete([{objectId: 'objectId1'}]);
242 resultPromiseString = dataStore.bulkDelete([{objectId: 'objectId1', foo: 'bar'}]);
243
244 resultString = dataStore.bulkDeleteSync('where clause string');
245 resultString = dataStore.bulkDeleteSync(['objectId1', 'objectId2', 'objectId3']);
246 resultString = dataStore.bulkDeleteSync([{objectId: 'objectId1'}]);
247 resultString = dataStore.bulkDeleteSync([{objectId: 'objectId1', foo: 'bar'}]);
248}
177bulkDocsDB(docs, options) {
178 return this.getDB().then(db => db.bulkDocs(docs, options))
179}
51async replaceMany(entities: EntityWithPartialId<T>[], upsert: boolean): Promise<T[]> {
52 const operations = entities.map((entity) => toReplaceOneOperation(entity, upsert));
53 const bulkWriteResult = await this.collection.bulkWrite(operations);
54 const upsertedIds = bulkWriteResult.upsertedIds as IdsMap;
55 const savedEntities = entities.map((entity, index) => {
56 const entityCopy = { ...entity };
57
58 const hasUpsertedId = upsertedIds.hasOwnProperty(index);
59 if (hasUpsertedId) {
60 entityCopy.id = objectIdOrStringToString(upsertedIds[index]._id);
61 }
62
63 return entityCopy as T;
64 });
65
66 return savedEntities;
67}
211public async bulkSet (objects: T[]) {
212 const batch = this.context.firestore.batch()
213
214 objects.forEach((obj) => {
215 const docId = obj.id
216 const setDoc = this._toDoc(obj)
217 batch.set(this.collectionRef.doc(docId), setDoc)
218 })
219 return batch.commit()
220}

Related snippets