diff --git a/lib/collection.js b/lib/collection.js index 1ac6c50221a..17ce105448a 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -422,11 +422,6 @@ Collection.prototype.find = function(query, options, callback) { const cursor = this.s.topology.cursor(this.s.namespace, findCommand, newOptions); - // automatically call map on the cursor if the map option is set - if (typeof this.s.options.map === 'function') { - cursor.map(this.s.options.map); - } - return typeof callback === 'function' ? handleCallback(callback, null, cursor) : cursor; }; @@ -756,10 +751,6 @@ Collection.prototype.replaceOne = function(filter, doc, options, callback) { options.ignoreUndefined = this.s.options.ignoreUndefined; } - if (typeof this.s.options.unmap === 'function') { - doc = this.s.options.unmap(doc); - } - return executeOperation(this.s.topology, replaceOne, [this, filter, doc, options, callback]); }; diff --git a/lib/db.js b/lib/db.js index 6b47c10d4ac..9f845341825 100644 --- a/lib/db.js +++ b/lib/db.js @@ -305,8 +305,6 @@ const collectionKeys = [ * @param {(ReadPreference|string)} [options.readPreference=null] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). * @param {boolean} [options.serializeFunctions=false] Serialize functions on any object. * @param {boolean} [options.strict=false] Returns an error if the collection does not exist - * @param {function} [options.map] Function to map documents returned in find, findOne, and findAndModify commands. - * @param {function} [options.unmap] Function to unmap documents passed to insertOne, insertMany, and replaceOne commands. * @param {object} [options.readConcern=null] Specify a read concern for the collection. (only MongoDB 3.2 or higher supported) * @param {object} [options.readConcern.level='local'] Specify a read concern level for the collection operations, one of [local|majority]. (only MongoDB 3.2 or higher supported) * @param {Db~collectionResultCallback} [callback] The collection result callback diff --git a/lib/operations/collection_ops.js b/lib/operations/collection_ops.js index 50b0d5c89a0..05f28ad377b 100644 --- a/lib/operations/collection_ops.js +++ b/lib/operations/collection_ops.js @@ -502,10 +502,6 @@ function findAndModify(coll, query, sort, doc, options, callback) { executeCommand(coll.s.db, queryObject, finalOptions, (err, result) => { if (err) return handleCallback(callback, err, null); - if (result && result.value && typeof coll.s.options.map === 'function') { - result.value = coll.s.options.map(result.value); - } - return handleCallback(callback, null, result); }); } @@ -1046,11 +1042,8 @@ function prepareDocs(coll, docs, options) { ? options.forceServerObjectId : coll.s.db.options.forceServerObjectId; - const unmap = typeof coll.s.options.unmap === 'function' ? coll.s.options.unmap : false; - // no need to modify the docs if server sets the ObjectId - // and unmap collection option is unset - if (forceServerObjectId === true && !unmap) { + if (forceServerObjectId === true) { return docs; } @@ -1059,7 +1052,7 @@ function prepareDocs(coll, docs, options) { doc._id = coll.s.pkFactory.createPk(); } - return unmap ? unmap(doc) : doc; + return doc; }); } diff --git a/test/functional/collection_mapping_tests.js b/test/functional/collection_mapping_tests.js deleted file mode 100644 index 6ee957f8622..00000000000 --- a/test/functional/collection_mapping_tests.js +++ /dev/null @@ -1,365 +0,0 @@ -'use strict'; -const ObjectID = require('bson').ObjectID; -const expect = require('chai').expect; -const setupDatabase = require('./shared').setupDatabase; - -class User { - constructor(doc) { - doc = doc || {}; - - this._id = doc._id || new ObjectID(); - this.firstName = doc.firstName; - this.lastName = doc.lastName; - } - - getFullName() { - return `${this.firstName} ${this.lastName}`; - } - - static map(doc) { - return new User(doc); - } - - static unmap(user) { - return { - _id: user._id, - firstName: user.firstName, - lastName: user.lastName, - internalField: true - }; - } -} - -describe('Collection Mapping', function() { - before(function() { - return setupDatabase(this.configuration); - }); - - it('should map find', { - metadata: { - requires: { topology: ['single'] } - }, - - // The actual test we wish to run - test: function(done) { - const configuration = this.configuration; - const ObjectID = configuration.require.ObjectID; - - const client = configuration.newClient(configuration.writeConcernMax(), { - poolSize: 1 - }); - - client.connect(function(err, client) { - expect(err).to.be.null; - const db = client.db(configuration.db); - - const collection = db.collection('collection_mapping_find', { - map: User.map - }); - - const docs = [ - { - _id: new ObjectID(), - firstName: 'John', - lastName: 'Doe' - }, - { - _id: new ObjectID(), - firstName: 'Mongo', - lastName: 'DB' - } - ]; - - collection.insertMany(docs, configuration.writeConcernMax(), function(err) { - expect(err).to.be.null; - - collection - .find({}) - .sort({ firstName: 1 }) - .toArray(function(err, users) { - expect(err).to.be.null; - expect(users[0]).to.be.an.instanceof(User); - expect(users[0].firstName).to.equal('John'); - expect(users[0].lastName).to.equal('Doe'); - expect(users[0].getFullName()).to.equal('John Doe'); - client.close(); - done(); - }); - }); - }); - } - }); - - it('should map findOne', { - metadata: { - requires: { topology: ['single'] } - }, - - // The actual test we wish to run - test: function(done) { - const configuration = this.configuration; - const ObjectID = configuration.require.ObjectID; - - const client = configuration.newClient(configuration.writeConcernMax(), { - poolSize: 1 - }); - - client.connect(function(err, client) { - const db = client.db(configuration.db); - expect(err).to.be.null; - - const collection = db.collection('collection_mapping_findOne', { - map: User.map - }); - - const doc = { - _id: new ObjectID(), - firstName: 'John', - lastName: 'Doe' - }; - - //insert new user - collection.insertOne(doc, configuration.writeConcernMax(), function(err) { - expect(err).to.be.null; - - collection.findOne({}, function(err, user) { - expect(err).to.be.null; - expect(user).to.be.an.instanceof(User); - expect(user.getFullName()).to.equal('John Doe'); - client.close(); - done(); - }); - }); - }); - } - }); - - it('should map findAndModify commands', { - metadata: { - requires: { topology: ['single'] } - }, - - // The actual test we wish to run - test: function(done) { - const configuration = this.configuration; - - const client = configuration.newClient(configuration.writeConcernMax(), { - poolSize: 1 - }); - - client.connect(function(err, client) { - const db = client.db(configuration.db); - expect(err).to.be.null; - - const collection = db.collection('collection_mapping_findAndModify', { - map: User.map - }); - - const doc = { firstName: 'John', lastName: 'Doe' }; - - collection.insertOne(doc, configuration.writeConcernMax(), function(err) { - expect(err).to.be.null; - - const opts = { upsert: true, returnOriginal: false }; - - collection.findOneAndUpdate({}, { $set: { firstName: 'Johnny' } }, opts, function( - err, - result - ) { - expect(err).to.be.null; - expect(result.value).to.be.an.instanceof(User); - expect(result.value.getFullName()).to.equal('Johnny Doe'); - - // Execute findOneAndReplace - collection.findOneAndReplace( - {}, - { firstName: 'Johnny Boy', lastName: 'Doey' }, - opts, - function(err, result) { - expect(err).to.be.null; - expect(result.value).to.be.an.instanceof(User); - expect(result.value.getFullName()).to.equal('Johnny Boy Doey'); - - // Execute findOneAndReplace - collection.findOneAndDelete({}, function(err, result) { - expect(err).to.be.null; - expect(result.value).to.be.an.instanceof(User); - expect(result.value.getFullName()).to.equal('Johnny Boy Doey'); - - client.close(); - done(); - }); - } - ); - }); - }); - }); - } - }); - - it('should unmap insertOne', { - metadata: { - requires: { topology: ['single'] } - }, - - // The actual test we wish to run - test: function(done) { - const configuration = this.configuration; - - const client = configuration.newClient(configuration.writeConcernMax(), { - poolSize: 1 - }); - - client.connect(function(err, client) { - const db = client.db(configuration.db); - expect(err).to.be.null; - - const collection = db.collection('collection_mapping_insertOne', { - unmap: User.unmap - }); - - const user = new User(); - user.firstName = 'John'; - user.lastName = 'Doe'; - - collection.insertOne(user, function(err) { - expect(err).to.be.null; - - collection.findOne({}, function(err, doc) { - expect(err).to.be.null; - - expect(doc).to.deep.equal({ - _id: user._id, - firstName: 'John', - lastName: 'Doe', - internalField: true - }); - - client.close(); - done(); - }); - }); - }); - } - }); - - it('should unmap insertMany', { - metadata: { - requires: { topology: ['single'] } - }, - - // The actual test we wish to run - test: function(done) { - const configuration = this.configuration; - - const client = configuration.newClient(configuration.writeConcernMax(), { - poolSize: 1 - }); - - client.connect(function(err, client) { - const db = client.db(configuration.db); - expect(err).to.be.null; - - const collection = db.collection('collection_mapping_insertMany', { - unmap: User.unmap - }); - - const daenerys = new User(); - daenerys.firstName = 'Daenerys'; - daenerys.lastName = 'Targaryen'; - - const jon = new User(); - jon.firstName = 'Jon'; - jon.lastName = 'Snow'; - - collection.insertMany([daenerys, jon], function(err) { - expect(err).to.be.null; - - collection - .find({}) - .sort({ firstName: 1 }) - .toArray(function(err, docs) { - expect(err).to.be.null; - - expect(docs).to.deep.equal([ - { - _id: daenerys._id, - firstName: 'Daenerys', - lastName: 'Targaryen', - internalField: true - }, - { - _id: jon._id, - firstName: 'Jon', - lastName: 'Snow', - internalField: true - } - ]); - - client.close(); - - done(); - }); - }); - }); - } - }); - - it('should unmap replaceOne', { - metadata: { - requires: { topology: ['single'] } - }, - - // The actual test we wish to run - test: function(done) { - const configuration = this.configuration; - - const client = configuration.newClient(configuration.writeConcernMax(), { - poolSize: 1 - }); - - client.connect(function(err, client) { - const db = client.db(configuration.db); - expect(err).to.be.null; - - const collection = db.collection('collection_mapping_replaceOne', { - unmap: User.unmap - }); - - const unmappedCollection = db.collection('collection_mapping_replaceOne'); - - const doc = { - _id: new ObjectID(), - firstName: 'John', - lastName: 'Doe' - }; - - unmappedCollection.insertOne(doc, function(err) { - expect(err).to.be.null; - - const user = new User(doc); - user.firstName = 'Johnny'; - user.lastName = 'Doey'; - - collection.replaceOne({}, user, function(err) { - expect(err).to.be.null; - - collection.findOne({}, function(err, doc) { - expect(err).to.be.null; - - expect(doc).to.deep.equal({ - _id: user._id, - firstName: 'Johnny', - lastName: 'Doey', - internalField: true - }); - - client.close(); - done(); - }); - }); - }); - }); - } - }); -});