diff --git a/src/collection.ts b/src/collection.ts index 6040e574eb..22b11b98a8 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -104,8 +104,6 @@ export interface CollectionOptions WriteConcernOptions, LoggerOptions { slaveOk?: boolean; - /** Returns an error if the collection does not exist */ - strict?: boolean; /** Specify a read concern for the collection. (only MongoDB 3.2 or higher supported) */ readConcern?: ReadConcernLike; /** The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST). */ diff --git a/src/db.ts b/src/db.ts index 8a198298ab..8a2034405d 100644 --- a/src/db.ts +++ b/src/db.ts @@ -311,77 +311,23 @@ export class Db { } /** - * Fetch a specific collection (containing the actual collection information). If the application does not use strict mode you - * can use it without a callback in the following way: `const collection = db.collection('mycollection');` + * Returns a reference to a MongoDB Collection. If it does not exist it will be created implicitly. * * @param name - the collection name we wish to access. - * @returns return the new Collection instance if not in strict mode + * @returns return the new Collection instance */ collection(name: string): Collection; collection( name: string, - options: CollectionOptions - ): Collection; - collection( - name: string, - callback: Callback> - ): void; - collection( - name: string, - options: CollectionOptions, - callback: Callback> - ): void; - collection( - name: string, - options?: CollectionOptions | Callback>, - callback?: Callback> - ): Collection | void { - if (typeof options === 'function') (callback = options), (options = {}); - const finalOptions = resolveOptions(this, options); - - // Execute - if (!finalOptions.strict) { - try { - const collection = new Collection(this, name, finalOptions); - if (callback) callback(undefined, collection); - return collection; - } catch (err) { - if (err instanceof MongoError && callback) return callback(err); - throw err; - } - } - - // Strict mode - if (typeof callback !== 'function') { - throw new MongoError( - `A callback is required in strict mode. While getting collection ${name}` - ); + options?: CollectionOptions + ): Collection { + if (!options) { + options = {}; + } else if (typeof options === 'function') { + throw new TypeError('The callback form of this helper has been removed.'); } - - // Did the user destroy the topology - if (getTopology(this).isDestroyed()) { - return callback(new MongoError('topology was destroyed')); - } - - const listCollectionOptions: ListCollectionsOptions = Object.assign({}, finalOptions, { - nameOnly: true - }); - - // Strict mode - this.listCollections({ name }, listCollectionOptions).toArray((err, collections) => { - if (callback == null) return; - if (err != null || !collections) return callback(err); - if (collections.length === 0) - return callback( - new MongoError(`Collection ${name} does not exist. Currently in strict mode.`) - ); - - try { - return callback(undefined, new Collection(this, name, finalOptions)); - } catch (err) { - return callback(err); - } - }); + const finalOptions = resolveOptions(this, options); + return new Collection(this, name, finalOptions); } /** diff --git a/test/functional/aggregation.test.js b/test/functional/aggregation.test.js index 648be6ebe9..776835aad1 100644 --- a/test/functional/aggregation.test.js +++ b/test/functional/aggregation.test.js @@ -806,37 +806,35 @@ describe('Aggregation', function () { expect(err).to.not.exist; var db = client.db(databaseName); - db.collection('te.st', function (err, col) { + const col = db.collection('te.st'); + var count = 0; + + col.insertMany([{ a: 1 }, { a: 1 }, { a: 1 }], function (err, r) { expect(err).to.not.exist; - var count = 0; + expect(r).property('insertedCount').to.equal(3); - col.insertMany([{ a: 1 }, { a: 1 }, { a: 1 }], function (err, r) { - expect(err).to.not.exist; - expect(r).property('insertedCount').to.equal(3); + const cursor = col.aggregate([{ $project: { a: 1 } }]); - const cursor = col.aggregate([{ $project: { a: 1 } }]); + cursor.toArray(function (err, docs) { + expect(err).to.not.exist; + expect(docs.length).to.be.greaterThan(0); - cursor.toArray(function (err, docs) { - expect(err).to.not.exist; - expect(docs.length).to.be.greaterThan(0); - - //Using cursor - KO - col - .aggregate([{ $project: { a: 1 } }], { - cursor: { batchSize: 10000 } - }) - .forEach( - function () { - count = count + 1; - }, - function (err) { - expect(err).to.not.exist; - expect(count).to.be.greaterThan(0); + //Using cursor - KO + col + .aggregate([{ $project: { a: 1 } }], { + cursor: { batchSize: 10000 } + }) + .forEach( + function () { + count = count + 1; + }, + function (err) { + expect(err).to.not.exist; + expect(count).to.be.greaterThan(0); - client.close(done); - } - ); - }); + client.close(done); + } + ); }); }); }); diff --git a/test/functional/collection.test.js b/test/functional/collection.test.js index f69f8aa235..cdb6b11507 100644 --- a/test/functional/collection.test.js +++ b/test/functional/collection.test.js @@ -83,35 +83,32 @@ describe('Collection', function () { db.createCollection('test.spiderman', () => { db.createCollection('test.mario', () => { // Insert test documents (creates collections) - db.collection('test.spiderman', (err, spiderman_collection) => { - spiderman_collection.insertOne({ foo: 5 }, configuration.writeConcernMax(), err => { + const spiderman_collection = db.collection('test.spiderman'); + spiderman_collection.insertOne({ foo: 5 }, configuration.writeConcernMax(), err => { + expect(err).to.not.exist; + const mario_collection = db.collection('test.mario'); + mario_collection.insertOne({ bar: 0 }, configuration.writeConcernMax(), err => { expect(err).to.not.exist; - db.collection('test.mario', (err, mario_collection) => { - mario_collection.insertOne({ bar: 0 }, configuration.writeConcernMax(), err => { - expect(err).to.not.exist; - // Assert collections - db.collections((err, collections) => { - expect(err).to.not.exist; + // Assert collections + db.collections((err, collections) => { + expect(err).to.not.exist; - let found_spiderman = false; - let found_mario = false; - let found_does_not_exist = false; - - collections.forEach(collection => { - if (collection.collectionName === 'test.spiderman') { - found_spiderman = true; - } - if (collection.collectionName === 'test.mario') found_mario = true; - if (collection.collectionName === 'does_not_exist') - found_does_not_exist = true; - }); + let found_spiderman = false; + let found_mario = false; + let found_does_not_exist = false; - expect(found_spiderman).to.be.true; - expect(found_mario).to.be.true; - expect(found_does_not_exist).to.be.false; - done(); - }); + collections.forEach(collection => { + if (collection.collectionName === 'test.spiderman') { + found_spiderman = true; + } + if (collection.collectionName === 'test.mario') found_mario = true; + if (collection.collectionName === 'does_not_exist') found_does_not_exist = true; }); + + expect(found_spiderman).to.be.true; + expect(found_mario).to.be.true; + expect(found_does_not_exist).to.be.false; + done(); }); }); }); @@ -166,23 +163,6 @@ describe('Collection', function () { }); }); - it('should ensure strict access collection', function (done) { - db.collection('does-not-exist', { strict: true }, err => { - expect(err).to.be.an.instanceof(Error); - expect(err.message).to.equal( - 'Collection does-not-exist does not exist. Currently in strict mode.' - ); - db.createCollection('test_strict_access_collection', err => { - expect(err).to.not.exist; - db.collection('test_strict_access_collection', configuration.writeConcernMax(), err => { - expect(err).to.not.exist; - // Let's close the db - done(); - }); - }); - }); - }); - it('should fail to insert due to illegal keys', function (done) { db.createCollection('test_invalid_key_names', (err, collection) => { // Legal inserts @@ -280,39 +260,25 @@ describe('Collection', function () { }); it('should fail due to illegal listCollections', function (done) { - db.collection(5, err => { - expect(err.message).to.equal('collection name must be a String'); - }); - - db.collection('', err => { - expect(err.message).to.equal('collection names cannot be empty'); - }); - - db.collection('te$t', err => { - expect(err.message).to.equal("collection names must not contain '$'"); - }); - - db.collection('.test', err => { - expect(err.message).to.equal("collection names must not start or end with '.'"); - }); - - db.collection('test.', err => { - expect(err.message).to.equal("collection names must not start or end with '.'"); - }); - - db.collection('test..t', err => { - expect(err.message).to.equal('collection names cannot be empty'); - done(); - }); + expect(() => db.collection(5)).to.throw('collection name must be a String'); + expect(() => db.collection('')).to.throw('collection names cannot be empty'); + expect(() => db.collection('te$t')).to.throw("collection names must not contain '$'"); + expect(() => db.collection('.test')).to.throw( + "collection names must not start or end with '.'" + ); + expect(() => db.collection('test.')).to.throw( + "collection names must not start or end with '.'" + ); + expect(() => db.collection('test..t')).to.throw('collection names cannot be empty'); + done(); }); it('should correctly count on non-existent collection', function (done) { - db.collection('test_multiple_insert_2', (err, collection) => { - collection.countDocuments((err, count) => { - expect(count).to.equal(0); - // Let's close the db - done(); - }); + const collection = db.collection('test_multiple_insert_2'); + collection.countDocuments((err, count) => { + expect(count).to.equal(0); + // Let's close the db + done(); }); }); @@ -351,22 +317,20 @@ describe('Collection', function () { }); it('should perform collection remove with no callback', function (done) { - db.collection('remove_with_no_callback_bug_test', (err, collection) => { + const collection = db.collection('remove_with_no_callback_bug_test'); + collection.insertOne({ a: 1 }, configuration.writeConcernMax(), err => { expect(err).to.not.exist; - collection.insertOne({ a: 1 }, configuration.writeConcernMax(), err => { + collection.insertOne({ b: 1 }, configuration.writeConcernMax(), err => { expect(err).to.not.exist; - collection.insertOne({ b: 1 }, configuration.writeConcernMax(), err => { + collection.insertOne({ c: 1 }, configuration.writeConcernMax(), err => { expect(err).to.not.exist; - collection.insertOne({ c: 1 }, configuration.writeConcernMax(), err => { + collection.remove({ a: 1 }, configuration.writeConcernMax(), err => { expect(err).to.not.exist; - collection.remove({ a: 1 }, configuration.writeConcernMax(), err => { + // Let's perform a count + collection.countDocuments((err, count) => { expect(err).to.not.exist; - // Let's perform a count - collection.countDocuments((err, count) => { - expect(err).to.not.exist; - expect(count).to.equal(2); - done(); - }); + expect(count).to.equal(2); + done(); }); }); }); diff --git a/test/functional/connection.test.js b/test/functional/connection.test.js index fd296114f6..724bd2d374 100644 --- a/test/functional/connection.test.js +++ b/test/functional/connection.test.js @@ -110,7 +110,7 @@ describe('Connection - functional', function () { expect(err).to.not.exist; var db = client.db(configuration.db); - db.collection(testName, function (err, collection) { + db.createCollection(testName, function (err, collection) { expect(err).to.not.exist; collection.insert({ foo: 123 }, { writeConcern: { w: 1 } }, function (err) { diff --git a/test/functional/cursor.test.js b/test/functional/cursor.test.js index 34c594b815..f55de943ae 100644 --- a/test/functional/cursor.test.js +++ b/test/functional/cursor.test.js @@ -4170,30 +4170,26 @@ describe('Cursor', function () { const findSort = (input, output) => withMonitoredClient('find', function (client, events, done) { const db = client.db('test'); - db.collection('test_sort_dos', (err, collection) => { + const collection = db.collection('test_sort_dos'); + const cursor = collection.find({}, { sort: input }); + cursor.next(err => { expect(err).to.not.exist; - const cursor = collection.find({}, { sort: input }); - cursor.next(err => { - expect(err).to.not.exist; - expect(events[0].command.sort).to.be.instanceOf(Map); - expect(Array.from(events[0].command.sort)).to.deep.equal(Array.from(output)); - cursor.close(done); - }); + expect(events[0].command.sort).to.be.instanceOf(Map); + expect(Array.from(events[0].command.sort)).to.deep.equal(Array.from(output)); + cursor.close(done); }); }); const cursorSort = (input, output) => withMonitoredClient('find', function (client, events, done) { const db = client.db('test'); - db.collection('test_sort_dos', (err, collection) => { + const collection = db.collection('test_sort_dos'); + const cursor = collection.find({}).sort(input); + cursor.next(err => { expect(err).to.not.exist; - const cursor = collection.find({}).sort(input); - cursor.next(err => { - expect(err).to.not.exist; - expect(events[0].command.sort).to.be.instanceOf(Map); - expect(Array.from(events[0].command.sort)).to.deep.equal(Array.from(output)); - cursor.close(done); - }); + expect(events[0].command.sort).to.be.instanceOf(Map); + expect(Array.from(events[0].command.sort)).to.deep.equal(Array.from(output)); + cursor.close(done); }); }); @@ -4287,16 +4283,14 @@ describe('Cursor', function () { metadata: { requires: { mongodb: '>=4.4' } }, test: withMonitoredClient('find', function (client, events, done) { const db = client.db('test'); - db.collection('test_sort_allow_disk_use', (err, collection) => { + const collection = db.collection('test_sort_allow_disk_use'); + const cursor = collection.find({}).sort(['alpha', 1]).allowDiskUse(); + cursor.next(err => { expect(err).to.not.exist; - const cursor = collection.find({}).sort(['alpha', 1]).allowDiskUse(); - cursor.next(err => { - expect(err).to.not.exist; - const { command } = events.shift(); - expect(command.sort).to.deep.equal(new Map([['alpha', 1]])); - expect(command.allowDiskUse).to.be.true; - cursor.close(done); - }); + const { command } = events.shift(); + expect(command.sort).to.deep.equal(new Map([['alpha', 1]])); + expect(command.allowDiskUse).to.be.true; + cursor.close(done); }); }) }); @@ -4305,13 +4299,11 @@ describe('Cursor', function () { metadata: { requires: { mongodb: '>=4.4' } }, test: withClient(function (client, done) { const db = client.db('test'); - db.collection('test_sort_allow_disk_use', (err, collection) => { - expect(err).to.not.exist; - expect(() => collection.find({}).allowDiskUse()).to.throw( - /allowDiskUse requires a sort specification/ - ); - done(); - }); + const collection = db.collection('test_sort_allow_disk_use'); + expect(() => collection.find({}).allowDiskUse()).to.throw( + /allowDiskUse requires a sort specification/ + ); + done(); }) }); }); diff --git a/test/functional/db.test.js b/test/functional/db.test.js index 6f4f9ee497..9ba9b28e32 100644 --- a/test/functional/db.test.js +++ b/test/functional/db.test.js @@ -33,68 +33,6 @@ describe('Db', function () { }) }); - it('should not call callback twice on collection() with callback', { - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: function (done) { - var configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); - client.connect(function (err, client) { - expect(err).to.not.exist; - var db = client.db(configuration.db); - var count = 0; - - var coll = db.collection('coll_name', function (err) { - expect(err).to.not.exist; - count = count + 1; - }); - - try { - coll.findOne({}, null, function () { - //e - errors b/c findOne needs a query selector - test.equal(1, count); - client.close(done); - }); - } catch (e) { - process.nextTick(function () { - test.equal(1, count); - client.close(done); - }); - } - }); - } - }); - - it('should callback with an error only when a MongoError', { - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: function (done) { - let configuration = this.configuration; - let client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); - - client.connect(function (err, client) { - let callbackCalled = 0; - expect(err).to.not.exist; - let db = client.db(configuration.db); - - try { - db.collection('collectionCallbackTest', function (err) { - callbackCalled++; - expect(err).to.not.exist; - throw new Error('Erroring on purpose with a non MongoError'); - }); - } catch (e) { - test.equal(callbackCalled, 1); - client.close(done); - } - }); - } - }); - it('shouldCorrectlyHandleFailedConnection', { metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } @@ -364,4 +302,14 @@ describe('Db', function () { }); } }); + + it( + 'should throw if Db.collection is passed a deprecated callback argument', + withClient((client, done) => { + expect(() => client.db('test').collection('test', () => {})).to.throw( + 'The callback form of this helper has been removed.' + ); + done(); + }) + ); }); diff --git a/test/functional/error.test.js b/test/functional/error.test.js index e30a75e27d..c34d80122d 100644 --- a/test/functional/error.test.js +++ b/test/functional/error.test.js @@ -21,29 +21,31 @@ describe('Errors', function () { it('should fail insert due to unique index', function (done) { const db = client.db(this.configuration.db); - const collection = db.collection('test_failing_insert_due_to_unique_index'); - collection.createIndexes( - [ - { - name: 'test_failing_insert_due_to_unique_index', - key: { a: 1 }, - unique: true - } - ], - { writeConcern: { w: 1 } }, - err => { - expect(err).to.not.exist; - - collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => { + db.createCollection('test_failing_insert_due_to_unique_index', (err, collection) => { + expect(err).to.not.exist; + collection.createIndexes( + [ + { + name: 'test_failing_insert_due_to_unique_index', + key: { a: 1 }, + unique: true + } + ], + { writeConcern: { w: 1 } }, + err => { expect(err).to.not.exist; collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => { - expect(err.code).to.equal(11000); - done(); + expect(err).to.not.exist; + + collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => { + expect(err.code).to.equal(11000); + done(); + }); }); - }); - } - ); + } + ); + }); }); it('should fail insert due to unique index strict', function (done) { @@ -51,29 +53,28 @@ describe('Errors', function () { db.dropCollection('test_failing_insert_due_to_unique_index_strict', () => { db.createCollection('test_failing_insert_due_to_unique_index_strict', err => { expect(err).to.not.exist; - db.collection('test_failing_insert_due_to_unique_index_strict', (err, collection) => { - collection.createIndexes( - [ - { - name: 'test_failing_insert_due_to_unique_index_strict', - key: { a: 1 }, - unique: true - } - ], - { writeConcern: { w: 1 } }, - err => { + const collection = db.collection('test_failing_insert_due_to_unique_index_strict'); + collection.createIndexes( + [ + { + name: 'test_failing_insert_due_to_unique_index_strict', + key: { a: 1 }, + unique: true + } + ], + { writeConcern: { w: 1 } }, + err => { + expect(err).to.not.exist; + collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => { expect(err).to.not.exist; - collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => { - expect(err).to.not.exist; - collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => { - expect(err.code).to.equal(11000); - done(); - }); + collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => { + expect(err.code).to.equal(11000); + done(); }); - } - ); - }); + }); + } + ); }); }); }); diff --git a/test/functional/find.test.js b/test/functional/find.test.js index f82fa7bde7..2cbadf20ce 100644 --- a/test/functional/find.test.js +++ b/test/functional/find.test.js @@ -25,31 +25,30 @@ describe('Find', function () { expect(err).to.not.exist; var db = client.db(configuration.db); - db.collection('test_find_simple', function (err, collection) { - const docs = [{ a: 2 }, { b: 3 }]; + const collection = db.collection('test_find_simple'); + const docs = [{ a: 2 }, { b: 3 }]; - // Insert some test documents - collection.insert(docs, configuration.writeConcernMax(), err => { + // Insert some test documents + collection.insert(docs, configuration.writeConcernMax(), err => { + expect(err).to.not.exist; + + // Ensure correct insertion testing via the cursor and the count function + collection.find().toArray(function (err, documents) { expect(err).to.not.exist; + test.equal(2, documents.length); - // Ensure correct insertion testing via the cursor and the count function - collection.find().toArray(function (err, documents) { + collection.count(function (err, count) { expect(err).to.not.exist; - test.equal(2, documents.length); + test.equal(2, count); - collection.count(function (err, count) { + // Fetch values by selection + collection.find({ a: docs[0].a }).toArray(function (err, documents) { expect(err).to.not.exist; - test.equal(2, count); - - // Fetch values by selection - collection.find({ a: docs[0].a }).toArray(function (err, documents) { - expect(err).to.not.exist; - test.equal(1, documents.length); - test.equal(docs[0].a, documents[0].a); - // Let's close the db - client.close(done); - }); + test.equal(1, documents.length); + test.equal(docs[0].a, documents[0].a); + // Let's close the db + client.close(done); }); }); }); @@ -73,27 +72,26 @@ describe('Find', function () { var db = client.db(configuration.db); db.createCollection('test_find_simple_chained', function (err) { expect(err).to.not.exist; - db.collection('test_find_simple_chained', function (err, collection) { - const docs = [{ a: 2 }, { b: 3 }]; + const collection = db.collection('test_find_simple_chained'); + const docs = [{ a: 2 }, { b: 3 }]; - // Insert some test documents - collection.insert(docs, configuration.writeConcernMax(), err => { - expect(err).to.not.exist; + // Insert some test documents + collection.insert(docs, configuration.writeConcernMax(), err => { + expect(err).to.not.exist; - // Ensure correct insertion testing via the cursor and the count function - collection.find().toArray(function (err, documents) { - test.equal(2, documents.length); + // Ensure correct insertion testing via the cursor and the count function + collection.find().toArray(function (err, documents) { + test.equal(2, documents.length); - collection.count(function (err, count) { - test.equal(2, count); + collection.count(function (err, count) { + test.equal(2, count); - // Fetch values by selection - collection.find({ a: docs[0].a }).toArray(function (err, documents) { - test.equal(1, documents.length); - test.equal(docs[0].a, documents[0].a); - // Let's close the db - client.close(done); - }); + // Fetch values by selection + collection.find({ a: docs[0].a }).toArray(function (err, documents) { + test.equal(1, documents.length); + test.equal(docs[0].a, documents[0].a); + // Let's close the db + client.close(done); }); }); }); @@ -213,115 +211,114 @@ describe('Find', function () { db.createCollection('test_find_sorting', function (err) { expect(err).to.not.exist; - db.collection('test_find_sorting', function (err, collection) { - // Insert some test documents - collection.insert( - [ - { a: 1, b: 2 }, - { a: 2, b: 1 }, - { a: 3, b: 2 }, - { a: 4, b: 1 } - ], - configuration.writeConcernMax(), - function (err) { - expect(err).to.not.exist; + const collection = db.collection('test_find_sorting'); + // Insert some test documents + collection.insert( + [ + { a: 1, b: 2 }, + { a: 2, b: 1 }, + { a: 3, b: 2 }, + { a: 4, b: 1 } + ], + configuration.writeConcernMax(), + function (err) { + expect(err).to.not.exist; - // Test sorting (ascending) - collection - .find({ a: { $lt: 10 } }, { sort: [['a', 1]] }) - .toArray(function (err, documents) { - test.equal(4, documents.length); - test.equal(1, documents[0].a); - test.equal(2, documents[1].a); - test.equal(3, documents[2].a); - test.equal(4, documents[3].a); - - // Test sorting (descending) - collection - .find({ a: { $lt: 10 } }, { sort: [['a', -1]] }) - .toArray(function (err, documents) { - test.equal(4, documents.length); - test.equal(4, documents[0].a); - test.equal(3, documents[1].a); - test.equal(2, documents[2].a); - test.equal(1, documents[3].a); - - // Test sorting (descending), sort is hash - collection - .find({ a: { $lt: 10 } }, { sort: { a: -1 } }) - .toArray(function (err, documents) { - test.equal(4, documents.length); - test.equal(4, documents[0].a); - test.equal(3, documents[1].a); - test.equal(2, documents[2].a); - test.equal(1, documents[3].a); - - // Sorting using array of names, assumes ascending order - collection - .find({ a: { $lt: 10 } }, { sort: ['a'] }) - .toArray(function (err, documents) { - test.equal(4, documents.length); - test.equal(1, documents[0].a); - test.equal(2, documents[1].a); - test.equal(3, documents[2].a); - test.equal(4, documents[3].a); - - // Sorting using single name, assumes ascending order - collection - .find({ a: { $lt: 10 } }, { sort: 'a' }) - .toArray(function (err, documents) { - test.equal(4, documents.length); - test.equal(1, documents[0].a); - test.equal(2, documents[1].a); - test.equal(3, documents[2].a); - test.equal(4, documents[3].a); - - // Sorting using single name, assumes ascending order, sort is hash - collection - .find({ a: { $lt: 10 } }, { sort: { a: 1 } }) - .toArray(function (err, documents) { - test.equal(4, documents.length); - test.equal(1, documents[0].a); - test.equal(2, documents[1].a); - test.equal(3, documents[2].a); - test.equal(4, documents[3].a); - - collection - .find({ a: { $lt: 10 } }, { sort: ['b', 'a'] }) - .toArray(function (err, documents) { - test.equal(4, documents.length); - test.equal(2, documents[0].a); - test.equal(4, documents[1].a); - test.equal(1, documents[2].a); - test.equal(3, documents[3].a); - - // Sorting using empty array, no order guarantee should not blow up - collection - .find({ a: { $lt: 10 } }, { sort: [] }) - .toArray(function (err, documents) { - test.equal(4, documents.length); - - /* NONACTUAL */ - // Sorting using ordered hash - collection - .find({ a: { $lt: 10 } }, { sort: { a: -1 } }) - .toArray(function (err, documents) { - // Fail test if not an error - test.equal(4, documents.length); - // Let's close the db - client.close(done); - }); - }); - }); - }); - }); - }); - }); - }); - }); - } - ); - }); + // Test sorting (ascending) + collection + .find({ a: { $lt: 10 } }, { sort: [['a', 1]] }) + .toArray(function (err, documents) { + test.equal(4, documents.length); + test.equal(1, documents[0].a); + test.equal(2, documents[1].a); + test.equal(3, documents[2].a); + test.equal(4, documents[3].a); + + // Test sorting (descending) + collection + .find({ a: { $lt: 10 } }, { sort: [['a', -1]] }) + .toArray(function (err, documents) { + test.equal(4, documents.length); + test.equal(4, documents[0].a); + test.equal(3, documents[1].a); + test.equal(2, documents[2].a); + test.equal(1, documents[3].a); + + // Test sorting (descending), sort is hash + collection + .find({ a: { $lt: 10 } }, { sort: { a: -1 } }) + .toArray(function (err, documents) { + test.equal(4, documents.length); + test.equal(4, documents[0].a); + test.equal(3, documents[1].a); + test.equal(2, documents[2].a); + test.equal(1, documents[3].a); + + // Sorting using array of names, assumes ascending order + collection + .find({ a: { $lt: 10 } }, { sort: ['a'] }) + .toArray(function (err, documents) { + test.equal(4, documents.length); + test.equal(1, documents[0].a); + test.equal(2, documents[1].a); + test.equal(3, documents[2].a); + test.equal(4, documents[3].a); + + // Sorting using single name, assumes ascending order + collection + .find({ a: { $lt: 10 } }, { sort: 'a' }) + .toArray(function (err, documents) { + test.equal(4, documents.length); + test.equal(1, documents[0].a); + test.equal(2, documents[1].a); + test.equal(3, documents[2].a); + test.equal(4, documents[3].a); + + // Sorting using single name, assumes ascending order, sort is hash + collection + .find({ a: { $lt: 10 } }, { sort: { a: 1 } }) + .toArray(function (err, documents) { + test.equal(4, documents.length); + test.equal(1, documents[0].a); + test.equal(2, documents[1].a); + test.equal(3, documents[2].a); + test.equal(4, documents[3].a); + + collection + .find({ a: { $lt: 10 } }, { sort: ['b', 'a'] }) + .toArray(function (err, documents) { + test.equal(4, documents.length); + test.equal(2, documents[0].a); + test.equal(4, documents[1].a); + test.equal(1, documents[2].a); + test.equal(3, documents[3].a); + + // Sorting using empty array, no order guarantee should not blow up + collection + .find({ a: { $lt: 10 } }, { sort: [] }) + .toArray(function (err, documents) { + test.equal(4, documents.length); + + /* NONACTUAL */ + // Sorting using ordered hash + collection + .find({ a: { $lt: 10 } }, { sort: { a: -1 } }) + .toArray(function (err, documents) { + // Fail test if not an error + test.equal(4, documents.length); + // Let's close the db + client.close(done); + }); + }); + }); + }); + }); + }); + }); + }); + }); + } + ); }); }); } @@ -343,43 +340,42 @@ describe('Find', function () { db.createCollection('test_find_limits', function (err) { expect(err).to.not.exist; - db.collection('test_find_limits', function (err, collection) { - // Insert some test documents - collection.insert( - [{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }], - configuration.writeConcernMax(), - function (err) { - expect(err).to.not.exist; + const collection = db.collection('test_find_limits'); + // Insert some test documents + collection.insert( + [{ a: 1 }, { b: 2 }, { c: 3 }, { d: 4 }], + configuration.writeConcernMax(), + function (err) { + expect(err).to.not.exist; - // Test limits - collection.find({}, { limit: 1 }).toArray(function (err, documents) { - test.equal(1, documents.length); + // Test limits + collection.find({}, { limit: 1 }).toArray(function (err, documents) { + test.equal(1, documents.length); + + collection.find({}, { limit: 2 }).toArray(function (err, documents) { + test.equal(2, documents.length); - collection.find({}, { limit: 2 }).toArray(function (err, documents) { - test.equal(2, documents.length); + collection.find({}, { limit: 3 }).toArray(function (err, documents) { + test.equal(3, documents.length); - collection.find({}, { limit: 3 }).toArray(function (err, documents) { - test.equal(3, documents.length); + collection.find({}, { limit: 4 }).toArray(function (err, documents) { + test.equal(4, documents.length); - collection.find({}, { limit: 4 }).toArray(function (err, documents) { + collection.find({}, {}).toArray(function (err, documents) { test.equal(4, documents.length); - collection.find({}, {}).toArray(function (err, documents) { + collection.find({}, { limit: 99 }).toArray(function (err, documents) { test.equal(4, documents.length); - - collection.find({}, { limit: 99 }).toArray(function (err, documents) { - test.equal(4, documents.length); - // Let's close the db - client.close(done); - }); + // Let's close the db + client.close(done); }); }); }); }); }); - } - ); - }); + }); + } + ); }); }); } @@ -401,23 +397,22 @@ describe('Find', function () { db.createCollection('test_find_non_quoted_values', function (err) { expect(err).to.not.exist; - db.collection('test_find_non_quoted_values', function (err, collection) { - // insert test document - collection.insert( - [ - { a: 19, b: 'teststring', c: 59920303 }, - { a: '19', b: 'teststring', c: 3984929 } - ], - configuration.writeConcernMax(), - function (err) { - expect(err).to.not.exist; - collection.find({ a: 19 }).toArray(function (err, documents) { - test.equal(1, documents.length); - client.close(done); - }); - } - ); - }); + const collection = db.collection('test_find_non_quoted_values'); + // insert test document + collection.insert( + [ + { a: 19, b: 'teststring', c: 59920303 }, + { a: '19', b: 'teststring', c: 3984929 } + ], + configuration.writeConcernMax(), + function (err) { + expect(err).to.not.exist; + collection.find({ a: 19 }).toArray(function (err, documents) { + test.equal(1, documents.length); + client.close(done); + }); + } + ); }); }); } @@ -439,34 +434,33 @@ describe('Find', function () { db.createCollection('test_find_embedded_document', function (err) { expect(err).to.not.exist; - db.collection('test_find_embedded_document', function (err, collection) { - // insert test document - collection.insert( - [ - { a: { id: 10, value: 'foo' }, b: 'bar', c: { id: 20, value: 'foobar' } }, - { a: { id: 11, value: 'foo' }, b: 'bar2', c: { id: 20, value: 'foobar' } } - ], - configuration.writeConcernMax(), - function (err) { - expect(err).to.not.exist; + const collection = db.collection('test_find_embedded_document'); + // insert test document + collection.insert( + [ + { a: { id: 10, value: 'foo' }, b: 'bar', c: { id: 20, value: 'foobar' } }, + { a: { id: 11, value: 'foo' }, b: 'bar2', c: { id: 20, value: 'foobar' } } + ], + configuration.writeConcernMax(), + function (err) { + expect(err).to.not.exist; - // test using integer value - collection.find({ 'a.id': 10 }).toArray(function (err, documents) { - test.equal(1, documents.length); - test.equal('bar', documents[0].b); + // test using integer value + collection.find({ 'a.id': 10 }).toArray(function (err, documents) { + test.equal(1, documents.length); + test.equal('bar', documents[0].b); - // test using string value - collection.find({ 'a.value': 'foo' }).toArray(function (err, documents) { - // should yield 2 documents - test.equal(2, documents.length); - test.equal('bar', documents[0].b); - test.equal('bar2', documents[1].b); - client.close(done); - }); + // test using string value + collection.find({ 'a.value': 'foo' }).toArray(function (err, documents) { + // should yield 2 documents + test.equal(2, documents.length); + test.equal('bar', documents[0].b); + test.equal('bar2', documents[1].b); + client.close(done); }); - } - ); - }); + }); + } + ); }); }); } @@ -487,13 +481,12 @@ describe('Find', function () { var db = client.db(configuration.db); db.createCollection('test_find_one_no_records', function (err) { expect(err).to.not.exist; - db.collection('test_find_one_no_records', function (err, collection) { - expect(err).to.not.exist; - collection.find({ a: 1 }, {}).toArray(function (err, documents) { - test.equal(0, documents.length); - // Let's close the db - client.close(done); - }); + const collection = db.collection('test_find_one_no_records'); + expect(err).to.not.exist; + collection.find({ a: 1 }, {}).toArray(function (err, documents) { + test.equal(0, documents.length); + // Let's close the db + client.close(done); }); }); }); @@ -704,14 +697,10 @@ describe('Find', function () { ) { collection.insert({ a: 0 }, configuration.writeConcernMax(), function (err) { expect(err).to.not.exist; - db.collection('test_should_correctly_retrieve_one_record', function ( - err, - usercollection - ) { - usercollection.findOne({ a: 0 }, function (err) { - expect(err).to.not.exist; - p_client.close(done); - }); + const usercollection = db.collection('test_should_correctly_retrieve_one_record'); + usercollection.findOne({ a: 0 }, function (err) { + expect(err).to.not.exist; + p_client.close(done); }); }); }); @@ -760,28 +749,27 @@ describe('Find', function () { var db = client.db(configuration.db); db.createCollection('test_field_select_with_options', function (err) { expect(err).to.not.exist; - db.collection('test_field_select_with_options', function (err, collection) { - var docCount = 25, - docs = []; + const collection = db.collection('test_field_select_with_options'); + var docCount = 25, + docs = []; - // Insert some test documents - while (docCount--) docs.push({ a: docCount, b: docCount }); - collection.insert(docs, configuration.writeConcernMax(), function (err, retDocs) { - docs = retDocs; - - collection - .find({}, { limit: 3, sort: [['a', -1]], projection: { a: 1 } }) - .toArray(function (err, documents) { - test.equal(3, documents.length); + // Insert some test documents + while (docCount--) docs.push({ a: docCount, b: docCount }); + collection.insert(docs, configuration.writeConcernMax(), function (err, retDocs) { + docs = retDocs; - documents.forEach(function (doc, idx) { - expect(doc.b).to.not.exist; // making sure field select works - test.equal(24 - idx, doc.a); // checking limit sort object with field select - }); + collection + .find({}, { limit: 3, sort: [['a', -1]], projection: { a: 1 } }) + .toArray(function (err, documents) { + test.equal(3, documents.length); - client.close(done); + documents.forEach(function (doc, idx) { + expect(doc.b).to.not.exist; // making sure field select works + test.equal(24 - idx, doc.a); // checking limit sort object with field select }); - }); + + client.close(done); + }); }); }); }); @@ -1626,7 +1614,7 @@ describe('Find', function () { p_client.connect(function (err, client) { var db = client.db(configuration.db); // Create a collection - db.collection('collection1', function (err, collection) { + db.createCollection('collection1', function (err, collection) { // Wait a bit and then execute something that will throw a duplicate error setTimeout(function () { var id = new ObjectId(); @@ -1643,7 +1631,7 @@ describe('Find', function () { }, 200); }); - db.collection('collection2', function (err, collection) { + db.createCollection('collection2', function (err, collection) { // Keep hammering in inserts var insert; insert = function () { @@ -1998,31 +1986,30 @@ describe('Find', function () { var db = client.db(configuration.db); // Create a collection we want to drop later - db.collection('shouldCorrectlyPerformNegativeLimit', function (err, collection) { - var docs = []; - for (var i = 0; i < 1000; i++) { - docs.push({ - a: 1, - b: - 'helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld' - }); - } + const collection = db.collection('shouldCorrectlyPerformNegativeLimit'); + var docs = []; + for (var i = 0; i < 1000; i++) { + docs.push({ + a: 1, + b: + 'helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld helloworld' + }); + } - // Insert a bunch of documents - collection.insert(docs, configuration.writeConcernMax(), function (err) { - expect(err).to.not.exist; + // Insert a bunch of documents + collection.insert(docs, configuration.writeConcernMax(), function (err) { + expect(err).to.not.exist; - // Perform a simple find and return all the documents - collection - .find({}) - .limit(-10) - .toArray(function (err, docs) { - expect(err).to.not.exist; - test.equal(10, docs.length); + // Perform a simple find and return all the documents + collection + .find({}) + .limit(-10) + .toArray(function (err, docs) { + expect(err).to.not.exist; + test.equal(10, docs.length); - client.close(done); - }); - }); + client.close(done); + }); }); }); } @@ -2041,7 +2028,7 @@ describe('Find', function () { var db = client.db(configuration.db); // Create a collection we want to drop later - db.collection('shouldCorrectlyExecuteExhaustQuery', function (err, collection) { + db.createCollection('shouldCorrectlyExecuteExhaustQuery', function (err, collection) { expect(err).to.not.exist; var docs1 = []; @@ -2108,7 +2095,7 @@ describe('Find', function () { } // Create a collection we want to drop later - db.collection('Readpreferencesshouldworkfine', function (err, collection) { + db.createCollection('Readpreferencesshouldworkfine', function (err, collection) { // Insert a bunch of documents collection.insert(docs, configuration.writeConcernMax(), function (err) { expect(err).to.not.exist; @@ -2137,18 +2124,17 @@ describe('Find', function () { var db = client.db(configuration.db); expect(err).to.not.exist; // Create a collection we want to drop later - db.collection('noresultAvailableForEachToIterate', function (err, collection) { - // Perform a simple find and return all the documents - collection.find({}).forEach( - doc => { - expect(doc).to.not.exist; - }, - err => { - expect(err).to.not.exist; - client.close(done); - } - ); - }); + const collection = db.collection('noresultAvailableForEachToIterate'); + // Perform a simple find and return all the documents + collection.find({}).forEach( + doc => { + expect(doc).to.not.exist; + }, + err => { + expect(err).to.not.exist; + client.close(done); + } + ); }); } }); @@ -2350,26 +2336,25 @@ describe('Find', function () { var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); client.connect(function (err, client) { var db = client.db(configuration.db); - db.collection('test_find_simple_batchsize_0', function (err, collection) { - // Insert some test documents - collection.insert( - [{ a: 2 }, { b: 3 }, { b: 4 }], - configuration.writeConcernMax(), - function (err) { - expect(err).to.not.exist; - // Ensure correct insertion testing via the cursor and the count function - collection - .find() - .batchSize(-5) - .toArray(function (err, documents) { - expect(err).to.not.exist; - test.equal(3, documents.length); - // Let's close the db - client.close(done); - }); - } - ); - }); + const collection = db.collection('test_find_simple_batchsize_0'); + // Insert some test documents + collection.insert( + [{ a: 2 }, { b: 3 }, { b: 4 }], + configuration.writeConcernMax(), + function (err) { + expect(err).to.not.exist; + // Ensure correct insertion testing via the cursor and the count function + collection + .find() + .batchSize(-5) + .toArray(function (err, documents) { + expect(err).to.not.exist; + test.equal(3, documents.length); + // Let's close the db + client.close(done); + }); + } + ); }); } }); @@ -2387,29 +2372,27 @@ describe('Find', function () { var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); client.connect(function (err, client) { var db = client.db(configuration.db); - db.collection('test_find_simple_limit_0', function (err, collection) { - expect(err).to.not.exist; + const collection = db.collection('test_find_simple_limit_0'); - // Insert some test documents - collection.insert( - [{ a: 2 }, { b: 3 }, { b: 4 }], - configuration.writeConcernMax(), - function (err) { - expect(err).to.not.exist; - // Ensure correct insertion testing via the cursor and the count function - collection - .find() - .limit(-5) - .toArray(function (err, documents) { - expect(err).to.not.exist; - test.equal(3, documents.length); + // Insert some test documents + collection.insert( + [{ a: 2 }, { b: 3 }, { b: 4 }], + configuration.writeConcernMax(), + function (err) { + expect(err).to.not.exist; + // Ensure correct insertion testing via the cursor and the count function + collection + .find() + .limit(-5) + .toArray(function (err, documents) { + expect(err).to.not.exist; + test.equal(3, documents.length); - // Let's close the db - client.close(done); - }); - } - ); - }); + // Let's close the db + client.close(done); + }); + } + ); }); } }); @@ -2427,32 +2410,29 @@ describe('Find', function () { var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); client.connect(function (err, client) { var db = client.db(configuration.db); - db.collection('elem_match_test', function (err, collection) { - expect(err).to.not.exist; - - // Insert some test documents - collection.insert( - [ - { _id: 1, results: [82, 85, 88] }, - { _id: 2, results: [75, 88, 89] } - ], - configuration.writeConcernMax(), - function (err) { - expect(err).to.not.exist; + const collection = db.collection('elem_match_test'); + // Insert some test documents + collection.insert( + [ + { _id: 1, results: [82, 85, 88] }, + { _id: 2, results: [75, 88, 89] } + ], + configuration.writeConcernMax(), + function (err) { + expect(err).to.not.exist; - // Ensure correct insertion testing via the cursor and the count function - collection - .find({ results: { $elemMatch: { $gte: 80, $lt: 85 } } }) - .toArray(function (err, documents) { - expect(err).to.not.exist; - test.deepEqual([{ _id: 1, results: [82, 85, 88] }], documents); + // Ensure correct insertion testing via the cursor and the count function + collection + .find({ results: { $elemMatch: { $gte: 80, $lt: 85 } } }) + .toArray(function (err, documents) { + expect(err).to.not.exist; + test.deepEqual([{ _id: 1, results: [82, 85, 88] }], documents); - // Let's close the db - client.close(done); - }); - } - ); - }); + // Let's close the db + client.close(done); + }); + } + ); }); } }); @@ -2470,50 +2450,47 @@ describe('Find', function () { var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); client.connect(function (err, client) { var db = client.db(configuration.db); - db.collection('test_find_simple_limit_101', function (err, collection) { - expect(err).to.not.exist; - - function clone(obj) { - var o = {}; - for (var name in obj) o[name] = obj[name]; - return o; - } + const collection = db.collection('test_find_simple_limit_101'); + function clone(obj) { + var o = {}; + for (var name in obj) o[name] = obj[name]; + return o; + } - var template = { - linkid: '12633170', - advertisercid: '4612127', - websitename: 'Car Rental 8', - destinationurl: 'https://www.carrental8.com/en/', - who: '8027061-12633170-1467924618000', - href: 'http://www.tkqlhce.com', - src: 'http://www.awltovhc.com', - r1: 3, - r2: 44, - r3: 24, - r4: 58 - }; + var template = { + linkid: '12633170', + advertisercid: '4612127', + websitename: 'Car Rental 8', + destinationurl: 'https://www.carrental8.com/en/', + who: '8027061-12633170-1467924618000', + href: 'http://www.tkqlhce.com', + src: 'http://www.awltovhc.com', + r1: 3, + r2: 44, + r3: 24, + r4: 58 + }; - var docs = []; - for (var i = 0; i < 1000; i++) { - docs.push(clone(template)); - } + var docs = []; + for (var i = 0; i < 1000; i++) { + docs.push(clone(template)); + } - // Insert some test documents - collection.insertMany(docs, configuration.writeConcernMax(), function (err, r) { - expect(err).to.not.exist; - test.ok(r); + // Insert some test documents + collection.insertMany(docs, configuration.writeConcernMax(), function (err, r) { + expect(err).to.not.exist; + test.ok(r); - // Ensure correct insertion testing via the cursor and the count function - collection - .find() - .limit(200) - .toArray(function (err, documents) { - expect(err).to.not.exist; - test.equal(200, documents.length); - // Let's close the db - client.close(done); - }); - }); + // Ensure correct insertion testing via the cursor and the count function + collection + .find() + .limit(200) + .toArray(function (err, documents) { + expect(err).to.not.exist; + test.equal(200, documents.length); + // Let's close the db + client.close(done); + }); }); }); } diff --git a/test/functional/index.test.js b/test/functional/index.test.js index 5447becc0f..eec7b82adf 100644 --- a/test/functional/index.test.js +++ b/test/functional/index.test.js @@ -357,34 +357,30 @@ describe('Indexes', function () { var db = client.db(configuration.db); db.createCollection('create_and_use_sparse_index_test', function (err) { expect(err).to.not.exist; - db.collection('create_and_use_sparse_index_test', function (err, collection) { + const collection = db.collection('create_and_use_sparse_index_test'); + collection.createIndex({ title: 1 }, { sparse: true, writeConcern: { w: 1 } }, function ( + err + ) { expect(err).to.not.exist; - collection.createIndex( - { title: 1 }, - { sparse: true, writeConcern: { w: 1 } }, + collection.insert( + [{ name: 'Jim' }, { name: 'Sarah', title: 'Princess' }], + configuration.writeConcernMax(), function (err) { expect(err).to.not.exist; - collection.insert( - [{ name: 'Jim' }, { name: 'Sarah', title: 'Princess' }], - configuration.writeConcernMax(), - function (err) { - expect(err).to.not.exist; - collection - .find({ title: { $ne: null } }) - .sort({ title: 1 }) - .toArray(function (err, items) { - test.equal(1, items.length); - test.equal('Sarah', items[0].name); - - // Fetch the info for the indexes - collection.indexInformation({ full: true }, function (err, indexInfo) { - expect(err).to.not.exist; - test.equal(2, indexInfo.length); - client.close(done); - }); - }); - } - ); + collection + .find({ title: { $ne: null } }) + .sort({ title: 1 }) + .toArray(function (err, items) { + test.equal(1, items.length); + test.equal('Sarah', items[0].name); + + // Fetch the info for the indexes + collection.indexInformation({ full: true }, function (err, indexInfo) { + expect(err).to.not.exist; + test.equal(2, indexInfo.length); + client.close(done); + }); + }); } ); }); @@ -410,22 +406,21 @@ describe('Indexes', function () { var db = client.db(configuration.db); db.createCollection('geospatial_index_test', function (err) { expect(err).to.not.exist; - db.collection('geospatial_index_test', function (err, collection) { - collection.createIndex({ loc: '2d' }, configuration.writeConcernMax(), function (err) { + const collection = db.collection('geospatial_index_test'); + collection.createIndex({ loc: '2d' }, configuration.writeConcernMax(), function (err) { + expect(err).to.not.exist; + collection.insert({ loc: [-100, 100] }, configuration.writeConcernMax(), function ( + err + ) { expect(err).to.not.exist; - collection.insert({ loc: [-100, 100] }, configuration.writeConcernMax(), function ( + + collection.insert({ loc: [200, 200] }, configuration.writeConcernMax(), function ( err ) { - expect(err).to.not.exist; - - collection.insert({ loc: [200, 200] }, configuration.writeConcernMax(), function ( - err - ) { - test.ok(err.errmsg.indexOf('point not in interval of') !== -1); - test.ok(err.errmsg.indexOf('-180') !== -1); - test.ok(err.errmsg.indexOf('180') !== -1); - client.close(done); - }); + test.ok(err.errmsg.indexOf('point not in interval of') !== -1); + test.ok(err.errmsg.indexOf('-180') !== -1); + test.ok(err.errmsg.indexOf('180') !== -1); + client.close(done); }); }); }); @@ -451,35 +446,34 @@ describe('Indexes', function () { var db = client.db(configuration.db); db.createCollection('geospatial_index_altered_test', function (err) { expect(err).to.not.exist; - db.collection('geospatial_index_altered_test', function (err, collection) { - collection.createIndex( - { loc: '2d' }, - { min: 0, max: 1024, writeConcern: { w: 1 } }, - function (err) { + const collection = db.collection('geospatial_index_altered_test'); + collection.createIndex( + { loc: '2d' }, + { min: 0, max: 1024, writeConcern: { w: 1 } }, + function (err) { + expect(err).to.not.exist; + collection.insert({ loc: [100, 100] }, configuration.writeConcernMax(), function ( + err + ) { expect(err).to.not.exist; - collection.insert({ loc: [100, 100] }, configuration.writeConcernMax(), function ( + collection.insert({ loc: [200, 200] }, configuration.writeConcernMax(), function ( err ) { expect(err).to.not.exist; - collection.insert({ loc: [200, 200] }, configuration.writeConcernMax(), function ( - err - ) { - expect(err).to.not.exist; - collection.insert( - { loc: [-200, -200] }, - configuration.writeConcernMax(), - function (err) { - test.ok(err.errmsg.indexOf('point not in interval of') !== -1); - test.ok(err.errmsg.indexOf('0') !== -1); - test.ok(err.errmsg.indexOf('1024') !== -1); - client.close(done); - } - ); - }); + collection.insert( + { loc: [-200, -200] }, + configuration.writeConcernMax(), + function (err) { + test.ok(err.errmsg.indexOf('point not in interval of') !== -1); + test.ok(err.errmsg.indexOf('0') !== -1); + test.ok(err.errmsg.indexOf('1024') !== -1); + client.close(done); + } + ); }); - } - ); - }); + }); + } + ); }); }); } diff --git a/test/functional/insert.test.js b/test/functional/insert.test.js index b913479a3e..71647f211e 100644 --- a/test/functional/insert.test.js +++ b/test/functional/insert.test.js @@ -267,7 +267,7 @@ describe('Insert', function () { }; } - db.collection( + db.createCollection( 'users', getResult(function (user_collection) { user_collection.remove({}, configuration.writeConcernMax(), function (err) { diff --git a/test/functional/multiple_db.test.js b/test/functional/multiple_db.test.js index bebf4ee066..740668d72c 100644 --- a/test/functional/multiple_db.test.js +++ b/test/functional/multiple_db.test.js @@ -77,7 +77,7 @@ describe('Multiple Databases', function () { db_instance = client.db('site2'); db_instance = client.db('rss'); - db_instance.collection('counters', function (err, collection) { + db_instance.createCollection('counters', function (err, collection) { expect(err).to.not.exist; collection.findOneAndUpdate( {}, diff --git a/test/functional/operation_example.test.js b/test/functional/operation_example.test.js index 45ae5ba58e..24c2155535 100644 --- a/test/functional/operation_example.test.js +++ b/test/functional/operation_example.test.js @@ -3359,70 +3359,6 @@ describe('Operation Examples', function () { } }); - /** - * An example of retrieving a collection from a db using the collection function. - * - * @example-class Db - * @example-method collection - */ - it('shouldCorrectlyAccessACollection', { - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: function (done) { - var configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); - client.connect(function (err, client) { - // LINE var MongoClient = require('mongodb').MongoClient, - // LINE test = require('assert'); - // LINE const client = new MongoClient('mongodb://localhost:27017/test'); - // LINE client.connect(function(err, client) { - // LINE var db = client.db('test); - // REPLACE configuration.writeConcernMax() WITH {w:1} - // REMOVE-LINE restartAndDone - // REMOVE-LINE done(); - // REMOVE-LINE var db = client.db(configuration.db); - // BEGIN - var db = client.db(configuration.db); - expect(err).to.not.exist; - - // Grab a collection with a callback but no safe operation - db.collection('test_correctly_access_collections', function (err, col2) { - test.ok(col2); - expect(err).to.not.exist; - - // Grab a collection with a callback in safe mode, ensuring it exists (should fail as it's not created) - db.collection('test_correctly_access_collections', { strict: true }, function ( - err, - col3 - ) { - expect(col3).to.not.exist; - test.ok(err != null); - - // Create the collection - db.createCollection('test_correctly_access_collections', function (err, result) { - test.ok(result); - expect(err).to.not.exist; - - // Retry to get the collection, should work as it's now created - db.collection('test_correctly_access_collections', { strict: true }, function ( - err, - col3 - ) { - test.ok(col3); - expect(err).to.not.exist; - - client.close(done); - }); - }); - }); - }); - }); - // END - } - }); - /** * An example of retrieving all collections for a db as Collection objects * diff --git a/test/functional/operation_promises_example.test.js b/test/functional/operation_promises_example.test.js index 88b58171c3..f3a621d435 100644 --- a/test/functional/operation_promises_example.test.js +++ b/test/functional/operation_promises_example.test.js @@ -2582,64 +2582,6 @@ describe('Operation (Promises)', function () { } }); - /** - * An example of retrieving a collection from a db using the collection function with a Promise. - * - * @example-class Db - * @example-method collection - */ - it('shouldCorrectlyAccessACollectionWithPromises', { - metadata: { requires: { topology: ['single'] } }, - - test: function (done) { - var configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { - maxPoolSize: 1 - }); - - client.connect().then(function (client) { - var db = client.db(configuration.db); - // LINE var MongoClient = require('mongodb').MongoClient, - // LINE test = require('assert'); - // LINE const client = new MongoClient('mongodb://localhost:27017/test'); - // LINE client.connect().then(() => { - // LINE var db = client.db('test); - // REPLACE configuration.writeConcernMax() WITH {w:1} - // REMOVE-LINE done(); - // BEGIN - // Grab a collection with a callback but no safe operation - db.collection('test_correctly_access_collections_with_promise', function (err) { - expect(err).to.not.exist; - - // Grab a collection with a callback in safe mode, ensuring it exists (should fail as it's not created) - db.collection( - 'test_correctly_access_collections_with_promise', - { strict: true }, - function (err) { - test.ok(err); - - // Create the collection - db.createCollection('test_correctly_access_collections_with_promise').then( - function () { - // Retry to get the collection, should work as it's now created - db.collection( - 'test_correctly_access_collections_with_promise', - { strict: true }, - function (err) { - expect(err).to.not.exist; - client.close(done); - } - ); - } - ); - } - ); - }); - }); - // END - } - }); - /** * An example of retrieving all collections for a db as Collection objects using a Promise. * diff --git a/test/functional/remove.test.js b/test/functional/remove.test.js index 72caf51842..e77e77d56f 100644 --- a/test/functional/remove.test.js +++ b/test/functional/remove.test.js @@ -25,31 +25,29 @@ describe('Remove', function () { db.createCollection('test_clear', function (err) { expect(err).to.not.exist; - db.collection('test_clear', function (err, collection) { + const collection = db.collection('test_clear'); + + collection.insert({ i: 1 }, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; - collection.insert({ i: 1 }, { writeConcern: { w: 1 } }, function (err) { + collection.insert({ i: 2 }, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; - collection.insert({ i: 2 }, { writeConcern: { w: 1 } }, function (err) { + collection.count(function (err, count) { expect(err).to.not.exist; + expect(count).to.equal(2); - collection.count(function (err, count) { + // Clear the collection + collection.remove({}, { writeConcern: { w: 1 } }, function (err, r) { expect(err).to.not.exist; - expect(count).to.equal(2); + expect(r).property('deletedCount').to.equal(2); - // Clear the collection - collection.remove({}, { writeConcern: { w: 1 } }, function (err, r) { + collection.count(function (err, count) { expect(err).to.not.exist; - expect(r).property('deletedCount').to.equal(2); - - collection.count(function (err, count) { - expect(err).to.not.exist; - expect(count).to.equal(0); + expect(count).to.equal(0); - // Let's close the db - client.close(done); - }); + // Let's close the db + client.close(done); }); }); }); @@ -78,33 +76,31 @@ describe('Remove', function () { db.createCollection('test_remove_regexp', function (err) { expect(err).to.not.exist; - db.collection('test_remove_regexp', function (err, collection) { - expect(err).to.not.exist; + const collection = db.collection('test_remove_regexp'); - collection.insert( - { address: '485 7th ave new york' }, - { writeConcern: { w: 1 } }, - function (err) { - expect(err).to.not.exist; + collection.insert( + { address: '485 7th ave new york' }, + { writeConcern: { w: 1 } }, + function (err) { + expect(err).to.not.exist; - // Clear the collection - collection.remove({ address: /485 7th ave/ }, { writeConcern: { w: 1 } }, function ( - err, - r - ) { - expect(r).property('deletedCount').to.equal(1); + // Clear the collection + collection.remove({ address: /485 7th ave/ }, { writeConcern: { w: 1 } }, function ( + err, + r + ) { + expect(r).property('deletedCount').to.equal(1); - collection.count(function (err, count) { - expect(err).to.not.exist; - expect(count).to.equal(0); + collection.count(function (err, count) { + expect(err).to.not.exist; + expect(count).to.equal(0); - // Let's close the db - client.close(done); - }); + // Let's close the db + client.close(done); }); - } - ); - }); + }); + } + ); }); }); } @@ -128,30 +124,28 @@ describe('Remove', function () { db.createCollection('shouldCorrectlyRemoveOnlyFirstDocument', function (err) { expect(err).to.not.exist; - db.collection('shouldCorrectlyRemoveOnlyFirstDocument', function (err, collection) { - expect(err).to.not.exist; + const collection = db.collection('shouldCorrectlyRemoveOnlyFirstDocument'); - collection.insert( - [{ a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }], - { writeConcern: { w: 1 } }, - function (err) { - expect(err).to.not.exist; + collection.insert( + [{ a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }], + { writeConcern: { w: 1 } }, + function (err) { + expect(err).to.not.exist; - // Remove the first - collection.remove({ a: 1 }, { writeConcern: { w: 1 }, single: true }, function ( - err, - r - ) { - expect(r).property('deletedCount').to.equal(1); + // Remove the first + collection.remove({ a: 1 }, { writeConcern: { w: 1 }, single: true }, function ( + err, + r + ) { + expect(r).property('deletedCount').to.equal(1); - collection.find({ a: 1 }).count(function (err, result) { - expect(result).to.equal(3); - client.close(done); - }); + collection.find({ a: 1 }).count(function (err, result) { + expect(result).to.equal(3); + client.close(done); }); - } - ); - }); + }); + } + ); }); }); } diff --git a/test/functional/unicode.test.js b/test/functional/unicode.test.js index bde972e70b..d3c8b99b1d 100644 --- a/test/functional/unicode.test.js +++ b/test/functional/unicode.test.js @@ -122,17 +122,15 @@ describe('Unicode', function () { var db = client.db(configuration.db); db.createCollection('create_object_with_chinese_object_name', function (err) { expect(err).to.not.exist; - db.collection('create_object_with_chinese_object_name', function (err, collection) { + const collection = db.collection('create_object_with_chinese_object_name'); + collection.insert(object, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; - collection.insert(object, { writeConcern: { w: 1 } }, function (err) { - expect(err).to.not.exist; - collection.findOne(function (err, item) { - test.equal(object['客家话'], item['客家话']); + collection.findOne(function (err, item) { + test.equal(object['客家话'], item['客家话']); - collection.find().toArray(function (err, items) { - test.equal(object['客家话'], items[0]['客家话']); - client.close(done); - }); + collection.find().toArray(function (err, items) { + test.equal(object['客家话'], items[0]['客家话']); + client.close(done); }); }); }); diff --git a/test/unit/db_list_collections.test.js b/test/unit/db_list_collections.test.js index 056551ed02..9baa658ad4 100644 --- a/test/unit/db_list_collections.test.js +++ b/test/unit/db_list_collections.test.js @@ -45,11 +45,6 @@ describe('db.listCollections', function () { description: 'should send nameOnly: true for db.collections', command: db => db.collections(() => {}), listCollectionsValue: true - }, - { - description: 'should send nameOnly: true for db.collection', - command: db => db.collection('foo', { strict: true }, () => {}), - listCollectionsValue: true } ].forEach(config => { function testFn(done) {