NodeJS, Mongoose - findOneAndUpdate not working from Server
12,694
findOneAndUpdate
is an asynchronous function. You need call a callback. In this callback you recive the results.
Voucher.findOneAndUpdate(
{"lease":"71610b3209bb37a59c90b20c36d3bb34"},
{
$set: {'value':92}
},
{
returnNewDocument: true
}
, function( error, result){
// In this moment, you recive a result object or error
// ... Your code when have result ... //
});
More info in mongoose website documentation for findOneAndUpdate
Related videos on Youtube
Author by
LavenPillay
Updated on May 26, 2022Comments
-
LavenPillay almost 2 years
I'm new to NodeJS and MongoDB and Mongoose.
This works when I run it in a MongoDB client (see screenshot).
Question : What do I need to do differently to make the update work from the server-side code ?
db.getCollection('vouchers').findOneAndUpdate( {"lease":"71610b3209bb37a59c90b20c36d3bb34"}, { $set: {'value':93} }, { returnNewDocument: true } )
However when I run the exact same code from my NodeJS server, using Mongoose, it does not perform the update.
var result = Voucher.findOneAndUpdate( {"lease":"71610b3209bb37a59c90b20c36d3bb34"}, { $set: {'value':92} }, { returnNewDocument: true } );
The result object looks like :
Query { _mongooseOptions: {}, mongooseCollection: NativeCollection { collection: Collection { s: [Object] }, opts: { bufferCommands: true, capped: false }, name: 'vouchers', collectionName: 'vouchers', conn: NativeConnection { base: [Object], collections: [Object], models: [Object], config: [Object], replica: false, hosts: null, host: 'localhost', port: 27017, user: undefined, pass: undefined, name: 'ellispark_db', options: [Object], otherDbs: [], states: [Object], _readyState: 1, _closeCalled: false, _hasOpened: true, _listening: false, db: [Object] }, queue: [], buffer: false, emitter: EventEmitter { domain: null, _events: {}, _eventsCount: 0, _maxListeners: undefined } }, model: { [Function: model] hooks: Kareem { _pres: {}, _posts: {} }, base: Mongoose { connections: [Object], models: [Object], modelSchemas: [Object], options: [Object], plugins: [Object] }, modelName: 'Voucher', model: [Function: model], db: NativeConnection { base: [Object], collections: [Object], models: [Object], config: [Object], replica: false, hosts: null, host: 'localhost', port: 27017, user: undefined, pass: undefined, name: 'ellispark_db', options: [Object], otherDbs: [], states: [Object], _readyState: 1, _closeCalled: false, _hasOpened: true, _listening: false, db: [Object] }, discriminators: undefined, '$appliedHooks': true, _events: { init: [Function], save: [Function] }, _eventsCount: 2, schema: Schema { obj: [Object], paths: [Object], aliases: {}, subpaths: {}, virtuals: [Object], singleNestedPaths: {}, nested: {}, inherits: {}, callQueue: [Object], _indexes: [], methods: {}, statics: {}, tree: [Object], query: {}, childSchemas: [], plugins: [Object], s: [Object], options: [Object], '$globalPluginsApplied': true }, collection: NativeCollection { collection: [Object], opts: [Object], name: 'vouchers', collectionName: 'vouchers', conn: [Object], queue: [], buffer: false, emitter: [Object] }, Query: { [Function] base: [Object] }, '$__insertMany': [Function], insertMany: [Function] }, schema: Schema { obj: { msisdn: [Object], name: [Object], lease: [Object], description: [Object], bcodeRef: [Object], smsRef: [Object], subscriberRef: [Object], value: [Object] }, paths: { msisdn: [Object], name: [Object], lease: [Object], description: [Object], bcodeRef: [Object], smsRef: [Object], subscriberRef: [Object], value: [Object], _id: [Object], __v: [Object] }, aliases: {}, subpaths: {}, virtuals: { id: [Object] }, singleNestedPaths: {}, nested: {}, inherits: {}, callQueue: [ [Object], [Object], [Object], [Object], [Object], [Object] ], _indexes: [], methods: {}, statics: {}, tree: { msisdn: [Object], name: [Object], lease: [Object], description: [Object], bcodeRef: [Object], smsRef: [Object], subscriberRef: [Object], value: [Object], _id: [Object], __v: [Function: Number], id: [Object] }, query: {}, childSchemas: [], plugins: [ [Object], [Object], [Object], [Object] ], s: { hooks: [Object], kareemHooks: [Object] }, options: { retainKeyOrder: false, typeKey: 'type', id: true, noVirtualId: false, _id: true, noId: false, validateBeforeSave: true, read: null, shardKey: null, autoIndex: null, minimize: true, discriminatorKey: '__t', versionKey: '__v', capped: false, bufferCommands: true, strict: true, pluralization: true }, '$globalPluginsApplied': true }, op: 'findOneAndUpdate', options: { retainKeyOrder: false, returnNewDocument: true }, _conditions: { lease: '71610b3209bb37a59c90b20c36d3bb34' }, _fields: undefined, _update: { '$set': { value: 92 } }, _path: undefined, _distinct: undefined, _collection: NodeCollection { collection: NativeCollection { collection: [Object], opts: [Object], name: 'vouchers', collectionName: 'vouchers', conn: [Object], queue: [], buffer: false, emitter: [Object] }, collectionName: 'vouchers' }, _traceFunction: undefined, _count: [Function], _execUpdate: [Function], _find: [Function], _findOne: [Function], _findOneAndRemove: [Function], _findOneAndUpdate: [Function], _replaceOne: [Function], _updateMany: [Function], _updateOne: [Function] }
-
LavenPillay over 6 yearsfacepalm I found the answer in those docs just a few minutes after posting. Thanks a lot !