10 examples of 'mongoose upsert' in JavaScript

Every line of 'mongoose upsert' 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
30async update(
31 entry: IdentifyEntry,
32 fields: string[] = this.defaultQueryFields
33): Promise<T> {
34 const instance = await this.model
35 .findOneAndUpdate(
36 { _id: entry.id },
37 { $set: entry },
38 { upsert: true, fields: this.getFields(fields), new: true }
39 )
40 .exec();
41 return instance;
42}
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

139upsert() {
140 this.cmds.upsert(true);
141
142 return this;
143}
94findOneAndUpdate(collection, query, values, options) {
95 var that = this;
96 query = castQueryIds(query);
97 if (!options) {
98 options = {};
99 }
100
101 // Always return the updated object
102 options.returnOriginal = false;
103
104 return new Promise(function(resolve, reject) {
105 var db = that._mongo.collection(collection);
106
107 var update = values;
108 if (options.upsert) {
109 update = { $setOnInsert: update };
110 } else {
111 update = { $set: update };
112 }
113
114 db.findOneAndUpdate(query, update, options, function(error, result) {
115 if (error) return reject(error);
116 resolve(result.value);
117 });
118 });
119}
312upsert (documents) {
313 return writeOp.call(this, 'upsert', arguments, documents)
314}
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}
278insertMany(docs: ObjectLiteral[], options?: CollectionInsertManyOptions): Promise<InsertWriteOpResult> {
279 return this.manager.insertMany(this.metadata.tableName, docs, options);
280}
216afterUpsert({ doc, organizationSourceIds }, callback) {
217 callback();
218}
128updateOne(query, update) {
129 return this._mongoCollection.updateOne(query, update);
130}
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 }
18update (query, data) {
19 return this.table.updateOne(query, {
20 $set: data
21 });
22}

Related snippets