diff --git a/lib/mongodb.js b/lib/mongodb.js index f90ae37a7..296fe5626 100644 --- a/lib/mongodb.js +++ b/lib/mongodb.js @@ -508,7 +508,7 @@ MongoDB.prototype.create = function(model, data, options, callback) { data = self.toDatabase(model, data); - this.execute(model, 'insert', data, {safe: true}, function(err, result) { + this.execute(model, 'insertOne', data, {safe: true}, function(err, result) { if (self.debug) { debug('create.callback', model, err, result); } @@ -550,7 +550,7 @@ MongoDB.prototype.save = function(model, data, options, callback) { data = self.toDatabase(model, data); - this.execute(model, 'save', data, {}, function(err, result) { + this.execute(model, 'updateOne', {_id: oid}, {$set: data}, {upsert: true}, function(err, result) { if (!err) { self.setIdValue(model, data, idValue); if (idName !== '_id') { @@ -736,13 +736,16 @@ MongoDB.prototype.updateOrCreate = function updateOrCreate( this.execute( model, - 'findAndModify', + 'findOneAndUpdate', { _id: oid, }, - [['_id', 'asc']], data, - {upsert: true, new: true}, + { + upsert: true, + returnOriginal: false, + sort: [['_id', 'asc']], + }, function(err, result) { if (self.debug) { debug('updateOrCreate.callback', model, id, err, result); @@ -804,7 +807,7 @@ MongoDB.prototype.destroy = function destroy(model, id, options, callback) { debug('delete', model, id); } id = self.coerceId(model, id); - this.execute(model, 'remove', {_id: id}, function(err, result) { + this.execute(model, 'deleteOne', {_id: id}, function(err, result) { if (self.debug) { debug('delete.callback', model, id, err, result); } @@ -1319,7 +1322,7 @@ MongoDB.prototype.destroyAll = function destroyAll( } where = self.buildWhere(model, where, options); - this.execute(model, 'remove', where || {}, function(err, info) { + this.execute(model, 'deleteMany', where || {}, function(err, info) { if (err) return callback && callback(err); if (self.debug) debug('destroyAll.callback', model, where, err, info); @@ -1394,7 +1397,7 @@ MongoDB.prototype.replaceWithOptions = function(model, id, data, options, cb) { var self = this; var idName = self.idName(model); delete data[idName]; - this.execute(model, 'update', {_id: id}, data, options, function( + this.execute(model, 'replaceOne', {_id: id}, data, options, function( err, info ) { @@ -1464,11 +1467,14 @@ MongoDB.prototype.updateAttributes = function updateAttrs( this.execute( model, - 'findAndModify', - {_id: oid}, - [['_id', 'asc']], + 'findOneAndUpdate', + { + _id: oid, + }, data, - {}, + { + sort: [['_id', 'asc']], + }, function(err, result) { if (self.debug) { debug('updateAttributes.callback', model, id, err, result); @@ -1526,10 +1532,10 @@ MongoDB.prototype.update = MongoDB.prototype.updateAll = function updateAll( this.execute( model, - 'update', + 'updateMany', where, data, - {multi: true, upsert: false}, + {upsert: false}, function(err, info) { if (err) return cb && cb(err); diff --git a/test/mongodb.test.js b/test/mongodb.test.js index 464306bde..e2423c78f 100644 --- a/test/mongodb.test.js +++ b/test/mongodb.test.js @@ -73,7 +73,7 @@ describe('lazyConnect', function() { ds.connector.execute( 'TestLazy', - 'insert', + 'insertOne', {value: 'test value'}, function(err, success) { if (err) { @@ -100,11 +100,11 @@ describe('lazyConnect', function() { ds.connector.execute( 'TestLazy', - 'insert', + 'insertOne', {value: 'test value'}, function(err, success) { if (err) done(err); - var id = success.insertedIds[0]; + var id = success.insertedId; ds.connector.should.have.property('db'); ds.connector.db.should.have.property('topology'); ds.connector.db.topology.should.have.property('isDestroyed'); @@ -805,8 +805,8 @@ describe('mongodb connector', function() { ) { Post.find(function(err, results) { events.should.eql([ - 'before execute insert', - 'after execute insert', + 'before execute insertOne', + 'after execute insertOne', 'before execute find', 'after execute find', ]);