10 examples of 'mongoose insertmany' in JavaScript

Every line of 'mongoose insertmany' 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
278insertMany(docs: ObjectLiteral[], options?: CollectionInsertManyOptions): Promise<InsertWriteOpResult> {
279 return this.manager.insertMany(this.metadata.tableName, docs, options);
280}
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

78Insert(collection, data) {
79 var self = this;
80
81 return new Promise(function (resolve, reject) {
82 var _collection = self.DB.collection(collection);
83 _collection.insert(data, function (err, result) {
84 if(err){
85 reject(err);
86 return;
87 }
88 resolve(result);
89 });
90 });
91 }
174insertMany(entities) {
175 return this.db.insert(entities);
176}
101UserSchema.pre<UserModel>("insertMany", async function preInsertMany(
102 _next,
103 docs
104) {
105 await Promise.all(
106 docs
107 .filter(doc => doc.password)
108 .map(async doc => {
109 const hash = await this.encryptPassword(doc.password);
110 doc.password = hash;
111 })
112 );
113});
89insert(options: IOptions): Promise<IMongoResult> {
90 let self:Mongo = this;
91 return new Promise((resolve, reject) => {
92 try {
93 self.db.collection(options.tbName).insert(options.data, options || {}, (err, docs) => {
94 err ? reject({
95 ok: -1,
96 es: err
97 }) : resolve({
98 ok: 1,
99 es: docs.result,
100 obj: docs
101 });
102 });
103 } catch (e) {
104 reject({
105 ok: -1,
106 es: e
107 });
108 }
109 });
110}
34insertOne(doc) {
35 return this.constructor._getCollection(this.collection)
36 .then(function(db) {
37 return new Promise((resolve, reject) => {
38 db.insert(doc, function (err, newDoc) {
39 if(err) {
40 reject(err);
41 }
42 else {
43 resolve(newDoc);
44 }
45 });
46 });
47 });
48 }
46insert(table, sets, callback) {
47 try {
48 checkParams({table, sets, callback})
49 } catch (err) {
50 return callback(err)
51 }
52 if (this._readonly) {
53 return errNotAllowd(callback)
54 }
55 this._conn.collection(table).insert(
56 _.cloneDeep(sets),
57 {castIds: false},
58 (err, result) => {
59 if (err) {
60 return callback(err)
61 }
62 if (result.constructor === Object) {
63 result = [result]
64 }
65 return callback(err, {
66 affected_rows: result.length,
67 docs: result,
68 }, null)
69 })
70}
45function insert (callback) {
46 if (typeof instance.name !== 'string' || instance.name.length === 0) {
47 return callback(new Error("Invalid name for the " + type));
48 }
49
50 if (typeof instance.position === 'undefined'){
51 if(type === 'list') {
52 wunderlist.database.getLastListPosition(function(err, position){
53 instance.position = position;
54 insertPhase2(callback);
55 });
56 } else if(type === 'task') {
57 wunderlist.database.getLastTaskPosition(instance.list_id, function(err, position){
58 instance.position = position;
59 insertPhase2(callback);
60 });
61 }
62 } else {
63 insertPhase2(callback);
64 }
65}
62insert(doc) {
63 const cmd = this.ensureCommand('insert', 'documents');
64
65 doc._id = doc._id || oid();
66 cmd.documents.push(doc);
67}
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}

Related snippets