10 examples of 'findandupdate mongodb' in JavaScript

Every line of 'findandupdate mongodb' 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
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}
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

103function update ( data, callback ){
104
105 callback = callback || function(){};
106
107 player.findOneAndUpdate({'info.id': data.info.id}, data, function (error, foundPlayer) {
108
109 if (error){
110 console.log(error);
111 }
112
113 return callback( foundPlayer );
114
115 //console.log(foundPlayer);
116 });
117}
25async findOneAndUpdate(id, data) {
26 const query = {_id: ObjectId(id)};
27 const modifier = {$set: data};
28 const options = {returnOriginal: false};
29 const operation = await this.db
30 .collection(this.name)
31 .findOneAndUpdate(query, modifier, options);
32
33 if (!operation.value) {
34 throw new Error('Db findOneAndUpdate error');
35 }
36 return operation.value;
37}
128updateOne(query, update) {
129 return this._mongoCollection.updateOne(query, update);
130}
180findOneAndUpdate(table, options, sets, where, callback) {
181 try {
182 checkParams({table, options, sets, where, callback})
183 } catch (err) {
184 return callback(err)
185 }
186 let _options = _.cloneDeep(options)
187 _options.castIds = false
188 this._conn.collection(table).findOneAndUpdate(where, sets, _options,
189 (err, updated_doc) => {
190 return callback(err, updated_doc, null)
191 })
192}
127async findOne(collectionName: string, query, fields = {}, options = {}): Promise<IResult> {
128 const collection = await this.getCollenction(collectionName);
129 options = Object.assign({}, options, { projection: fields });
130 return await collection.findOne(query, options);
131}
58public findOne(conditions: any) {
59 return (RoomCache.findOne(conditions, {
60 _id: 0,
61 locked: 1,
62 processId: 1,
63 roomId: 1,
64 })) as any as QueryHelpers<RoomListingData>;
65}
82findOne(collection, query) {
83 var that = this;
84 query = castQueryIds(query);
85 return new Promise(function(resolve, reject) {
86 var db = that._mongo.collection(collection);
87 db.findOne(query, function (error, doc) {
88 if (error) return reject(error);
89 return resolve(doc);
90 });
91 });
92}
150findOne(collection) {
151 return new MongoDBOperation('findOne', collection, this, (collection, args, cb) => {
152 collection.findOne(args.query || {}, args.projection || {}, (err, item) => {
153 if (err) return cb(err);
154 if (!item) return cb(new Error("Not found"));
155 cb(null, item);
156 });
157 });
158}
18update (query, data) {
19 return this.table.updateOne(query, {
20 $set: data
21 });
22}

Related snippets