From accb457dce709526a8841e491541955407248cb5 Mon Sep 17 00:00:00 2001 From: Hana Pearlman Date: Fri, 20 Nov 2020 10:39:44 -0500 Subject: [PATCH 1/5] remove top-level wc options --- src/cmap/wire_protocol/command.ts | 8 +++----- src/db.ts | 5 +---- src/gridfs-stream/upload.ts | 6 +++--- src/mongo_client.ts | 11 +++++++++-- src/operations/connect.ts | 32 ++++++++++++++++--------------- src/write_concern.ts | 29 ++++++++++++++-------------- 6 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/cmap/wire_protocol/command.ts b/src/cmap/wire_protocol/command.ts index a165cba9f3b..a97c4342dfb 100644 --- a/src/cmap/wire_protocol/command.ts +++ b/src/cmap/wire_protocol/command.ts @@ -8,11 +8,12 @@ import type { Document, BSONSerializeOptions } from '../../bson'; import type { Server } from '../../sdam/server'; import type { Topology } from '../../sdam/topology'; import type { ReadPreferenceLike } from '../../read_preference'; -import type { WriteConcernOptions, WriteConcern, W } from '../../write_concern'; +import type { WriteConcernOptions } from '../../write_concern'; import type { WriteCommandOptions } from './write_command'; /** @internal */ -export interface CommandOptions extends BSONSerializeOptions { +// FIXME: NODE-2781 +export interface CommandOptions extends BSONSerializeOptions, WriteConcernOptions { command?: boolean; slaveOk?: boolean; /** Specify read preference if command supports it */ @@ -28,9 +29,6 @@ export interface CommandOptions extends BSONSerializeOptions { // FIXME: NODE-2802 willRetryWrite?: boolean; - - // FIXME: NODE-2781 - writeConcern?: WriteConcernOptions | WriteConcern | W; } function isClientEncryptionEnabled(server: Server) { diff --git a/src/db.ts b/src/db.ts index 0c8feaf5475..bdeacfa04a8 100644 --- a/src/db.ts +++ b/src/db.ts @@ -60,10 +60,7 @@ import type { Admin } from './admin'; // Allowed parameters const legalOptionNames = [ - 'w', - 'wtimeout', - 'fsync', - 'j', + 'writeConcern', 'readPreference', 'readPreferenceTags', 'native_parser', diff --git a/src/gridfs-stream/upload.ts b/src/gridfs-stream/upload.ts index ce144e9fa50..b839edebd57 100644 --- a/src/gridfs-stream/upload.ts +++ b/src/gridfs-stream/upload.ts @@ -515,9 +515,9 @@ function doWrite( function getWriteOptions(stream: GridFSBucketWriteStream): WriteConcernOptions { const obj: WriteConcernOptions = {}; if (stream.writeConcern) { - obj.w = stream.writeConcern.w; - obj.wtimeout = stream.writeConcern.wtimeout; - obj.j = stream.writeConcern.j; + obj.writeConcern = { w: stream.writeConcern.w }; + obj.writeConcern.wtimeout = stream.writeConcern.wtimeout; + obj.writeConcern.j = stream.writeConcern.j; } return obj; } diff --git a/src/mongo_client.ts b/src/mongo_client.ts index ad01cc1f041..41a9f003e56 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -3,7 +3,7 @@ import { EventEmitter } from 'events'; import { ChangeStream, ChangeStreamOptions } from './change_stream'; import { ReadPreference, ReadPreferenceMode } from './read_preference'; import { MongoError, AnyError } from './error'; -import { WriteConcern, WriteConcernOptions } from './write_concern'; +import { W, WriteConcern, WriteConcernOptions } from './write_concern'; import { maybePromise, MongoDBNamespace, Callback } from './utils'; import { deprecate } from 'util'; import { connect, validOptions } from './operations/connect'; @@ -54,7 +54,7 @@ type CleanUpHandlerFunction = (err?: AnyError, result?: any, opts?: any) => Prom * @public * @see https://docs.mongodb.com/manual/reference/connection-string */ -export interface MongoURIOptions extends Pick { +export interface MongoURIOptions { /** Specifies the name of the replica set, if the mongod is a member of a replica set. */ replicaSet?: string; /** Enables or disables TLS/SSL for the connection. */ @@ -127,6 +127,13 @@ export interface MongoURIOptions extends Pick Date: Fri, 20 Nov 2020 10:40:03 -0500 Subject: [PATCH 2/5] changes to pass tests --- test/functional/apm.test.js | 9 +-- test/functional/bulk.test.js | 10 +-- test/functional/collection.test.js | 6 +- test/functional/command_write_concern.test.js | 65 ++++++++++++------- test/functional/find_and_modify.test.js | 39 ++++++----- test/functional/insert.test.js | 2 +- test/functional/mongo_client.test.js | 5 +- .../operation_promises_example.test.js | 4 +- test/functional/promises_collection.test.js | 8 ++- test/functional/spec-runner/index.js | 12 ++-- test/functional/write_concern.test.js | 11 ++-- test/tools/runner/config.js | 10 ++- 12 files changed, 106 insertions(+), 75 deletions(-) diff --git a/test/functional/apm.test.js b/test/functional/apm.test.js index cfbf3b3d43c..977767b524e 100644 --- a/test/functional/apm.test.js +++ b/test/functional/apm.test.js @@ -961,13 +961,8 @@ describe('APM', function () { if (args.requests) params.push(args.requests); if (args.writeConcern) { - if (options == null) { - options = args.writeConcern; - } else { - for (let name in args.writeConcern) { - options[name] = args.writeConcern[name]; - } - } + options = options || {}; + options.writeConcern = args.writeConcern; } if (typeof args.ordered === 'boolean') { diff --git a/test/functional/bulk.test.js b/test/functional/bulk.test.js index 41bbf372a28..68bba334bf3 100644 --- a/test/functional/bulk.test.js +++ b/test/functional/bulk.test.js @@ -76,7 +76,7 @@ describe('Bulk', function () { metadata: { requires: { mongodb: '>=3.6.x' } }, test: function (done) { const configuration = this.configuration; - const client = configuration.newClient({}, { w: 1 }); + const client = configuration.newClient({ w: 1 }); client.connect((err, client) => { const db = client.db(configuration.db); @@ -620,7 +620,7 @@ describe('Bulk', function () { bulk.find({ b: 1 }).upsert().update({ b: 1 }); bulk.find({ c: 1 }).remove(); - bulk.execute({ w: 0 }, function (err, result) { + bulk.execute({ writeConcern: { w: 0 } }, function (err, result) { expect(err).to.not.exist; test.equal(0, result.nUpserted); test.equal(0, result.nInserted); @@ -1126,7 +1126,7 @@ describe('Bulk', function () { bulk.find({ b: 1 }).upsert().update({ b: 1 }); bulk.find({ c: 1 }).remove(); - bulk.execute({ w: 0 }, function (err, result) { + bulk.execute({ writeConcern: { w: 0 } }, function (err, result) { expect(err).to.not.exist; test.equal(0, result.nUpserted); test.equal(0, result.nInserted); @@ -1162,7 +1162,7 @@ describe('Bulk', function () { batch.insert({ a: 1 }); batch.insert({ a: 2 }); - batch.execute({ w: 2, wtimeout: 1000 }, function (err) { + batch.execute({ writeConcern: { w: 2, wtimeout: 1000 } }, function (err) { test.ok(err != null); test.ok(err.code != null); test.ok(err.errmsg != null); @@ -1233,7 +1233,7 @@ describe('Bulk', function () { batch.insert({ a: 1 }); batch.insert({ a: 2 }); - batch.execute({ w: 2, wtimeout: 1000 }, function (err) { + batch.execute({ writeConcern: { w: 2, wtimeout: 1000 } }, function (err) { test.ok(err != null); test.ok(err.code != null); test.ok(err.errmsg != null); diff --git a/test/functional/collection.test.js b/test/functional/collection.test.js index 5eb2df734c5..d617ad23332 100644 --- a/test/functional/collection.test.js +++ b/test/functional/collection.test.js @@ -668,7 +668,7 @@ describe('Collection', function () { let db; let collection; beforeEach(function () { - client = configuration.newClient({}, { w: 1 }); + client = configuration.newClient({ w: 1 }); return client.connect().then(client => { db = client.db(configuration.db); @@ -852,7 +852,7 @@ describe('Collection', function () { function testCapped(testConfiguration, config, done) { const configuration = config.config; - const client = testConfiguration.newClient({}, { w: 1 }); + const client = testConfiguration.newClient({ w: 1 }); client.connect((err, client) => { const db = client.db(configuration.db); @@ -938,7 +938,7 @@ describe('Collection', function () { metadata: { requires: { mongodb: '>=3.0.0' } }, test: function (done) { const configuration = this.configuration; - const client = configuration.newClient({}, { w: 1 }); + const client = configuration.newClient({ w: 1 }); let finish = err => { finish = () => {}; diff --git a/test/functional/command_write_concern.test.js b/test/functional/command_write_concern.test.js index d4b6f3b23fa..85fffb88789 100644 --- a/test/functional/command_write_concern.test.js +++ b/test/functional/command_write_concern.test.js @@ -121,8 +121,10 @@ describe('Command Write Concern', function () { db.collection('test') .aggregate([{ $match: {} }, { $out: 'readConcernCollectionAggregate1Output' }], { - w: 2, - wtimeout: 1000 + writeConcern: { + w: 2, + wtimeout: 1000 + } }) .toArray(function (err) { expect(err).to.not.exist; @@ -244,12 +246,16 @@ describe('Command Write Concern', function () { expect(err).to.not.exist; var db = client.db(configuration.db); - db.createCollection('test_collection_methods', { w: 2, wtimeout: 1000 }, function (err) { - expect(err).to.not.exist; - test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); + db.createCollection( + 'test_collection_methods', + { writeConcern: { w: 2, wtimeout: 1000 } }, + function (err) { + expect(err).to.not.exist; + test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); - client.close(done); - }); + client.close(done); + } + ); }); }); } @@ -359,8 +365,7 @@ describe('Command Write Concern', function () { { a: 1 }, { indexOptionDefaults: true, - w: 2, - wtimeout: 1000 + writeConcern: { w: 2, wtimeout: 1000 } }, function (err) { expect(err).to.not.exist; @@ -475,8 +480,10 @@ describe('Command Write Concern', function () { db.collection('indexOptionDefault').drop( { - w: 2, - wtimeout: 1000 + writeConcern: { + w: 2, + wtimeout: 1000 + } }, function (err) { expect(err).to.not.exist; @@ -591,8 +598,10 @@ describe('Command Write Concern', function () { db.dropDatabase( { - w: 2, - wtimeout: 1000 + writeConcern: { + w: 2, + wtimeout: 1000 + } }, function (err) { expect(err).to.not.exist; @@ -707,8 +716,10 @@ describe('Command Write Concern', function () { db.collection('test').dropIndexes( { - w: 2, - wtimeout: 1000 + writeConcern: { + w: 2, + wtimeout: 1000 + } }, function (err) { expect(err).to.not.exist; @@ -831,8 +842,7 @@ describe('Command Write Concern', function () { reduce, { out: { replace: 'tempCollection' }, - w: 2, - wtimeout: 1000 + writeConcern: { w: 2, wtimeout: 1000 } }, function (err) { expect(err).to.not.exist; @@ -945,12 +955,17 @@ describe('Command Write Concern', function () { expect(err).to.not.exist; var db = client.db(configuration.db); - db.admin().addUser('kay:kay', 'abc123', { w: 2, wtimeout: 1000 }, function (err) { - expect(err).to.not.exist; - test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); + db.admin().addUser( + 'kay:kay', + 'abc123', + { writeConcern: { w: 2, wtimeout: 1000 } }, + function (err) { + expect(err).to.not.exist; + test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); - client.close(done); - }); + client.close(done); + } + ); }); }); } @@ -1055,7 +1070,9 @@ describe('Command Write Concern', function () { expect(err).to.not.exist; var db = client.db(configuration.db); - db.admin().removeUser('kay:kay', { w: 2, wtimeout: 1000 }, function (err) { + db.admin().removeUser('kay:kay', { writeConcern: { w: 2, wtimeout: 1000 } }, function ( + err + ) { expect(err).to.not.exist; test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); @@ -1170,7 +1187,7 @@ describe('Command Write Concern', function () { { a: 1 }, [['a', 1]], { $set: { b1: 1 } }, - { new: true, w: 2, wtimeout: 1000 }, + { new: true, writeConcern: { w: 2, wtimeout: 1000 } }, function (err) { expect(err).to.not.exist; test.deepEqual({ w: 2, wtimeout: 1000 }, commandResult.writeConcern); diff --git a/test/functional/find_and_modify.test.js b/test/functional/find_and_modify.test.js index 5d85361fa1b..13ba5243f48 100644 --- a/test/functional/find_and_modify.test.js +++ b/test/functional/find_and_modify.test.js @@ -40,16 +40,11 @@ describe('Find and Modify', function () { var collection = db.collection('findAndModifyTEST'); // Execute findOneAndUpdate - collection.findOneAndUpdate({}, { $set: { a: 1 } }, { fsync: 1 }, function (err) { - expect(err).to.not.exist; - test.deepEqual({ fsync: 1 }, started[0].command.writeConcern); - - // Cleanup - started = []; - succeeded = []; - - // Execute findOneAndReplace - collection.findOneAndReplace({}, { b: 1 }, { fsync: 1 }, function (err) { + collection.findOneAndUpdate( + {}, + { $set: { a: 1 } }, + { writeConcern: { fsync: 1 } }, + function (err) { expect(err).to.not.exist; test.deepEqual({ fsync: 1 }, started[0].command.writeConcern); @@ -58,15 +53,27 @@ describe('Find and Modify', function () { succeeded = []; // Execute findOneAndReplace - collection.findOneAndDelete({}, { fsync: 1 }, function (err) { + collection.findOneAndReplace({}, { b: 1 }, { writeConcern: { fsync: 1 } }, function ( + err + ) { expect(err).to.not.exist; test.deepEqual({ fsync: 1 }, started[0].command.writeConcern); - listener.uninstrument(); - client.close(done); + // Cleanup + started = []; + succeeded = []; + + // Execute findOneAndReplace + collection.findOneAndDelete({}, { writeConcern: { fsync: 1 } }, function (err) { + expect(err).to.not.exist; + test.deepEqual({ fsync: 1 }, started[0].command.writeConcern); + + listener.uninstrument(); + client.close(done); + }); }); - }); - }); + } + ); }); } }); @@ -100,7 +107,7 @@ describe('Find and Modify', function () { var db = client.db(configuration.db); expect(err).to.not.exist; - var collection = db.collection('findAndModifyTEST', { fsync: 1 }); + var collection = db.collection('findAndModifyTEST', { writeConcern: { fsync: 1 } }); // Execute findOneAndUpdate collection.findOneAndUpdate({}, { $set: { a: 1 } }, function (err) { expect(err).to.not.exist; diff --git a/test/functional/insert.test.js b/test/functional/insert.test.js index 1f51543c8cd..bb968fd010d 100644 --- a/test/functional/insert.test.js +++ b/test/functional/insert.test.js @@ -1451,7 +1451,7 @@ describe('Insert', function () { client.connect(function (err, client) { var db = client.db(configuration.db); var collection = db.collection('gh-completely3'); - collection.update({ a: 1 }, { a: 2 }, { upsert: true, w: 0 }, cb); + collection.update({ a: 1 }, { a: 2 }, { upsert: true, writeConcern: { w: 0 } }, cb); }); } }); diff --git a/test/functional/mongo_client.test.js b/test/functional/mongo_client.test.js index e9a404e3eb6..c83c9cd57a1 100644 --- a/test/functional/mongo_client.test.js +++ b/test/functional/mongo_client.test.js @@ -25,10 +25,7 @@ describe('MongoClient', function () { const client = configuration.newClient( {}, { - w: 1, - wtimeout: 1000, - fsync: true, - j: true, + writeConcern: { w: 1, wtimeout: 1000, fsync: true, j: true }, readPreference: 'nearest', readPreferenceTags: { loc: 'ny' }, native_parser: false, diff --git a/test/functional/operation_promises_example.test.js b/test/functional/operation_promises_example.test.js index 496011a8010..6caf9ed9a42 100644 --- a/test/functional/operation_promises_example.test.js +++ b/test/functional/operation_promises_example.test.js @@ -2031,12 +2031,12 @@ describe('Operation (Promises)', function () { // Insert a bunch of documents return collection - .insertMany([{ a: 1 }, { b: 2 }], { w: 1 }) + .insertMany([{ a: 1 }, { b: 2 }], { writeConcern: { w: 1 } }) .then(function (result) { test.ok(result); // Remove all the document - return collection.removeOne({ a: 1 }, { w: 1 }); + return collection.removeOne({ a: 1 }, { writeConcern: { w: 1 } }); }) .then(function (r) { expect(r).property('deletedCount').to.equal(1); diff --git a/test/functional/promises_collection.test.js b/test/functional/promises_collection.test.js index 8b763cc3abf..0ff90092686 100644 --- a/test/functional/promises_collection.test.js +++ b/test/functional/promises_collection.test.js @@ -300,7 +300,9 @@ describe('Promises (Collection)', function () { const client = configuration.newClient(url); client.connect().then(function (client) { var db = client.db(configuration.db); - var bulk = db.collection('unordered_bulk_promise_form').initializeUnorderedBulkOp({ w: 1 }); + var bulk = db + .collection('unordered_bulk_promise_form') + .initializeUnorderedBulkOp({ writeConcern: { w: 1 } }); bulk.insert({ a: 1 }); return bulk .execute() @@ -333,7 +335,9 @@ describe('Promises (Collection)', function () { const client = configuration.newClient(url); client.connect().then(function (client) { var db = client.db(configuration.db); - var bulk = db.collection('unordered_bulk_promise_form').initializeOrderedBulkOp({ w: 1 }); + var bulk = db + .collection('unordered_bulk_promise_form') + .initializeOrderedBulkOp({ writeConcern: { w: 1 } }); bulk.insert({ a: 1 }); return bulk .execute() diff --git a/test/functional/spec-runner/index.js b/test/functional/spec-runner/index.js index 55cb3961c14..07d9904b350 100644 --- a/test/functional/spec-runner/index.js +++ b/test/functional/spec-runner/index.js @@ -184,7 +184,7 @@ function prepareDatabaseForSuite(suite, context) { const coll = db.collection(context.collectionName); return setupPromise - .then(() => coll.drop({ writeConcern: 'majority' })) + .then(() => coll.drop({ writeConcern: { w: 'majority' } })) .catch(err => { if (!err.message.match(/ns not found/)) throw err; }) @@ -192,7 +192,7 @@ function prepareDatabaseForSuite(suite, context) { if (suite.key_vault_data) { const dataKeysCollection = context.sharedClient.db('keyvault').collection('datakeys'); return dataKeysCollection - .drop({ w: 'majority' }) + .drop({ writeConcern: { w: 'majority' } }) .catch(err => { if (!err.message.match(/ns not found/)) { throw err; @@ -200,13 +200,15 @@ function prepareDatabaseForSuite(suite, context) { }) .then(() => { if (suite.key_vault_data.length) { - return dataKeysCollection.insertMany(suite.key_vault_data, { w: 'majority' }); + return dataKeysCollection.insertMany(suite.key_vault_data, { + writeConcern: { w: 'majority' } + }); } }); } }) .then(() => { - const options = { w: 'majority' }; + const options = { writeConcern: { w: 'majority' } }; if (suite.json_schema) { options.validator = { $jsonSchema: suite.json_schema }; } @@ -215,7 +217,7 @@ function prepareDatabaseForSuite(suite, context) { }) .then(() => { if (suite.data && Array.isArray(suite.data) && suite.data.length > 0) { - return coll.insertMany(suite.data, { w: 'majority' }); + return coll.insertMany(suite.data, { writeConcern: { w: 'majority' } }); } }) .then(() => { diff --git a/test/functional/write_concern.test.js b/test/functional/write_concern.test.js index 6bddecf6781..7e1bc5b1158 100644 --- a/test/functional/write_concern.test.js +++ b/test/functional/write_concern.test.js @@ -23,9 +23,8 @@ describe('Write Concern', function () { // TODO: once `read-write-concern/connection-string` spec tests are implemented these can likely be removed describe('test journal connection string option', function () { function journalOptionTest(client, events, done) { - expect(client).to.have.nested.property('s.options'); - const clientOptions = client.s.options; - expect(clientOptions).to.containSubset({ j: true }); + expect(client).to.have.nested.property('s.options.writeConcern'); + expect(client.s.options.writeConcern).to.satisfy(wc => wc.j || wc.journal); client .db('test') .collection('test') @@ -46,7 +45,11 @@ describe('Write Concern', function () { // baseline to confirm client option is working it( 'should set write concern with j: true client option', - withMonitoredClient('insert', { clientOptions: { j: true } }, journalOptionTest) + withMonitoredClient( + 'insert', + { clientOptions: { writeConcern: { j: true } } }, + journalOptionTest + ) ); // ensure query option in connection string passes through diff --git a/test/tools/runner/config.js b/test/tools/runner/config.js index 3d7b10020e1..9fae11607c5 100644 --- a/test/tools/runner/config.js +++ b/test/tools/runner/config.js @@ -109,6 +109,12 @@ class NativeConfiguration { Object.assign(dbOptions, { replicaSet: this.options.replicaSet, auto_reconnect: false }); } + // Flatten any options nested under `writeConcern` before we make the connection string + if (dbOptions.writeConcern) { + Object.assign(dbOptions, dbOptions.writeConcern); + delete dbOptions.writeConcern; + } + const urlOptions = { protocol: 'mongodb', slashes: true, @@ -211,10 +217,10 @@ class NativeConfiguration { writeConcernMax() { if (this.topologyType !== TopologyType.Single) { - return { w: 'majority', wtimeout: 30000 }; + return { writeConcern: { w: 'majority', wtimeout: 30000 } }; } - return { w: 1 }; + return { writeConcern: { w: 1 } }; } // Accessors and methods Client-Side Encryption From 5d1c6faf49142f1cf4e0c10e54eb0d3cd4339904 Mon Sep 17 00:00:00 2001 From: Hana Pearlman Date: Fri, 20 Nov 2020 13:07:11 -0500 Subject: [PATCH 3/5] update some tests to use new wc option --- src/operations/map_reduce.ts | 3 - test/examples/transactions.js | 12 +++- test/functional/aggregation.test.js | 27 +++++---- test/functional/apm.test.js | 8 ++- test/functional/causal_consistency.test.js | 2 +- test/functional/change_stream.test.js | 32 +++++----- test/functional/change_stream_spec.test.js | 4 +- test/functional/collection.test.js | 37 +++++++----- test/functional/connection.test.js | 50 +++++++++------- test/functional/cursor.test.js | 68 ++++++++++++---------- test/functional/spec-runner/index.js | 11 +++- 11 files changed, 148 insertions(+), 106 deletions(-) diff --git a/src/operations/map_reduce.ts b/src/operations/map_reduce.ts index c6d36cfc8f0..37e6ad0794d 100644 --- a/src/operations/map_reduce.ts +++ b/src/operations/map_reduce.ts @@ -23,9 +23,6 @@ const exclusionList = [ 'readConcern', 'session', 'bypassDocumentValidation', - 'w', - 'wtimeout', - 'j', 'writeConcern', 'raw', 'fieldsAsRaw', diff --git a/test/examples/transactions.js b/test/examples/transactions.js index 1d3df7dc281..a32281e146e 100644 --- a/test/examples/transactions.js +++ b/test/examples/transactions.js @@ -234,9 +234,15 @@ describe('examples(transactions):', function () { // Prereq: Create collections. - await client.db('mydb1').collection('foo').insertOne({ abc: 0 }, { w: 'majority' }); - - await client.db('mydb2').collection('bar').insertOne({ xyz: 0 }, { w: 'majority' }); + await client + .db('mydb1') + .collection('foo') + .insertOne({ abc: 0 }, { writeConcern: { w: 'majority' } }); + + await client + .db('mydb2') + .collection('bar') + .insertOne({ xyz: 0 }, { writeConcern: { w: 'majority' } }); // Step 1: Start a Client Session const session = client.startSession(); diff --git a/test/functional/aggregation.test.js b/test/functional/aggregation.test.js index a89d09dd5d8..388855b368a 100644 --- a/test/functional/aggregation.test.js +++ b/test/functional/aggregation.test.js @@ -53,7 +53,7 @@ describe('Aggregation', function () { // Create a collection var collection = db.collection('shouldCorrectlyExecuteSimpleAggregationPipelineUsingArray'); // Insert the docs - collection.insert(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(result).to.exist; expect(err).to.not.exist; @@ -172,7 +172,7 @@ describe('Aggregation', function () { 'shouldCorrectlyExecuteSimpleAggregationPipelineUsingArguments' ); // Insert the docs - collection.insert(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(result).to.exist; expect(err).to.not.exist; @@ -260,7 +260,7 @@ describe('Aggregation', function () { 'shouldCorrectlyExecuteSimpleAggregationPipelineUsingArguments' ); // Insert the docs - collection.insert(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(result).to.exist; expect(err).to.not.exist; @@ -346,7 +346,7 @@ describe('Aggregation', function () { // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs - collection.insert(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(err).to.not.exist; expect(result).to.exist; @@ -431,7 +431,7 @@ describe('Aggregation', function () { // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs - collection.insert(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(result).to.exist; expect(err).to.not.exist; @@ -520,7 +520,7 @@ describe('Aggregation', function () { // Create a collection const collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs - collection.insert(docs, { w: 1 }, (err, result) => { + collection.insert(docs, { writeConcern: { w: 1 } }, (err, result) => { expect(result).to.exist; expect(err).to.not.exist; @@ -609,7 +609,7 @@ describe('Aggregation', function () { // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs - collection.insert(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(result).to.exist; expect(err).to.not.exist; @@ -694,7 +694,7 @@ describe('Aggregation', function () { // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs - collection.insert(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(result).to.exist; expect(err).to.not.exist; @@ -890,7 +890,7 @@ describe('Aggregation', function () { // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGetStream'); // Insert the docs - collection.insert(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(result).to.exist; expect(err).to.not.exist; @@ -1033,7 +1033,7 @@ describe('Aggregation', function () { // Create a collection const collection = db.collection('shouldCorrectlyDoAggWithCursorMaxTimeMSSet'); // Insert the docs - collection.insert(docs, { w: 1 }, (err, result) => { + collection.insert(docs, { writeConcern: { w: 1 } }, (err, result) => { expect(result).to.exist; expect(err).to.not.exist; @@ -1203,7 +1203,7 @@ describe('Aggregation', function () { // Create a collection var collection = db.collection('shouldCorrectlyQueryUsingISODate'); // Insert the docs - collection.insertMany(docs, { w: 1 }, function (err, result) { + collection.insertMany(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(result).to.exist; expect(err).to.not.exist; @@ -1255,7 +1255,10 @@ describe('Aggregation', function () { // Create a collection var collection = db.collection('shouldCorrectlyQueryUsingISODate3'); // Insert the docs - collection.insertMany([{ a: 1 }, { b: 1 }], { w: 1 }, function (err, result) { + collection.insertMany([{ a: 1 }, { b: 1 }], { writeConcern: { w: 1 } }, function ( + err, + result + ) { expect(result).to.exist; expect(err).to.not.exist; diff --git a/test/functional/apm.test.js b/test/functional/apm.test.js index 977767b524e..709732d4f8e 100644 --- a/test/functional/apm.test.js +++ b/test/functional/apm.test.js @@ -358,7 +358,9 @@ describe('APM', function () { // Insert test documents return db .collection('apm_test_2') - .insertMany([{ a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }], { w: 1 }); + .insertMany([{ a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }], { + writeConcern: { w: 1 } + }); }) .then(r => { expect(r.insertedCount).to.equal(6); @@ -529,7 +531,9 @@ describe('APM', function () { .then(() => db .collection('apm_test_2') - .insertMany([{ a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }], { w: 1 }) + .insertMany([{ a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }], { + writeConcern: { w: 1 } + }) ) .then(r => { expect(r.insertedCount).to.equal(6); diff --git a/test/functional/causal_consistency.test.js b/test/functional/causal_consistency.test.js index a31af8f60a3..fe0951d87e9 100644 --- a/test/functional/causal_consistency.test.js +++ b/test/functional/causal_consistency.test.js @@ -202,7 +202,7 @@ describe('Causal Consistency', function () { return db .collection('causal_test') - .insert({}, { session: session, w: 0 }) + .insert({}, { session: session, writeConcern: { w: 0 } }) .then(() => { expect(session.operationTime).to.be.null; }); diff --git a/test/functional/change_stream.test.js b/test/functional/change_stream.test.js index f5708b81972..9914c12b2ac 100644 --- a/test/functional/change_stream.test.js +++ b/test/functional/change_stream.test.js @@ -31,14 +31,18 @@ function withChangeStream(dbName, collectionName, callback) { return withClient((client, done) => { const db = client.db(dbName); db.dropCollection(collectionName, () => { - db.createCollection(collectionName, { w: 'majority' }, (err, collection) => { - if (err) return done(err); - withCursor( - collection.watch(), - (cursor, done) => callback(collection, cursor, done), - err => collection.drop(dropErr => done(err || dropErr)) - ); - }); + db.createCollection( + collectionName, + { writeConcern: { w: 'majority' } }, + (err, collection) => { + if (err) return done(err); + withCursor( + collection.watch(), + (cursor, done) => callback(collection, cursor, done), + err => collection.drop(dropErr => done(err || dropErr)) + ); + } + ); }); }); } @@ -2582,7 +2586,7 @@ describe('Change Streams', function () { coll = client.db('integration_tests').collection('setupAfterTest'); const changeStream = coll.watch(); waitForStarted(changeStream, () => { - coll.insertOne({ x: 1 }, { w: 'majority', j: true }, err => { + coll.insertOne({ x: 1 }, { writeConcern: { w: 'majority', j: true } }, err => { expect(err).to.not.exist; coll.drop(err => { @@ -2610,7 +2614,7 @@ describe('Change Streams', function () { const changeStream = coll.watch([], { startAfter }); this.defer(() => changeStream.close()); - coll.insertOne({ x: 2 }, { w: 'majority', j: true }, err => { + coll.insertOne({ x: 2 }, { writeConcern: { w: 'majority', j: true } }, err => { expect(err).to.not.exist; changeStream.once('change', change => { expect(change).to.containSubset({ @@ -2630,7 +2634,7 @@ describe('Change Streams', function () { const changeStream = coll.watch([], { startAfter }); this.defer(() => changeStream.close()); - coll.insertOne({ x: 2 }, { w: 'majority', j: true }, err => { + coll.insertOne({ x: 2 }, { writeConcern: { w: 'majority', j: true } }, err => { expect(err).to.not.exist; exhaust(changeStream, (err, bag) => { expect(err).to.not.exist; @@ -2675,7 +2679,7 @@ describe('Change Streams', function () { waitForStarted(changeStream, () => { triggerResumableError(changeStream, () => { events.push('error'); - coll.insertOne({ x: 2 }, { w: 'majority', j: true }); + coll.insertOne({ x: 2 }, { writeConcern: { w: 'majority', j: true } }); }); }); } @@ -2715,8 +2719,8 @@ describe('Change Streams', function () { waitForStarted(changeStream, () => this.defer( coll - .insertOne({ x: 2 }, { w: 'majority', j: true }) - .then(() => coll.insertOne({ x: 3 }, { w: 'majority', j: true })) + .insertOne({ x: 2 }, { writeConcern: { w: 'majority', j: true } }) + .then(() => coll.insertOne({ x: 3 }, { writeConcern: { w: 'majority', j: true } })) ) ); } diff --git a/test/functional/change_stream_spec.test.js b/test/functional/change_stream_spec.test.js index 6599b7b0d38..57a47239992 100644 --- a/test/functional/change_stream_spec.test.js +++ b/test/functional/change_stream_spec.test.js @@ -37,7 +37,9 @@ describe('Change Stream Spec', function () { const sDB = suite.database_name; const sColl = suite.collection_name; const configuration = this.configuration; - return Promise.all(ALL_DBS.map(db => gc.db(db).dropDatabase({ w: 'majority' }))) + return Promise.all( + ALL_DBS.map(db => gc.db(db).dropDatabase({ writeConcern: { w: 'majority' } })) + ) .then(() => gc.db(sDB).createCollection(sColl)) .then(() => { if (suite.database2_name && suite.collection2_name) { diff --git a/test/functional/collection.test.js b/test/functional/collection.test.js index d617ad23332..fae7d1eef2d 100644 --- a/test/functional/collection.test.js +++ b/test/functional/collection.test.js @@ -493,23 +493,28 @@ describe('Collection', function () { expect(err).to.not.exist; // Index name happens to be the same as collection name - db.createIndex(testCollection, 'collection_124', { w: 1 }, (err, indexName) => { - expect(err).to.not.exist; - expect(indexName).to.equal('collection_124_1'); - - db.listCollections().toArray((err, documents) => { + db.createIndex( + testCollection, + 'collection_124', + { writeConcern: { w: 1 } }, + (err, indexName) => { expect(err).to.not.exist; - expect(documents.length > 1).to.be.true; - let found = false; + expect(indexName).to.equal('collection_124_1'); - documents.forEach(document => { - if (document.name === testCollection) found = true; - }); + db.listCollections().toArray((err, documents) => { + expect(err).to.not.exist; + expect(documents.length > 1).to.be.true; + let found = false; - expect(found).to.be.true; - done(); - }); - }); + documents.forEach(document => { + if (document.name === testCollection) found = true; + }); + + expect(found).to.be.true; + done(); + }); + } + ); }); }); @@ -630,7 +635,7 @@ describe('Collection', function () { ) { collection.createIndex( { createdAt: 1 }, - { expireAfterSeconds: 1, w: 1 }, + { expireAfterSeconds: 1, writeConcern: { w: 1 } }, errorCallBack ); } else if ( @@ -638,7 +643,7 @@ describe('Collection', function () { ) { collection.ensureIndex( { createdAt: 1 }, - { expireAfterSeconds: 1, w: 1 }, + { expireAfterSeconds: 1, writeConcern: { w: 1 } }, errorCallBack ); } diff --git a/test/functional/connection.test.js b/test/functional/connection.test.js index 403d5ef4453..2c29d8d6b47 100644 --- a/test/functional/connection.test.js +++ b/test/functional/connection.test.js @@ -42,18 +42,22 @@ describe('Connection', function () { var db = client.db(configuration.db); expect(err).to.not.exist; - db.collection('domainSocketCollection0').insert({ a: 1 }, { w: 1 }, function (err) { - expect(err).to.not.exist; + db.collection('domainSocketCollection0').insert( + { a: 1 }, + { writeConcern: { w: 1 } }, + function (err) { + expect(err).to.not.exist; - db.collection('domainSocketCollection0') - .find({ a: 1 }) - .toArray(function (err, items) { - expect(err).to.not.exist; - test.equal(1, items.length); + db.collection('domainSocketCollection0') + .find({ a: 1 }) + .toArray(function (err, items) { + expect(err).to.not.exist; + test.equal(1, items.length); - client.close(done); - }); - }); + client.close(done); + }); + } + ); }); } }); @@ -106,18 +110,22 @@ describe('Connection', function () { var db = client.db(configuration.db); expect(err).to.not.exist; - db.collection('domainSocketCollection1').insert({ x: 1 }, { w: 1 }, function (err) { - expect(err).to.not.exist; + db.collection('domainSocketCollection1').insert( + { x: 1 }, + { writeConcern: { w: 1 } }, + function (err) { + expect(err).to.not.exist; - db.collection('domainSocketCollection1') - .find({ x: 1 }) - .toArray(function (err, items) { - expect(err).to.not.exist; - test.equal(1, items.length); + db.collection('domainSocketCollection1') + .find({ x: 1 }) + .toArray(function (err, items) { + expect(err).to.not.exist; + test.equal(1, items.length); - client.close(done); - }); - }); + client.close(done); + }); + } + ); }); } }); @@ -135,7 +143,7 @@ describe('Connection', function () { db.collection(testName, function (err, collection) { expect(err).to.not.exist; - collection.insert({ foo: 123 }, { w: 1 }, function (err) { + collection.insert({ foo: 123 }, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; db.dropDatabase(function (err, dropped) { diff --git a/test/functional/cursor.test.js b/test/functional/cursor.test.js index 2e45145bd0b..dc351f7be88 100644 --- a/test/functional/cursor.test.js +++ b/test/functional/cursor.test.js @@ -2103,47 +2103,51 @@ describe('Cursor', function () { var count = 100; // Just hammer the server for (var i = 0; i < 100; i++) { - collection.insert({ id: i }, { w: 'majority', wtimeout: 5000 }, err => { - expect(err).to.not.exist; - count = count - 1; + collection.insert( + { id: i }, + { writeConcern: { w: 'majority', wtimeout: 5000 } }, + err => { + expect(err).to.not.exist; + count = count - 1; - if (count === 0) { - const cursor = collection.find({}, { tailable: true, awaitData: true }); - const stream = cursor.stream(); - // let index = 0; - stream.resume(); + if (count === 0) { + const cursor = collection.find({}, { tailable: true, awaitData: true }); + const stream = cursor.stream(); + // let index = 0; + stream.resume(); - stream.on('error', err => { - expect(err).to.exist; - errorOccurred = true; - }); + stream.on('error', err => { + expect(err).to.exist; + errorOccurred = true; + }); - var validator = () => { - closeCount++; - if (closeCount === 2) { - expect(errorOccurred).to.equal(true); - done(); - } - }; + var validator = () => { + closeCount++; + if (closeCount === 2) { + expect(errorOccurred).to.equal(true); + done(); + } + }; - stream.on('end', validator); - cursor.on('close', validator); + stream.on('end', validator); + cursor.on('close', validator); - // Just hammer the server - for (var i = 0; i < 100; i++) { - const id = i; - process.nextTick(function () { - collection.insert({ id }, err => { - expect(err).to.not.exist; + // Just hammer the server + for (var i = 0; i < 100; i++) { + const id = i; + process.nextTick(function () { + collection.insert({ id }, err => { + expect(err).to.not.exist; - if (id === 99) { - setTimeout(() => client.close()); - } + if (id === 99) { + setTimeout(() => client.close()); + } + }); }); - }); + } } } - }); + ); } }); }); diff --git a/test/functional/spec-runner/index.js b/test/functional/spec-runner/index.js index 07d9904b350..fba18884bdc 100644 --- a/test/functional/spec-runner/index.js +++ b/test/functional/spec-runner/index.js @@ -35,7 +35,16 @@ function isPlainObject(value) { function translateClientOptions(options) { Object.keys(options).forEach(key => { - if (key === 'readConcernLevel') { + if (['j', 'journal', 'fsync', 'wtimeout', 'wtimeoutms'].indexOf(key) >= 0) { + throw new Error( + `Unhandled write concern key needs to be added to options.writeConcern: ${key}` + ); + } + + if (key === 'w') { + options.writeConcern = { w: options.w }; + delete options[key]; + } else if (key === 'readConcernLevel') { options.readConcern = { level: options.readConcernLevel }; delete options[key]; } else if (key === 'autoEncryptOpts') { From 8f237769bc8fadc0c28b6e8bb0312373e8479810 Mon Sep 17 00:00:00 2001 From: Hana Pearlman Date: Fri, 20 Nov 2020 14:56:21 -0500 Subject: [PATCH 4/5] update tests to use new wc shape --- src/bulk/common.ts | 2 +- .../client_side_encryption/prose.test.js | 4 +- test/functional/connections_stepdown.test.js | 6 +- test/functional/crud_api.test.js | 268 +++++++++++------- test/functional/cursor.test.js | 6 +- test/functional/cursorstream.test.js | 10 +- test/functional/custom_pk.test.js | 2 +- test/functional/document_validation.test.js | 58 ++-- test/functional/error.test.js | 14 +- test/functional/find.test.js | 83 +++--- test/functional/generator_based.test.js | 2 +- test/functional/index.test.js | 141 +++++---- test/functional/insert.test.js | 71 ++--- test/functional/maxtimems.test.js | 6 +- test/functional/object_id.test.js | 36 +-- test/functional/operation_example.test.js | 255 +++++++++-------- .../operation_generators_example.test.js | 100 ++++--- .../operation_promises_example.test.js | 98 ++++--- test/functional/promises_collection.test.js | 6 +- test/functional/raw.test.js | 86 +++--- test/functional/remove.test.js | 64 +++-- test/functional/sessions.test.js | 4 +- test/functional/unicode.test.js | 40 ++- test/tools/runner/config.js | 2 +- 24 files changed, 771 insertions(+), 593 deletions(-) diff --git a/src/bulk/common.ts b/src/bulk/common.ts index 8d29804b07d..fd0ce45cfc8 100644 --- a/src/bulk/common.ts +++ b/src/bulk/common.ts @@ -1044,7 +1044,7 @@ export abstract class BulkOperationBase { * bulkOp.find({ h: 8 }).delete(); * * // Add a replaceOne - * bulkOp.find({ i: 9 }).replaceOne({ j: 10 }); + * bulkOp.find({ i: 9 }).replaceOne({writeConcern: { j: 10 }}); * * // Update using a pipeline (requires Mongodb 4.2 or higher) * bulk.find({ k: 11, y: { $exists: true }, z: { $exists: true } }).updateOne([ diff --git a/test/functional/client_side_encryption/prose.test.js b/test/functional/client_side_encryption/prose.test.js index a34ae4c1988..3bdf4b9f106 100644 --- a/test/functional/client_side_encryption/prose.test.js +++ b/test/functional/client_side_encryption/prose.test.js @@ -478,7 +478,7 @@ describe('Client Side Encryption Prose Tests', function () { return this.client .db(keyVaultDbName) .collection(keyVaultCollName) - .insertOne(limitsKey, { w: 'majority' }); + .insertOne(limitsKey, { writeConcern: { w: 'majority' } }); }) ); }); @@ -735,7 +735,7 @@ describe('Client Side Encryption Prose Tests', function () { return this.client .db(keyVaultDbName) .collection(keyVaultCollName) - .insertOne(externalKey, { w: 'majority' }); + .insertOne(externalKey, { writeConcern: { w: 'majority' } }); }) ); }); diff --git a/test/functional/connections_stepdown.test.js b/test/functional/connections_stepdown.test.js index 4ff702a88c8..c3370e8e251 100644 --- a/test/functional/connections_stepdown.test.js +++ b/test/functional/connections_stepdown.test.js @@ -56,9 +56,9 @@ describe('Connections survive primary step down', function () { db = client.db('step-down'); collection = db.collection('step-down'); }) - .then(() => collection.drop({ w: 'majority' })) + .then(() => collection.drop({ writeConcern: { w: 'majority' } })) .catch(ignoreNsNotFound) - .then(() => db.createCollection('step-down', { w: 'majority' })); + .then(() => db.createCollection('step-down', { writeConcern: { w: 'majority' } })); }); let deferred = []; @@ -75,7 +75,7 @@ describe('Connections survive primary step down', function () { test: function () { return collection .insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }], { - w: 'majority' + writeConcern: { w: 'majority' } }) .then(result => expect(result.insertedCount).to.equal(5)) .then(() => { diff --git a/test/functional/crud_api.test.js b/test/functional/crud_api.test.js index de0a6e9589d..48e7eae772e 100644 --- a/test/functional/crud_api.test.js +++ b/test/functional/crud_api.test.js @@ -313,7 +313,7 @@ describe('CRUD API', function () { // Insert one method // ------------------------------------------------- var insertOne = function () { - db.collection('t2_3').insertOne({ a: 1 }, { w: 1 }, function (err, r) { + db.collection('t2_3').insertOne({ a: 1 }, { writeConcern: { w: 1 } }, function (err, r) { expect(err).to.not.exist; expect(r).property('insertedCount').to.equal(1); test.equal(1, r.insertedCount); @@ -328,7 +328,7 @@ describe('CRUD API', function () { // ------------------------------------------------- var insertMany = function () { var docs = [{ a: 1 }, { a: 1 }]; - db.collection('t2_4').insertMany(docs, { w: 1 }, function (err, r) { + db.collection('t2_4').insertMany(docs, { writeConcern: { w: 1 } }, function (err, r) { expect(err).to.not.exist; test.equal(2, r.result.n); test.equal(2, r.insertedCount); @@ -343,7 +343,10 @@ describe('CRUD API', function () { // Bulk write method unordered // ------------------------------------------------- var bulkWriteUnOrdered = function () { - db.collection('t2_5').insertMany([{ c: 1 }], { w: 1 }, function (err, r) { + db.collection('t2_5').insertMany([{ c: 1 }], { writeConcern: { w: 1 } }, function ( + err, + r + ) { expect(err).to.not.exist; expect(r).property('insertedCount').to.equal(1); @@ -356,7 +359,7 @@ describe('CRUD API', function () { { deleteOne: { q: { c: 1 } } }, { deleteMany: { q: { c: 1 } } } ], - { ordered: false, w: 1 }, + { ordered: false, writeConcern: { w: 1 } }, function (err, r) { if (err) console.dir(err); expect(err).to.not.exist; @@ -383,49 +386,53 @@ describe('CRUD API', function () { // Bulk write method unordered // ------------------------------------------------- var bulkWriteUnOrderedSpec = function () { - db.collection('t2_6').insertMany([{ c: 1 }, { c: 2 }, { c: 3 }], { w: 1 }, function ( - err, - r - ) { - expect(err).to.not.exist; - test.equal(3, r.result.n); - - db.collection('t2_6').bulkWrite( - [ - { insertOne: { document: { a: 1 } } }, - { updateOne: { filter: { a: 2 }, update: { $set: { a: 2 } }, upsert: true } }, - { updateMany: { filter: { a: 3 }, update: { $set: { a: 3 } }, upsert: true } }, - { deleteOne: { filter: { c: 1 } } }, - { deleteMany: { filter: { c: 2 } } }, - { replaceOne: { filter: { c: 3 }, replacement: { c: 4 }, upsert: true } } - ], - { ordered: false, w: 1 }, - function (err, r) { - expect(err).to.not.exist; - test.equal(1, r.nInserted); - test.equal(2, r.nUpserted); - test.equal(2, r.nRemoved); - - // Crud fields - test.equal(1, r.insertedCount); - test.equal(1, Object.keys(r.insertedIds).length); - test.equal(1, r.matchedCount); - test.equal(2, r.deletedCount); - test.equal(2, r.upsertedCount); - test.equal(2, Object.keys(r.upsertedIds).length); + db.collection('t2_6').insertMany( + [{ c: 1 }, { c: 2 }, { c: 3 }], + { writeConcern: { w: 1 } }, + function (err, r) { + expect(err).to.not.exist; + test.equal(3, r.result.n); + + db.collection('t2_6').bulkWrite( + [ + { insertOne: { document: { a: 1 } } }, + { updateOne: { filter: { a: 2 }, update: { $set: { a: 2 } }, upsert: true } }, + { updateMany: { filter: { a: 3 }, update: { $set: { a: 3 } }, upsert: true } }, + { deleteOne: { filter: { c: 1 } } }, + { deleteMany: { filter: { c: 2 } } }, + { replaceOne: { filter: { c: 3 }, replacement: { c: 4 }, upsert: true } } + ], + { ordered: false, writeConcern: { w: 1 } }, + function (err, r) { + expect(err).to.not.exist; + test.equal(1, r.nInserted); + test.equal(2, r.nUpserted); + test.equal(2, r.nRemoved); - // Ordered bulk operation - bulkWriteOrdered(); - } - ); - }); + // Crud fields + test.equal(1, r.insertedCount); + test.equal(1, Object.keys(r.insertedIds).length); + test.equal(1, r.matchedCount); + test.equal(2, r.deletedCount); + test.equal(2, r.upsertedCount); + test.equal(2, Object.keys(r.upsertedIds).length); + + // Ordered bulk operation + bulkWriteOrdered(); + } + ); + } + ); }; // // Bulk write method ordered // ------------------------------------------------- var bulkWriteOrdered = function () { - db.collection('t2_7').insertMany([{ c: 1 }], { w: 1 }, function (err, r) { + db.collection('t2_7').insertMany([{ c: 1 }], { writeConcern: { w: 1 } }, function ( + err, + r + ) { expect(err).to.not.exist; expect(r).property('insertedCount').to.equal(1); @@ -438,7 +445,7 @@ describe('CRUD API', function () { { deleteOne: { q: { c: 1 } } }, { deleteMany: { q: { c: 1 } } } ], - { ordered: true, w: 1 }, + { ordered: true, writeConcern: { w: 1 } }, function (err, r) { expect(err).to.not.exist; test.equal(3, r.nInserted); @@ -463,7 +470,10 @@ describe('CRUD API', function () { // Bulk write method ordered // ------------------------------------------------- var bulkWriteOrderedCrudSpec = function () { - db.collection('t2_8').insertMany([{ c: 1 }], { w: 1 }, function (err, r) { + db.collection('t2_8').insertMany([{ c: 1 }], { writeConcern: { w: 1 } }, function ( + err, + r + ) { expect(err).to.not.exist; expect(r).property('insertedCount').to.equal(1); @@ -476,7 +486,7 @@ describe('CRUD API', function () { { deleteMany: { filter: { c: 1 } } }, { replaceOne: { filter: { c: 3 }, replacement: { c: 4 }, upsert: true } } ], - { ordered: true, w: 1 }, + { ordered: true, writeConcern: { w: 1 } }, function (err, r) { // expect(err).to.not.exist; test.equal(1, r.nInserted); @@ -534,7 +544,10 @@ describe('CRUD API', function () { // Update one method // ------------------------------------------------- var updateOne = function () { - db.collection('t3_2').insertMany([{ c: 1 }], { w: 1 }, function (err, r) { + db.collection('t3_2').insertMany([{ c: 1 }], { writeConcern: { w: 1 } }, function ( + err, + r + ) { expect(err).to.not.exist; expect(r).property('insertedCount').to.equal(1); @@ -591,35 +604,39 @@ describe('CRUD API', function () { // Update many method // ------------------------------------------------- var updateMany = function () { - db.collection('t3_4').insertMany([{ a: 1 }, { a: 1 }], { w: 1 }, function (err, r) { - expect(err).to.not.exist; - expect(r).property('insertedCount').to.equal(2); + db.collection('t3_4').insertMany( + [{ a: 1 }, { a: 1 }], + { writeConcern: { w: 1 } }, + function (err, r) { + expect(err).to.not.exist; + expect(r).property('insertedCount').to.equal(2); - db.collection('t3_4').updateMany( - { a: 1 }, - { $set: { a: 2 } }, - { upsert: true, w: 1 }, - function (err, r) { - expect(err).to.not.exist; - expect(r).property('modifiedCount').to.equal(2); - test.equal(2, r.matchedCount); - test.ok(r.upsertedId == null); - - db.collection('t3_4').updateMany( - { c: 1 }, - { $set: { d: 2 } }, - { upsert: true, w: 1 }, - function (err, r) { - expect(err).to.not.exist; - test.equal(0, r.matchedCount); - test.ok(r.upsertedId != null); + db.collection('t3_4').updateMany( + { a: 1 }, + { $set: { a: 2 } }, + { upsert: true, writeConcern: { w: 1 } }, + function (err, r) { + expect(err).to.not.exist; + expect(r).property('modifiedCount').to.equal(2); + test.equal(2, r.matchedCount); + test.ok(r.upsertedId == null); - client.close(done); - } - ); - } - ); - }); + db.collection('t3_4').updateMany( + { c: 1 }, + { $set: { d: 2 } }, + { upsert: true, writeConcern: { w: 1 } }, + function (err, r) { + expect(err).to.not.exist; + test.equal(0, r.matchedCount); + test.ok(r.upsertedId != null); + + client.close(done); + } + ); + } + ); + } + ); }; legacyUpdate(); @@ -644,51 +661,63 @@ describe('CRUD API', function () { // Legacy update method // ------------------------------------------------- var legacyRemove = function () { - db.collection('t4_1').insertMany([{ a: 1 }, { a: 1 }], { w: 1 }, function (err, r) { - expect(err).to.not.exist; - test.equal(2, r.insertedCount); - - db.collection('t4_1').remove({ a: 1 }, { single: true }, function (err, r) { + db.collection('t4_1').insertMany( + [{ a: 1 }, { a: 1 }], + { writeConcern: { w: 1 } }, + function (err, r) { expect(err).to.not.exist; - test.equal(1, r.deletedCount); + test.equal(2, r.insertedCount); - deleteOne(); - }); - }); + db.collection('t4_1').remove({ a: 1 }, { single: true }, function (err, r) { + expect(err).to.not.exist; + test.equal(1, r.deletedCount); + + deleteOne(); + }); + } + ); }; // // Update one method // ------------------------------------------------- var deleteOne = function () { - db.collection('t4_2').insertMany([{ a: 1 }, { a: 1 }], { w: 1 }, (err, r) => { - expect(err).to.not.exist; - expect(r).property('insertedCount').to.equal(2); - - db.collection('t4_2').deleteOne({ a: 1 }, (err, r) => { + db.collection('t4_2').insertMany( + [{ a: 1 }, { a: 1 }], + { writeConcern: { w: 1 } }, + (err, r) => { expect(err).to.not.exist; - expect(r).property('deletedCount').to.equal(1); + expect(r).property('insertedCount').to.equal(2); - deleteMany(); - }); - }); + db.collection('t4_2').deleteOne({ a: 1 }, (err, r) => { + expect(err).to.not.exist; + expect(r).property('deletedCount').to.equal(1); + + deleteMany(); + }); + } + ); }; // // Update many method // ------------------------------------------------- var deleteMany = function () { - db.collection('t4_3').insertMany([{ a: 1 }, { a: 1 }], { w: 1 }, (err, r) => { - expect(err).to.not.exist; - expect(r).property('insertedCount').to.equal(2); - - db.collection('t4_3').deleteMany({ a: 1 }, (err, r) => { + db.collection('t4_3').insertMany( + [{ a: 1 }, { a: 1 }], + { writeConcern: { w: 1 } }, + (err, r) => { expect(err).to.not.exist; - expect(r).property('deletedCount').to.equal(2); + expect(r).property('insertedCount').to.equal(2); - client.close(done); - }); - }); + db.collection('t4_3').deleteMany({ a: 1 }, (err, r) => { + expect(err).to.not.exist; + expect(r).property('deletedCount').to.equal(2); + + client.close(done); + }); + } + ); }; legacyRemove(); @@ -713,7 +742,10 @@ describe('CRUD API', function () { // findOneAndRemove method // ------------------------------------------------- var findOneAndRemove = function () { - db.collection('t5_1').insertMany([{ a: 1, b: 1 }], { w: 1 }, function (err, r) { + db.collection('t5_1').insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }, function ( + err, + r + ) { expect(err).to.not.exist; expect(r).property('insertedCount').to.equal(1); @@ -735,7 +767,10 @@ describe('CRUD API', function () { // findOneAndRemove method // ------------------------------------------------- var findOneAndReplace = function () { - db.collection('t5_2').insertMany([{ a: 1, b: 1 }], { w: 1 }, function (err, r) { + db.collection('t5_2').insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }, function ( + err, + r + ) { expect(err).to.not.exist; expect(r).property('insertedCount').to.equal(1); @@ -764,7 +799,10 @@ describe('CRUD API', function () { // findOneAndRemove method // ------------------------------------------------- var findOneAndUpdate = function () { - db.collection('t5_3').insertMany([{ a: 1, b: 1 }], { w: 1 }, function (err, r) { + db.collection('t5_3').insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }, function ( + err, + r + ) { expect(err).to.not.exist; expect(r).property('insertedCount').to.equal(1); @@ -833,27 +871,33 @@ describe('CRUD API', function () { expect(err).to.not.exist; var col = db.collection('shouldCorrectlyExecuteInsertOneWithW0'); - col.insertOne({ a: 1 }, { w: 0 }, function (err, result) { + col.insertOne({ a: 1 }, { writeConcern: { w: 0 } }, function (err, result) { expect(err).to.not.exist; test.equal(1, result.ok); - col.insertMany([{ a: 1 }], { w: 0 }, function (err, result) { + col.insertMany([{ a: 1 }], { writeConcern: { w: 0 } }, function (err, result) { expect(err).to.not.exist; expect(result).to.exist; - col.updateOne({ a: 1 }, { $set: { b: 1 } }, { w: 0 }, function (err, result) { + col.updateOne({ a: 1 }, { $set: { b: 1 } }, { writeConcern: { w: 0 } }, function ( + err, + result + ) { expect(err).to.not.exist; expect(result).to.exist; - col.updateMany({ a: 1 }, { $set: { b: 1 } }, { w: 0 }, function (err, result) { + col.updateMany({ a: 1 }, { $set: { b: 1 } }, { writeConcern: { w: 0 } }, function ( + err, + result + ) { expect(err).to.not.exist; expect(result).to.exist; - col.deleteOne({ a: 1 }, { w: 0 }, function (err, result) { + col.deleteOne({ a: 1 }, { writeConcern: { w: 0 } }, function (err, result) { expect(err).to.not.exist; expect(result).to.exist; - col.deleteMany({ a: 1 }, { w: 0 }, function (err, result) { + col.deleteMany({ a: 1 }, { writeConcern: { w: 0 } }, function (err, result) { expect(err).to.not.exist; expect(result).to.exist; @@ -885,7 +929,7 @@ describe('CRUD API', function () { db.collection('try').updateOne( { _id: 1 }, { $set: { x: 1 } }, - { upsert: true, w: 0 }, + { upsert: true, writeConcern: { w: 0 } }, function (err, r) { expect(err).to.not.exist; test.ok(r != null); @@ -955,7 +999,9 @@ describe('CRUD API', function () { var db = client.db(configuration.db); expect(err).to.not.exist; - db.collection('t20_1').bulkWrite(ops, { ordered: false, w: 1 }, function (err) { + db.collection('t20_1').bulkWrite(ops, { ordered: false, writeConcern: { w: 1 } }, function ( + err + ) { test.ok(err !== null); client.close(done); }); @@ -986,7 +1032,9 @@ describe('CRUD API', function () { var db = client.db(configuration.db); expect(err).to.not.exist; - db.collection('t20_1').bulkWrite(ops, { ordered: true, w: 1 }, function (err) { + db.collection('t20_1').bulkWrite(ops, { ordered: true, writeConcern: { w: 1 } }, function ( + err + ) { test.ok(err !== null); client.close(done); }); diff --git a/test/functional/cursor.test.js b/test/functional/cursor.test.js index dc351f7be88..8509ddcaa02 100644 --- a/test/functional/cursor.test.js +++ b/test/functional/cursor.test.js @@ -2918,7 +2918,7 @@ describe('Cursor', function () { const db = client.db(configuration.db); var col = db.collection('count_hint'); - col.insert([{ i: 1 }, { i: 2 }], { w: 1 }, err => { + col.insert([{ i: 1 }, { i: 2 }], { writeConcern: { w: 1 } }, err => { expect(err).to.not.exist; col.ensureIndex({ i: 1 }, err => { @@ -3482,7 +3482,7 @@ describe('Cursor', function () { ordered.insert({ a: i }); } - ordered.execute({ w: 1 }, err => { + ordered.execute({ writeConcern: { w: 1 } }, err => { expect(err).to.not.exist; // Let's attempt to skip and limit @@ -3572,7 +3572,7 @@ describe('Cursor', function () { ordered.insert({ a: i }); } - ordered.execute({ w: 1 }, err => { + ordered.execute({ writeConcern: { w: 1 } }, err => { expect(err).to.not.exist; // Let's attempt to skip and limit diff --git a/test/functional/cursorstream.test.js b/test/functional/cursorstream.test.js index 88403a18325..7ed94069a21 100644 --- a/test/functional/cursorstream.test.js +++ b/test/functional/cursorstream.test.js @@ -39,7 +39,7 @@ describe('Cursor Streams', function () { ) { var left = allDocs.length; for (var i = 0; i < allDocs.length; i++) { - collection.insert(allDocs[i], { w: 1 }, function (err) { + collection.insert(allDocs[i], { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; left = left - 1; @@ -114,7 +114,7 @@ describe('Cursor Streams', function () { ) { var left = allDocs.length; for (var i = 0; i < allDocs.length; i++) { - collection.insert(allDocs[i], { w: 1 }, function (err) { + collection.insert(allDocs[i], { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; left = left - 1; @@ -181,7 +181,7 @@ describe('Cursor Streams', function () { err, collection ) { - collection.insert(docs, { w: 1 }, function (err) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; // Perform a find to get a cursor @@ -233,7 +233,7 @@ describe('Cursor Streams', function () { 'test_streaming_function_with_limit_for_fetching_update' ); - collection.insert(docs, { w: 1 }, function (err) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; const stream = collection.find({}).stream(); @@ -258,7 +258,7 @@ describe('Cursor Streams', function () { updateCollection.updateMany( { id: 1 }, { $inc: { count: 1 } }, - { w: 1, upsert: true }, + { writeConcern: { w: 1 }, upsert: true }, function (err) { expect(err).to.not.exist; stream.resume(); diff --git a/test/functional/custom_pk.test.js b/test/functional/custom_pk.test.js index 47169aa7e5f..188b1f3898e 100644 --- a/test/functional/custom_pk.test.js +++ b/test/functional/custom_pk.test.js @@ -37,7 +37,7 @@ describe('Custom PK', function () { var db = client.db(configuration.db); var collection = db.collection('test_custom_key'); - collection.insert({ a: 1 }, { w: 1 }, function (err) { + collection.insert({ a: 1 }, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; collection.find({ _id: new ObjectId('aaaaaaaaaaaa') }).toArray(function (err, items) { expect(items.length).to.equal(1); diff --git a/test/functional/document_validation.test.js b/test/functional/document_validation.test.js index a87f8096106..1133f0639b8 100644 --- a/test/functional/document_validation.test.js +++ b/test/functional/document_validation.test.js @@ -303,36 +303,40 @@ describe('Document Validation', function () { expect(err).to.not.exist; // Insert the docs - col.insertMany(docs, { w: 1, bypassDocumentValidation: true }, function (err) { - expect(err).to.not.exist; + col.insertMany( + docs, + { writeConcern: { w: 1 }, bypassDocumentValidation: true }, + function (err) { + expect(err).to.not.exist; - // Execute aggregate, notice the pipeline is expressed as an Array - const cursor = col.aggregate( - [ - { - $project: { - author: 1, - tags: 1 - } - }, - { $unwind: '$tags' }, - { - $group: { - _id: { tags: '$tags' }, - authors: { $addToSet: '$author' } - } - }, - { $out: 'createValidationCollectionOut' } - ], - { bypassDocumentValidation: true } - ); + // Execute aggregate, notice the pipeline is expressed as an Array + const cursor = col.aggregate( + [ + { + $project: { + author: 1, + tags: 1 + } + }, + { $unwind: '$tags' }, + { + $group: { + _id: { tags: '$tags' }, + authors: { $addToSet: '$author' } + } + }, + { $out: 'createValidationCollectionOut' } + ], + { bypassDocumentValidation: true } + ); - cursor.toArray(function (err) { - expect(err).to.not.exist; + cursor.toArray(function (err) { + expect(err).to.not.exist; - client.close(done); - }); - }); + client.close(done); + }); + } + ); } ); }); diff --git a/test/functional/error.test.js b/test/functional/error.test.js index dd0d561fd0e..a7d4239c65d 100644 --- a/test/functional/error.test.js +++ b/test/functional/error.test.js @@ -30,14 +30,14 @@ describe('Errors', function () { unique: true } ], - { w: 1 }, + { writeConcern: { w: 1 } }, err => { expect(err).to.not.exist; - collection.insertOne({ a: 2 }, { w: 1 }, err => { + collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => { expect(err).to.not.exist; - collection.insertOne({ a: 2 }, { w: 1 }, err => { + collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => { expect(err.code).to.equal(11000); done(); }); @@ -60,13 +60,13 @@ describe('Errors', function () { unique: true } ], - { w: 1 }, + { writeConcern: { w: 1 } }, err => { expect(err).to.not.exist; - collection.insertOne({ a: 2 }, { w: 1 }, err => { + collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => { expect(err).to.not.exist; - collection.insertOne({ a: 2 }, { w: 1 }, err => { + collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => { expect(err.code).to.equal(11000); done(); }); @@ -88,7 +88,7 @@ describe('Errors', function () { test: function (done) { const db = client.db(this.configuration.db); const c = db.collection('test_error_object_should_include_message'); - c.insertOne({ a: 2, b: 5 }, { w: 1 }, err => { + c.insertOne({ a: 2, b: 5 }, { writeConcern: { w: 1 } }, err => { expect(err).to.not.exist; c.findOne({ a: 2 }, { projection: { a: 1, b: 0 } }, err => { expect(PROJECTION_ERRORS).to.include(err.errmsg); diff --git a/test/functional/find.test.js b/test/functional/find.test.js index dc51bb476fd..613c3b648dd 100644 --- a/test/functional/find.test.js +++ b/test/functional/find.test.js @@ -673,17 +673,18 @@ describe('Find', function () { collection.insert(doc, configuration.writeConcernMax(), function (err) { expect(err).to.not.exist; - collection.findOne({ _id: doc._id }, { w: 1, projection: undefined }, function ( - err, - doc - ) { - expect(err).to.not.exist; - test.equal(2, doc.comments.length); - test.equal('number 1', doc.comments[0].title); - test.equal('number 2', doc.comments[1].title); + collection.findOne( + { _id: doc._id }, + { writeConcern: { w: 1 }, projection: undefined }, + function (err, doc) { + expect(err).to.not.exist; + test.equal(2, doc.comments.length); + test.equal('number 1', doc.comments[0].title); + test.equal('number 2', doc.comments[1].title); - client.close(done); - }); + client.close(done); + } + ); }); }); }); @@ -1003,7 +1004,9 @@ describe('Find', function () { var db = client.db(configuration.db); db.createCollection('FindAndModifyDuplicateKeyError', function (err, collection) { expect(err).to.not.exist; - collection.ensureIndex(['name', 1], { unique: true, w: 1 }, function (err) { + collection.ensureIndex(['name', 1], { unique: true, writeConcern: { w: 1 } }, function ( + err + ) { expect(err).to.not.exist; // Test return new document on change collection.insert( @@ -1229,33 +1232,37 @@ describe('Find', function () { ) { expect(err).to.not.exist; // Set up an index to force duplicate index erro - collection.ensureIndex([['failIndex', 1]], { unique: true, w: 1 }, function (err) { - expect(err).to.not.exist; + collection.ensureIndex( + [['failIndex', 1]], + { unique: true, writeConcern: { w: 1 } }, + function (err) { + expect(err).to.not.exist; - // Setup a new document - collection.insert( - { a: 2, b: 2, failIndex: 2 }, - configuration.writeConcernMax(), - function (err) { - expect(err).to.not.exist; + // Setup a new document + collection.insert( + { a: 2, b: 2, failIndex: 2 }, + configuration.writeConcernMax(), + function (err) { + expect(err).to.not.exist; - // Let's attempt to upsert with a duplicate key error - collection.findAndModify( - { c: 2 }, - [['a', 1]], - { a: 10, b: 10, failIndex: 2 }, - { w: 1, upsert: true }, - function (err, result) { - expect(result).to.not.exist; - expect(err) - .property('errmsg') - .to.match(/duplicate key/); - client.close(done); - } - ); - } - ); - }); + // Let's attempt to upsert with a duplicate key error + collection.findAndModify( + { c: 2 }, + [['a', 1]], + { a: 10, b: 10, failIndex: 2 }, + { writeConcern: { w: 1 }, upsert: true }, + function (err, result) { + expect(result).to.not.exist; + expect(err) + .property('errmsg') + .to.match(/duplicate key/); + client.close(done); + } + ); + } + ); + } + ); }); }); } @@ -1765,7 +1772,9 @@ describe('Find', function () { expect(err).to.not.exist; var id = r.ops[1]._id; // Set an index - collection.ensureIndex('login', { unique: true, w: 1 }, function (err) { + collection.ensureIndex('login', { unique: true, writeConcern: { w: 1 } }, function ( + err + ) { expect(err).to.not.exist; // Attemp to modify document diff --git a/test/functional/generator_based.test.js b/test/functional/generator_based.test.js index 4fc39cf711b..9e278b9cf0e 100644 --- a/test/functional/generator_based.test.js +++ b/test/functional/generator_based.test.js @@ -24,7 +24,7 @@ describe('Generators', function () { var docs = [{ a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }]; var collection = db.collection('batchSizeContinue'); - yield collection.insertMany(docs, { w: 1 }); + yield collection.insertMany(docs, { writeConcern: { w: 1 } }); var cursor = collection.aggregate([{ $match: { a: 1 } }, { $limit: 6 }], { cursor: { batchSize: 2 } }); diff --git a/test/functional/index.test.js b/test/functional/index.test.js index 31ba5d3cea4..639dad17011 100644 --- a/test/functional/index.test.js +++ b/test/functional/index.test.js @@ -145,7 +145,7 @@ describe('Indexes', function () { db.createIndex( collection.collectionName, 'hello', - { unique: true, w: 1 }, + { unique: true, writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; // Insert some docs @@ -193,7 +193,7 @@ describe('Indexes', function () { db.createIndex( collection.collectionName, 'hello_a', - { w: 1, unique: true }, + { writeConcern: { w: 1 }, unique: true }, function (err) { expect(err).to.not.exist; @@ -359,30 +359,34 @@ describe('Indexes', function () { expect(err).to.not.exist; db.collection('create_and_use_sparse_index_test', function (err, collection) { expect(err).to.not.exist; - collection.ensureIndex({ title: 1 }, { sparse: true, w: 1 }, 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.ensureIndex( + { title: 1 }, + { sparse: true, writeConcern: { w: 1 } }, + 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); + }); }); - }); - } - ); - }); + } + ); + } + ); }); }); }); @@ -448,29 +452,33 @@ describe('Indexes', function () { db.createCollection('geospatial_index_altered_test', function (err) { expect(err).to.not.exist; db.collection('geospatial_index_altered_test', function (err, collection) { - collection.ensureIndex({ loc: '2d' }, { min: 0, max: 1024, w: 1 }, function (err) { - expect(err).to.not.exist; - collection.insert({ loc: [100, 100] }, configuration.writeConcernMax(), function ( - err - ) { + collection.ensureIndex( + { loc: '2d' }, + { min: 0, max: 1024, writeConcern: { w: 1 } }, + function (err) { expect(err).to.not.exist; - collection.insert({ loc: [200, 200] }, configuration.writeConcernMax(), function ( + collection.insert({ loc: [100, 100] }, 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 + ) { + 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); + } + ); + }); }); - }); - }); + } + ); }); }); }); @@ -494,7 +502,9 @@ describe('Indexes', function () { collection.insert([{ a: 1 }, { a: 1 }], configuration.writeConcernMax(), function (err) { expect(err).to.not.exist; - collection.ensureIndex({ a: 1 }, { unique: true, w: 1 }, function (err) { + collection.ensureIndex({ a: 1 }, { unique: true, writeConcern: { w: 1 } }, function ( + err + ) { test.ok(err != null); client.close(done); }); @@ -521,7 +531,9 @@ describe('Indexes', function () { collection.insert([{ a: 1 }, { a: 1 }], configuration.writeConcernMax(), function (err) { expect(err).to.not.exist; - collection.ensureIndex({ a: 1 }, { unique: true, w: 1 }, function (err) { + collection.ensureIndex({ a: 1 }, { unique: true, writeConcern: { w: 1 } }, function ( + err + ) { test.ok(err != null); client.close(done); }); @@ -548,14 +560,20 @@ describe('Indexes', function () { ) { expect(err).to.not.exist; - collection.ensureIndex({ loc: '2d' }, { min: 200, max: 1400, w: 1 }, function (err) { - expect(err).to.not.exist; - - collection.insert({ loc: [600, 600] }, configuration.writeConcernMax(), function (err) { + collection.ensureIndex( + { loc: '2d' }, + { min: 200, max: 1400, writeConcern: { w: 1 } }, + function (err) { expect(err).to.not.exist; - client.close(done); - }); - }); + + collection.insert({ loc: [600, 600] }, configuration.writeConcernMax(), function ( + err + ) { + expect(err).to.not.exist; + client.close(done); + }); + } + ); }); }); } @@ -636,7 +654,9 @@ describe('Indexes', function () { function (err) { expect(err).to.not.exist; - collection.ensureIndex({ a: 1 }, { w: 1, unique: true }, function (err) { + collection.ensureIndex({ a: 1 }, { writeConcern: { w: 1 }, unique: true }, function ( + err + ) { test.ok(err != null); client.close(done); }); @@ -1273,19 +1293,28 @@ describe('Indexes', function () { it( 'should run command with commitQuorum if specified on db.createIndex', commitQuorumTest((db, collection, cb) => - db.createIndex(collection.collectionName, 'a', { w: 'majority', commitQuorum: 0 }, cb) + db.createIndex( + collection.collectionName, + 'a', + { writeConcern: { w: 'majority' }, commitQuorum: 0 }, + cb + ) ) ); it( 'should run command with commitQuorum if specified on collection.createIndex', commitQuorumTest((db, collection, cb) => - collection.createIndex('a', { w: 'majority', commitQuorum: 0 }, cb) + collection.createIndex('a', { writeConcern: { w: 'majority' }, commitQuorum: 0 }, cb) ) ); it( 'should run command with commitQuorum if specified on collection.createIndexes', commitQuorumTest((db, collection, cb) => - collection.createIndexes([{ key: { a: 1 } }], { w: 'majority', commitQuorum: 0 }, cb) + collection.createIndexes( + [{ key: { a: 1 } }], + { writeConcern: { w: 'majority' }, commitQuorum: 0 }, + cb + ) ) ); }); diff --git a/test/functional/insert.test.js b/test/functional/insert.test.js index bb968fd010d..8ae23262811 100644 --- a/test/functional/insert.test.js +++ b/test/functional/insert.test.js @@ -529,18 +529,19 @@ describe('Insert', function () { return 1; }; // Insert the update - collection.insert({ i: 1, z: func }, { w: 1, serializeFunctions: true }, function ( - err, - result - ) { - expect(err).to.not.exist; + collection.insert( + { i: 1, z: func }, + { writeConcern: { w: 1 }, serializeFunctions: true }, + function (err, result) { + expect(err).to.not.exist; - collection.findOne({ _id: result.ops[0]._id }, function (err, object) { - test.equal(normalizedFunctionString(func), object.z.code); - test.equal(1, object.i); - client.close(done); - }); - }); + collection.findOne({ _id: result.ops[0]._id }, function (err, object) { + test.equal(normalizedFunctionString(func), object.z.code); + test.equal(1, object.i); + client.close(done); + }); + } + ); }); } }); @@ -564,7 +565,7 @@ describe('Insert', function () { // Insert the update collection.insert( { i: 1, z: func }, - { w: 1, serializeFunctions: true, ordered: false }, + { writeConcern: { w: 1 }, serializeFunctions: true, ordered: false }, function (err, result) { expect(err).to.not.exist; @@ -963,7 +964,7 @@ describe('Insert', function () { collection.update( { str: 'String' }, { $set: { c: 1, d: function () {} } }, - { w: 1, serializeFunctions: false }, + { writeConcern: { w: 1 }, serializeFunctions: false }, function (err, result) { test.equal(1, result.result.n); @@ -1210,25 +1211,29 @@ describe('Insert', function () { ); // Upsert a new doc - collection.update({ a: 1 }, { $set: { a: 1 } }, { upsert: true, w: 1 }, function ( - err, - result - ) { - expect(err).to.not.exist; - if (result.result.updatedExisting) test.equal(false, result.result.updatedExisting); - test.equal(1, result.result.n); - test.ok(result.result.upserted != null); - - // Upsert an existing doc - collection.update({ a: 1 }, { $set: { a: 1 } }, { upsert: true, w: 1 }, function ( - err, - result - ) { - if (result.updatedExisting) test.equal(true, result.updatedExisting); + collection.update( + { a: 1 }, + { $set: { a: 1 } }, + { upsert: true, writeConcern: { w: 1 } }, + function (err, result) { + expect(err).to.not.exist; + if (result.result.updatedExisting) test.equal(false, result.result.updatedExisting); test.equal(1, result.result.n); - client.close(done); - }); - }); + test.ok(result.result.upserted != null); + + // Upsert an existing doc + collection.update( + { a: 1 }, + { $set: { a: 1 } }, + { upsert: true, writeConcern: { w: 1 } }, + function (err, result) { + if (result.updatedExisting) test.equal(true, result.updatedExisting); + test.equal(1, result.result.n); + client.close(done); + } + ); + } + ); }); } }); @@ -1428,7 +1433,7 @@ describe('Insert', function () { client.connect(function (err, client) { var db = client.db(configuration.db); var collection = db.collection('gh-completely2'); - collection.insert({ a: 1 }, { w: 0 }, cb); + collection.insert({ a: 1 }, { writeConcern: { w: 0 } }, cb); }); } }); @@ -1474,7 +1479,7 @@ describe('Insert', function () { client.connect(function (err, client) { var db = client.db(configuration.db); var collection = db.collection('gh-completely1'); - collection.remove({ a: 1 }, { w: 0 }, cb); + collection.remove({ a: 1 }, { writeConcern: { w: 0 } }, cb); }); } }); diff --git a/test/functional/maxtimems.test.js b/test/functional/maxtimems.test.js index ea7627f546c..ce0cca91af4 100644 --- a/test/functional/maxtimems.test.js +++ b/test/functional/maxtimems.test.js @@ -30,7 +30,7 @@ describe('Unicode', function () { var docs_1 = [{ agg_pipe: 1 }]; // Simple insert - col.insert(docs_1, { w: 1 }, function (err) { + col.insert(docs_1, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; // Execute a find command @@ -68,7 +68,7 @@ describe('Unicode', function () { var docs_1 = [{ agg_pipe: 1 }]; // Simple insert - col.insert(docs_1, { w: 1 }, function (err) { + col.insert(docs_1, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; // Execute a find command @@ -105,7 +105,7 @@ describe('Unicode', function () { var docs_1 = [{ agg_pipe: 10 }]; // Simple insert - col.insert(docs_1, { w: 1 }, function (err) { + col.insert(docs_1, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; db.admin().command( diff --git a/test/functional/object_id.test.js b/test/functional/object_id.test.js index 75412161d1b..3fba4fc260a 100644 --- a/test/functional/object_id.test.js +++ b/test/functional/object_id.test.js @@ -23,7 +23,7 @@ describe('ObjectId', function () { var collection = db.collection('test_object_id_generation.data'); // Insert test documents (creates collections and test fetch by query) - collection.insert({ name: 'Fred', age: 42 }, { w: 1 }, function (err, r) { + collection.insert({ name: 'Fred', age: 42 }, { writeConcern: { w: 1 } }, function (err, r) { test.equal(1, r.ops.length); test.ok(r.ops[0]['_id'].toHexString().length === 24); // Locate the first document inserted @@ -34,7 +34,7 @@ describe('ObjectId', function () { }); // Insert another test document and collect using ObjectId - collection.insert({ name: 'Pat', age: 21 }, { w: 1 }, function (err, r) { + collection.insert({ name: 'Pat', age: 21 }, { writeConcern: { w: 1 } }, function (err, r) { test.equal(1, r.ops.length); test.ok(r.ops[0]['_id'].toHexString().length === 24); // Locate the first document inserted @@ -47,17 +47,21 @@ describe('ObjectId', function () { // Manually created id var objectId = new ObjectId(null); // Insert a manually created document with generated oid - collection.insert({ _id: objectId, name: 'Donald', age: 95 }, { w: 1 }, function (err, r) { - test.equal(1, r.ops.length); - test.ok(r.ops[0]['_id'].toHexString().length === 24); - test.equal(objectId.toHexString(), r.ops[0]['_id'].toHexString()); - // Locate the first document inserted - collection.findOne(r.ops[0]['_id'], function (err, document) { - test.equal(r.ops[0]['_id'].toHexString(), document._id.toHexString()); - test.equal(objectId.toHexString(), document._id.toHexString()); - number_of_tests_done++; - }); - }); + collection.insert( + { _id: objectId, name: 'Donald', age: 95 }, + { writeConcern: { w: 1 } }, + function (err, r) { + test.equal(1, r.ops.length); + test.ok(r.ops[0]['_id'].toHexString().length === 24); + test.equal(objectId.toHexString(), r.ops[0]['_id'].toHexString()); + // Locate the first document inserted + collection.findOne(r.ops[0]['_id'], function (err, document) { + test.equal(r.ops[0]['_id'].toHexString(), document._id.toHexString()); + test.equal(objectId.toHexString(), document._id.toHexString()); + number_of_tests_done++; + }); + } + ); var intervalId = setInterval(function () { if (number_of_tests_done === 3) { @@ -116,7 +120,7 @@ describe('ObjectId', function () { date.setUTCMinutes(0); date.setUTCSeconds(30); - collection.insert({ _id: date }, { w: 1 }, function (err) { + collection.insert({ _id: date }, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; collection.find({ _id: date }).toArray(function (err, items) { test.equal('' + date, '' + items[0]._id); @@ -180,12 +184,12 @@ describe('ObjectId', function () { var db = client.db(configuration.db); var collection = db.collection('shouldCorrectlyInsertWithObjectId'); - collection.insert({}, { w: 1 }, function (err) { + collection.insert({}, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; const firstCompareDate = new Date(); setTimeout(function () { - collection.insert({}, { w: 1 }, function (err) { + collection.insert({}, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; const secondCompareDate = new Date(); diff --git a/test/functional/operation_example.test.js b/test/functional/operation_example.test.js index 8fc4cbd4ddf..65ec2c4be81 100644 --- a/test/functional/operation_example.test.js +++ b/test/functional/operation_example.test.js @@ -70,7 +70,7 @@ describe('Operation Examples', function () { // Create a collection var collection = db.collection('aggregationExample1'); // Insert the docs - collection.insertMany(docs, { w: 1 }, function (err, result) { + collection.insertMany(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(err).to.not.exist; test.ok(result); @@ -156,7 +156,7 @@ describe('Operation Examples', function () { // Create a collection var collection = db.collection('aggregationExample2'); // Insert the docs - collection.insertMany(docs, { w: 1 }, function (err, result) { + collection.insertMany(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(err).to.not.exist; test.ok(result); @@ -242,7 +242,7 @@ describe('Operation Examples', function () { // Create a collection var collection = db.collection('aggregation_toArray_example'); // Insert the docs - collection.insertMany(docs, { w: 1 }, function (err, result) { + collection.insertMany(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(err).to.not.exist; test.ok(result); @@ -330,7 +330,7 @@ describe('Operation Examples', function () { // Create a collection var collection = db.collection('aggregation_next_example'); // Insert the docs - collection.insertMany(docs, { w: 1 }, (err, result) => { + collection.insertMany(docs, { writeConcern: { w: 1 } }, (err, result) => { test.ok(result); expect(err).to.not.exist; @@ -417,7 +417,7 @@ describe('Operation Examples', function () { // Create a collection var collection = db.collection('aggregation_each_example'); // Insert the docs - collection.insertMany(docs, { w: 1 }, function (err, result) { + collection.insertMany(docs, { writeConcern: { w: 1 } }, function (err, result) { test.ok(result); expect(err).to.not.exist; @@ -505,7 +505,7 @@ describe('Operation Examples', function () { // Create a collection var collection = db.collection('aggregation_forEach_example'); // Insert the docs - collection.insertMany(docs, { w: 1 }, function (err, result) { + collection.insertMany(docs, { writeConcern: { w: 1 } }, function (err, result) { test.ok(result); expect(err).to.not.exist; @@ -599,7 +599,7 @@ describe('Operation Examples', function () { // Create a collection var collection = db.collection('aggregationExample3'); // Insert the docs - collection.insertMany(docs, { w: 1 }, function (err, result) { + collection.insertMany(docs, { writeConcern: { w: 1 } }, function (err, result) { test.ok(result); expect(err).to.not.exist; @@ -669,27 +669,28 @@ describe('Operation Examples', function () { // Crete the collection for the distinct example var collection = db.collection('countExample1'); // Insert documents to perform distinct against - collection.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4, b: 1 }], { w: 1 }, function ( - err, - ids - ) { - test.ok(ids); - expect(err).to.not.exist; - - // Perform a total count command - collection.count(function (err, count) { + collection.insertMany( + [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4, b: 1 }], + { writeConcern: { w: 1 } }, + function (err, ids) { + test.ok(ids); expect(err).to.not.exist; - test.equal(4, count); - // Perform a partial account where b=1 - collection.count({ b: 1 }, function (err, count) { + // Perform a total count command + collection.count(function (err, count) { expect(err).to.not.exist; - test.equal(1, count); + test.equal(4, count); - client.close(done); + // Perform a partial account where b=1 + collection.count({ b: 1 }, function (err, count) { + expect(err).to.not.exist; + test.equal(1, count); + + client.close(done); + }); }); - }); - }); + } + ); }); // END } @@ -739,7 +740,7 @@ describe('Operation Examples', function () { // Create an index on the a field collection.createIndex( { a: 1, b: 1 }, - { unique: true, background: true, w: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } }, function (err, indexName) { test.ok(indexName); expect(err).to.not.exist; @@ -795,26 +796,27 @@ describe('Operation Examples', function () { // Create a collection we want to drop later var collection = db.collection('createIndexExample2'); // Insert a bunch of documents for the index - collection.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }], { w: 1 }, function ( - err, - result - ) { - test.ok(result); - expect(err).to.not.exist; + collection.insertMany( + [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }], + { writeConcern: { w: 1 } }, + function (err, result) { + test.ok(result); + expect(err).to.not.exist; - // Create an index on the a field - collection.createIndex('a', { w: 1 }, function (err, indexName) { - test.equal('a_1', indexName); + // Create an index on the a field + collection.createIndex('a', { writeConcern: { w: 1 } }, function (err, indexName) { + test.equal('a_1', indexName); - // Perform a query, with explain to show we hit the query - collection.find({ a: 2 }).explain(function (err, explanation) { - expect(err).to.not.exist; - test.ok(explanation != null); + // Perform a query, with explain to show we hit the query + collection.find({ a: 2 }).explain(function (err, explanation) { + expect(err).to.not.exist; + test.ok(explanation != null); - client.close(done); + client.close(done); + }); }); - }); - }); + } + ); }); // END } @@ -856,12 +858,12 @@ describe('Operation Examples', function () { { a: 3, b: 3 }, { a: 4, b: 4 } ], - { w: 1 }, + { writeConcern: { w: 1 } }, function (err, result) { test.ok(result); expect(err).to.not.exist; - var options = { unique: true, background: true, w: 1 }; + var options = { unique: true, background: true, writeConcern: { w: 1 } }; // Create an index on the a field collection.createIndex({ a: 1, b: 1 }, options, function (err, indexName) { test.ok(indexName); @@ -1147,7 +1149,7 @@ describe('Operation Examples', function () { { a: 3, b: 3 }, { a: 4, b: 4 } ], - { w: 1 }, + { writeConcern: { w: 1 } }, function (err, result) { test.ok(result); expect(err).to.not.exist; @@ -1155,7 +1157,7 @@ describe('Operation Examples', function () { // Create an index on the a field collection.ensureIndex( { a: 1, b: 1 }, - { unique: true, background: true, w: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } }, function (err, indexName) { test.ok(indexName); expect(err).to.not.exist; @@ -1226,7 +1228,7 @@ describe('Operation Examples', function () { db.ensureIndex( 'ensureIndexExample1', { a: 1, b: 1 }, - { unique: true, background: true, w: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } }, function (err, indexName) { test.ok(indexName); expect(err).to.not.exist; @@ -1288,7 +1290,7 @@ describe('Operation Examples', function () { { a: 3, b: 3 }, { a: 4, b: 4 } ], - { w: 1 }, + { writeConcern: { w: 1 } }, function (err, result) { test.ok(result); expect(err).to.not.exist; @@ -1296,7 +1298,7 @@ describe('Operation Examples', function () { // Create an index on the a field collection.ensureIndex( { a: 1, b: 1 }, - { unique: true, background: true, w: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } }, function (err, indexName) { test.ok(indexName); expect(err).to.not.exist; @@ -1559,7 +1561,7 @@ describe('Operation Examples', function () { { d: 1 }, [['b', 1]], { d: 1, f: 1 }, - { new: true, upsert: true, w: 1 }, + { new: true, upsert: true, writeConcern: { w: 1 } }, function (err, doc) { expect(err).to.not.exist; test.equal(1, doc.value.d); @@ -1725,7 +1727,7 @@ describe('Operation Examples', function () { var collection = db.collection('test_map_reduce_functions'); // Insert some documents to perform map reduce over - collection.insertMany([{ user_id: 1 }, { user_id: 2 }], { w: 1 }, function(err, r) { + collection.insertMany([{ user_id: 1 }, { user_id: 2 }], { writeConcern: { w: 1 } }, function(err, r) { test.ok(r); expect(err).to.not.exist; @@ -1798,40 +1800,44 @@ describe('Operation Examples', function () { var collection = db.collection('test_map_reduce_functions_inline'); // Insert some test documents - collection.insertMany([{ user_id: 1 }, { user_id: 2 }], { w: 1 }, function (err, r) { - test.ok(r); - expect(err).to.not.exist; + collection.insertMany( + [{ user_id: 1 }, { user_id: 2 }], + { writeConcern: { w: 1 } }, + function (err, r) { + test.ok(r); + expect(err).to.not.exist; - // Map function - var map = function () { + // Map function + var map = function () { emit(this.user_id, 1); // eslint-disable-line - }; + }; - // Reduce function - // eslint-disable-next-line + // Reduce function + // eslint-disable-next-line var reduce = function(k, vals) { - return 1; - }; + return 1; + }; - // Execute map reduce and return results inline - collection.mapReduce(map, reduce, { out: { inline: 1 }, verbose: true }, function ( - err, - result - ) { - test.equal(2, result.results.length); - test.ok(result.stats != null); - - collection.mapReduce( - map, - reduce, - { out: { replace: 'mapreduce_integration_test' }, verbose: true }, - function (err, result) { - test.ok(result.stats != null); - client.close(done); - } - ); - }); - }); + // Execute map reduce and return results inline + collection.mapReduce(map, reduce, { out: { inline: 1 }, verbose: true }, function ( + err, + result + ) { + test.equal(2, result.results.length); + test.ok(result.stats != null); + + collection.mapReduce( + map, + reduce, + { out: { replace: 'mapreduce_integration_test' }, verbose: true }, + function (err, result) { + test.ok(result.stats != null); + client.close(done); + } + ); + }); + } + ); }); // END } @@ -1872,7 +1878,7 @@ describe('Operation Examples', function () { { user_id: 1, timestamp: new Date() }, { user_id: 2, timestamp: new Date() } ], - { w: 1 }, + { writeConcern: { w: 1 } }, function (err, r) { test.ok(r); expect(err).to.not.exist; @@ -1969,7 +1975,7 @@ describe('Operation Examples', function () { { user_id: 1, timestamp: new Date() }, { user_id: 2, timestamp: new Date() } ], - { w: 1 }, + { writeConcern: { w: 1 } }, function (err, r) { test.ok(r); expect(err).to.not.exist; @@ -2190,7 +2196,7 @@ describe('Operation Examples', function () { // Create an index on the a field collection.ensureIndex( { a: 1, b: 1 }, - { unique: true, background: true, w: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } }, function (err, indexName) { test.ok(indexName); expect(err).to.not.exist; @@ -2262,7 +2268,7 @@ describe('Operation Examples', function () { { a: 3, b: 3 }, { a: 4, b: 4 } ], - { w: 1 }, + { writeConcern: { w: 1 } }, function (err, result) { test.ok(result); expect(err).to.not.exist; @@ -2270,7 +2276,7 @@ describe('Operation Examples', function () { // Create an index on the a field collection.ensureIndex( { a: 1, b: 1 }, - { unique: true, background: true, w: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } }, function (err, indexName) { test.ok(indexName); expect(err).to.not.exist; @@ -2511,7 +2517,7 @@ describe('Operation Examples', function () { { name: 'Sarah', title: 'Princess' }, { name: 'Gump', title: 'Gump' } ], - { w: 1, keepGoing: true }, + { writeConcern: { w: 1 }, keepGoing: true }, function (err, result) { expect(result).to.not.exist; test.ok(err); @@ -2654,7 +2660,7 @@ describe('Operation Examples', function () { const collection = db.collection('remove_all_documents_no_safe'); // Insert a bunch of documents - collection.insertMany([{ a: 1 }, { b: 2 }], { w: 1 }, (err, result) => { + collection.insertMany([{ a: 1 }, { b: 2 }], { writeConcern: { w: 1 } }, (err, result) => { expect(err).to.not.exist; expect(result).to.exist; @@ -2708,12 +2714,15 @@ describe('Operation Examples', function () { // Fetch a collection to insert document into var collection = db.collection('remove_subset_of_documents_safe'); // Insert a bunch of documents - collection.insertMany([{ a: 1 }, { b: 2 }], { w: 1 }, function (err, result) { + collection.insertMany([{ a: 1 }, { b: 2 }], { writeConcern: { w: 1 } }, function ( + err, + result + ) { test.ok(result); expect(err).to.not.exist; // Remove all the document - collection.removeOne({ a: 1 }, { w: 1 }, function (err, r) { + collection.removeOne({ a: 1 }, { writeConcern: { w: 1 } }, function (err, r) { expect(err).to.not.exist; expect(r).property('deletedCount').to.equal(1); client.close(done); @@ -2919,21 +2928,23 @@ describe('Operation Examples', function () { // Get a collection var collection = db.collection('update_a_simple_document_upsert'); // Update the document using an upsert operation, ensuring creation if it does not exist - collection.updateOne({ a: 1 }, { $set: { b: 2, a: 1 } }, { upsert: true, w: 1 }, function ( - err, - result - ) { - expect(err).to.not.exist; - test.equal(1, result.result.n); - - // Fetch the document that we modified and check if it got inserted correctly - collection.findOne({ a: 1 }, function (err, item) { + collection.updateOne( + { a: 1 }, + { $set: { b: 2, a: 1 } }, + { upsert: true, writeConcern: { w: 1 } }, + function (err, result) { expect(err).to.not.exist; - test.equal(1, item.a); - test.equal(2, item.b); - client.close(done); - }); - }); + test.equal(1, result.result.n); + + // Fetch the document that we modified and check if it got inserted correctly + collection.findOne({ a: 1 }, function (err, item) { + expect(err).to.not.exist; + test.equal(1, item.a); + test.equal(2, item.b); + client.close(done); + }); + } + ); }); // END } @@ -3088,7 +3099,7 @@ describe('Operation Examples', function () { { a: 3, b: 3 }, { a: 4, b: 4, c: 4 } ], - { w: 1 }, + { writeConcern: { w: 1 } }, function (err, result) { test.ok(result); expect(err).to.not.exist; @@ -3096,7 +3107,7 @@ describe('Operation Examples', function () { // Create an index on the a field collection.ensureIndex( { a: 1, b: 1 }, - { unique: true, background: true, w: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } }, function (err, indexName) { test.ok(indexName); expect(err).to.not.exist; @@ -3104,7 +3115,7 @@ describe('Operation Examples', function () { // Create an additional index collection.ensureIndex( { c: 1 }, - { unique: true, background: true, w: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } }, function () { // Drop the index collection.dropAllIndexes(function (err, result) { @@ -3597,7 +3608,7 @@ describe('Operation Examples', function () { // Create a capped collection with a maximum of 1000 documents db.createCollection( 'a_simple_collection', - { capped: true, size: 10000, max: 1000, w: 1 }, + { capped: true, size: 10000, max: 1000, writeConcern: { w: 1 } }, function (err, collection) { expect(err).to.not.exist; @@ -3651,7 +3662,7 @@ describe('Operation Examples', function () { // Create a capped collection with a maximum of 1000 documents db.createCollection( 'a_simple_create_drop_collection', - { capped: true, size: 10000, max: 1000, w: 1 }, + { capped: true, size: 10000, max: 1000, writeConcern: { w: 1 } }, function (err, collection) { expect(err).to.not.exist; @@ -3855,7 +3866,7 @@ describe('Operation Examples', function () { db.createIndex( 'more_complex_index_test', { a: 1, b: 1 }, - { unique: true, background: true, w: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } }, function (err, indexName) { test.ok(indexName); expect(err).to.not.exist; @@ -3927,7 +3938,7 @@ describe('Operation Examples', function () { db.ensureIndex( 'more_complex_ensure_index_db_test', { a: 1, b: 1 }, - { unique: true, background: true, w: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } }, function (err, indexName) { test.ok(indexName); expect(err).to.not.exist; @@ -4103,11 +4114,11 @@ describe('Operation Examples', function () { var multipleColl2 = secondDb.collection('multiple_db_instances'); // Write a record into each and then count the records stored - multipleColl1.insertOne({ a: 1 }, { w: 1 }, function (err, result) { + multipleColl1.insertOne({ a: 1 }, { writeConcern: { w: 1 } }, function (err, result) { test.ok(result); expect(err).to.not.exist; - multipleColl2.insertOne({ a: 1 }, { w: 1 }, function (err, result) { + multipleColl2.insertOne({ a: 1 }, { writeConcern: { w: 1 } }, function (err, result) { test.ok(result); expect(err).to.not.exist; @@ -4283,7 +4294,7 @@ describe('Operation Examples', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted - collection.insertOne({ a: 1 }, { w: 1 }, function (err, doc) { + collection.insertOne({ a: 1 }, { writeConcern: { w: 1 } }, function (err, doc) { test.ok(doc); expect(err).to.not.exist; @@ -4334,7 +4345,7 @@ describe('Operation Examples', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted - collection.insertOne({ a: 1 }, { w: 1 }, function (err, doc) { + collection.insertOne({ a: 1 }, { writeConcern: { w: 1 } }, function (err, doc) { test.ok(doc); expect(err).to.not.exist; @@ -4403,7 +4414,7 @@ describe('Operation Examples', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted - collection.insertOne({ a: 1 }, { w: 1 }, function (err, doc) { + collection.insertOne({ a: 1 }, { writeConcern: { w: 1 } }, function (err, doc) { test.ok(doc); expect(err).to.not.exist; @@ -4656,7 +4667,7 @@ describe('Operation Examples', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted - collection.insertOne({ a: 1 }, { w: 1 }, function (err, doc) { + collection.insertOne({ a: 1 }, { writeConcern: { w: 1 } }, function (err, doc) { test.ok(doc); expect(err).to.not.exist; @@ -4705,7 +4716,7 @@ describe('Operation Examples', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted - collection.insertOne({ a: 1 }, { w: 1 }, function (err, doc) { + collection.insertOne({ a: 1 }, { writeConcern: { w: 1 } }, function (err, doc) { test.ok(doc); expect(err).to.not.exist; @@ -5615,7 +5626,7 @@ describe('Operation Examples', function () { var collection = db.collection('test_cursorstream_pause'); // Insert documents into collection - collection.insertMany(docs, { w: 1 }, function (err, ids) { + collection.insertMany(docs, { writeConcern: { w: 1 } }, function (err, ids) { test.ok(ids); expect(err).to.not.exist; @@ -5682,7 +5693,7 @@ describe('Operation Examples', function () { var collection = db.collection('test_cursorstream_destroy'); // Insert documents into collection - collection.insertMany(docs, { w: 1 }, function (err, ids) { + collection.insertMany(docs, { writeConcern: { w: 1 } }, function (err, ids) { test.ok(ids); expect(err).to.not.exist; @@ -6487,7 +6498,7 @@ describe('Operation Examples', function () { { deleteMany: { filter: { c: 1 } } }, { replaceOne: { filter: { c: 3 }, replacement: { c: 4 }, upsert: true } } ], - { ordered: true, w: 1 }, + { ordered: true, writeConcern: { w: 1 } }, function (err, r) { expect(err).to.not.exist; test.equal(1, r.nInserted); @@ -6540,7 +6551,7 @@ describe('Operation Examples', function () { var db = client.db(configuration.db); // Get the collection var col = db.collection('find_one_and_delete'); - col.insertMany([{ a: 1, b: 1 }], { w: 1 }, function (err, r) { + col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }, function (err, r) { expect(err).to.not.exist; test.equal(1, r.result.n); @@ -6588,7 +6599,7 @@ describe('Operation Examples', function () { var db = client.db(configuration.db); // Get the collection var col = db.collection('find_one_and_replace'); - col.insertMany([{ a: 1, b: 1 }], { w: 1 }, function (err, r) { + col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }, function (err, r) { expect(err).to.not.exist; test.equal(1, r.result.n); @@ -6644,7 +6655,7 @@ describe('Operation Examples', function () { var db = client.db(configuration.db); // Get the collection var col = db.collection('find_one_and_update'); - col.insertMany([{ a: 1, b: 1 }], { w: 1 }, function (err, r) { + col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }, function (err, r) { expect(err).to.not.exist; test.equal(1, r.result.n); @@ -6703,7 +6714,7 @@ describe('Operation Examples', function () { // Create a capped collection with a maximum of 1000 documents db.createCollection( 'a_simple_collection_2', - { capped: true, size: 100000, max: 10000, w: 1 }, + { capped: true, size: 100000, max: 10000, writeConcern: { w: 1 } }, function (err, collection) { expect(err).to.not.exist; diff --git a/test/functional/operation_generators_example.test.js b/test/functional/operation_generators_example.test.js index 6a625dcbb88..d198686fc01 100644 --- a/test/functional/operation_generators_example.test.js +++ b/test/functional/operation_generators_example.test.js @@ -68,7 +68,7 @@ describe('Operation (Generators)', function () { var collection = db.collection('aggregationExample2_with_generatorsGenerator'); // Insert the docs - yield collection.insertMany(docs, { w: 1 }); + yield collection.insertMany(docs, { writeConcern: { w: 1 } }); // Execute aggregate, notice the pipeline is expressed as an Array var cursor = collection.aggregate( @@ -151,7 +151,7 @@ describe('Operation (Generators)', function () { var collection = db.collection('aggregation_next_example_with_generatorsGenerator'); // Insert the docs - yield collection.insertMany(docs, { w: 1 }); + yield collection.insertMany(docs, { writeConcern: { w: 1 } }); // Execute aggregate, notice the pipeline is expressed as an Array var cursor = collection.aggregate( @@ -218,7 +218,7 @@ describe('Operation (Generators)', function () { var collection = db.collection('countExample1_with_generators'); // Insert documents to perform distinct against yield collection.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4, b: 1 }], { - w: 1 + writeConcern: { w: 1 } }); // Perform a total count command var count = yield collection.count(); @@ -279,7 +279,10 @@ describe('Operation (Generators)', function () { ); // Create an index on the a field - yield collection.createIndex({ a: 1, b: 1 }, { unique: true, background: true, w: 1 }); + yield collection.createIndex( + { a: 1, b: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } } + ); // Show that duplicate records got dropped var items = yield collection.find({}).toArray(); @@ -550,11 +553,14 @@ describe('Operation (Generators)', function () { { a: 3, b: 3 }, { a: 4, b: 4 } ], - { w: 1 } + { writeConcern: { w: 1 } } ); // Create an index on the a field - yield collection.ensureIndex({ a: 1, b: 1 }, { unique: true, background: true, w: 1 }); + yield collection.ensureIndex( + { a: 1, b: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } } + ); // Drop the index yield collection.dropIndex('a_1_b_1'); @@ -617,7 +623,7 @@ describe('Operation (Generators)', function () { yield db.ensureIndex( 'ensureIndexExample1_with_generators', { a: 1, b: 1 }, - { unique: true, background: true, w: 1 } + { unique: true, background: true, writeConcern: { w: 1 } } ); // Show that duplicate records got dropped @@ -673,11 +679,14 @@ describe('Operation (Generators)', function () { { a: 3, b: 3 }, { a: 4, b: 4 } ], - { w: 1 } + { writeConcern: { w: 1 } } ); // Create an index on the a field - yield collection.ensureIndex({ a: 1, b: 1 }, { unique: true, background: true, w: 1 }); + yield collection.ensureIndex( + { a: 1, b: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } } + ); // Show that duplicate records got dropped var items = yield collection.find({}).toArray(); @@ -922,7 +931,7 @@ describe('Operation (Generators)', function () { { d: 1 }, [['b', 1]], { d: 1, f: 1 }, - { new: true, upsert: true, w: 1 } + { new: true, upsert: true, writeConcern: { w: 1 } } ); test.equal(1, doc.value.d); test.equal(1, doc.value.f); @@ -1081,7 +1090,7 @@ describe('Operation (Generators)', function () { var collection = db.collection('test_map_reduce_functions_with_generators'); // Insert some documents to perform map reduce over - yield collection.insertMany([{ user_id: 1 }, { user_id: 2 }], { w: 1 }); + yield collection.insertMany([{ user_id: 1 }, { user_id: 2 }], { writeConcern: { w: 1 } }); // Map function var map = function () { @@ -1148,7 +1157,7 @@ describe('Operation (Generators)', function () { var collection = db.collection('test_map_reduce_functions_inline_with_generators'); // Insert some test documents - yield collection.insertMany([{ user_id: 1 }, { user_id: 2 }], { w: 1 }); + yield collection.insertMany([{ user_id: 1 }, { user_id: 2 }], { writeConcern: { w: 1 } }); // Map function var map = function () { @@ -1217,7 +1226,7 @@ describe('Operation (Generators)', function () { { user_id: 1, timestamp: new Date() }, { user_id: 2, timestamp: new Date() } ], - { w: 1 } + { writeConcern: { w: 1 } } ); // Map function @@ -1308,7 +1317,7 @@ describe('Operation (Generators)', function () { { user_id: 1, timestamp: new Date() }, { user_id: 2, timestamp: new Date() } ], - { w: 1 } + { writeConcern: { w: 1 } } ); // Map function @@ -1511,7 +1520,10 @@ describe('Operation (Generators)', function () { ); // Create an index on the a field - yield collection.ensureIndex({ a: 1, b: 1 }, { unique: true, background: true, w: 1 }); + yield collection.ensureIndex( + { a: 1, b: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } } + ); // Fetch basic indexInformation for collection var indexInformation = yield db.indexInformation( @@ -1579,11 +1591,14 @@ describe('Operation (Generators)', function () { { a: 3, b: 3 }, { a: 4, b: 4 } ], - { w: 1 } + { writeConcern: { w: 1 } } ); // Create an index on the a field - yield collection.ensureIndex({ a: 1, b: 1 }, { unique: true, background: true, w: 1 }); + yield collection.ensureIndex( + { a: 1, b: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } } + ); // Fetch basic indexInformation for collection var indexInformation = yield collection.indexInformation(); @@ -1804,7 +1819,7 @@ describe('Operation (Generators)', function () { { name: 'Sarah', title: 'Princess' }, { name: 'Gump', title: 'Gump' } ], - { w: 1, keepGoing: true } + { writeConcern: { w: 1 }, keepGoing: true } ); } catch (err) {} // eslint-disable-line // Count the number of documents left (should not include the duplicates) @@ -1949,7 +1964,7 @@ describe('Operation (Generators)', function () { var collection = db.collection('remove_all_documents_no_safe_with_generators'); // Insert a bunch of documents - yield collection.insertMany([{ a: 1 }, { b: 2 }], { w: 1 }); + yield collection.insertMany([{ a: 1 }, { b: 2 }], { writeConcern: { w: 1 } }); // Remove all the document yield collection.removeMany(); @@ -1997,9 +2012,9 @@ describe('Operation (Generators)', function () { // Fetch a collection to insert document into var collection = db.collection('remove_subset_of_documents_safe_with_generators'); // Insert a bunch of documents - yield collection.insertMany([{ a: 1 }, { b: 2 }], { w: 1 }); + yield collection.insertMany([{ a: 1 }, { b: 2 }], { writeConcern: { w: 1 } }); // Remove all the document - var r = yield collection.removeOne({ a: 1 }, { w: 1 }); + var r = yield collection.removeOne({ a: 1 }, { writeConcern: { w: 1 } }); expect(r).property('deletedCount').to.equal(1); yield client.close(); }); @@ -2202,7 +2217,7 @@ describe('Operation (Generators)', function () { var result = yield collection.updateOne( { a: 1 }, { $set: { b: 2, a: 1 } }, - { upsert: true, w: 1 } + { upsert: true, writeConcern: { w: 1 } } ); test.equal(1, result.result.n); @@ -2367,16 +2382,19 @@ describe('Operation (Generators)', function () { { a: 3, b: 3 }, { a: 4, b: 4, c: 4 } ], - { w: 1 } + { writeConcern: { w: 1 } } ); // Create an index on the a field - yield collection.ensureIndex({ a: 1, b: 1 }, { unique: true, background: true, w: 1 }); + yield collection.ensureIndex( + { a: 1, b: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } } + ); // Create an additional index yield collection.ensureIndex( { c: 1 }, - { unique: true, background: true, sparse: true, w: 1 } + { unique: true, background: true, sparse: true, writeConcern: { w: 1 } } ); // Drop the index @@ -2632,7 +2650,7 @@ describe('Operation (Generators)', function () { capped: true, size: 10000, max: 1000, - w: 1 + writeConcern: { w: 1 } }); // Insert a document in the capped collection @@ -2679,7 +2697,7 @@ describe('Operation (Generators)', function () { // Create a capped collection with a maximum of 1000 documents var collection = yield db.createCollection( 'a_simple_create_drop_collection_with_generators', - { capped: true, size: 10000, max: 1000, w: 1 } + { capped: true, size: 10000, max: 1000, writeConcern: { w: 1 } } ); // Insert a document in the capped collection @@ -2857,7 +2875,7 @@ describe('Operation (Generators)', function () { yield db.createIndex( 'more_complex_index_test_with_generators', { a: 1, b: 1 }, - { unique: true, background: true, w: 1 } + { unique: true, background: true, writeConcern: { w: 1 } } ); // Show that duplicate records got dropped @@ -2922,7 +2940,7 @@ describe('Operation (Generators)', function () { yield db.ensureIndex( 'more_complex_ensure_index_db_test_with_generators', { a: 1, b: 1 }, - { unique: true, background: true, w: 1 } + { unique: true, background: true, writeConcern: { w: 1 } } ); // Show that duplicate records got dropped @@ -3088,8 +3106,8 @@ describe('Operation (Generators)', function () { var multipleColl2 = secondDb.collection('multiple_db_instances_with_generators'); // Write a record into each and then count the records stored - yield multipleColl1.insertOne({ a: 1 }, { w: 1 }); - yield multipleColl2.insertOne({ a: 1 }, { w: 1 }); + yield multipleColl1.insertOne({ a: 1 }, { writeConcern: { w: 1 } }); + yield multipleColl2.insertOne({ a: 1 }, { writeConcern: { w: 1 } }); // Count over the results ensuring only on record in each collection var count = yield multipleColl1.count(); @@ -3233,7 +3251,7 @@ describe('Operation (Generators)', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted - yield collection.insertOne({ a: 1 }, { w: 1 }); + yield collection.insertOne({ a: 1 }, { writeConcern: { w: 1 } }); // Set the profiling level to only profile slow queries yield db.setProfilingLevel('slow_only'); @@ -3307,7 +3325,7 @@ describe('Operation (Generators)', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted - yield collection.insertOne({ a: 1 }, { w: 1 }); + yield collection.insertOne({ a: 1 }, { writeConcern: { w: 1 } }); // Set the profiling level to all yield db.setProfilingLevel('all'); @@ -3368,7 +3386,7 @@ describe('Operation (Generators)', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted - yield collection.insertOne({ a: 1 }, { w: 1 }); + yield collection.insertOne({ a: 1 }, { writeConcern: { w: 1 } }); // Use the admin database for the operation var adminDb = db.admin(); @@ -3597,7 +3615,7 @@ describe('Operation (Generators)', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted - yield collection.insertOne({ a: 1 }, { w: 1 }); + yield collection.insertOne({ a: 1 }, { writeConcern: { w: 1 } }); // Use the admin database for the operation var adminDb = db.admin(); @@ -3648,7 +3666,7 @@ describe('Operation (Generators)', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted - yield collection.insertOne({ a: 1 }, { w: 1 }); + yield collection.insertOne({ a: 1 }, { writeConcern: { w: 1 } }); // Use the admin database for the operation var adminDb = db.admin(); @@ -4391,7 +4409,7 @@ describe('Operation (Generators)', function () { { deleteMany: { filter: { c: 1 } } }, { replaceOne: { filter: { c: 3 }, replacement: { c: 4 }, upsert: true } } ], - { ordered: true, w: 1 } + { ordered: true, writeConcern: { w: 1 } } ); test.equal(1, r.nInserted); test.equal(2, r.nUpserted); @@ -4445,7 +4463,7 @@ describe('Operation (Generators)', function () { // BEGIN // Get the collection var col = db.collection('find_one_and_delete_with_generators'); - var r = yield col.insertMany([{ a: 1, b: 1 }], { w: 1 }); + var r = yield col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }); test.equal(1, r.result.n); r = yield col.findOneAndDelete({ a: 1 }, { projection: { b: 1 }, sort: { a: 1 } }); @@ -4490,7 +4508,7 @@ describe('Operation (Generators)', function () { // BEGIN // Get the collection var col = db.collection('find_one_and_replace_with_generators'); - var r = yield col.insertMany([{ a: 1, b: 1 }], { w: 1 }); + var r = yield col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }); test.equal(1, r.result.n); r = yield col.findOneAndReplace( @@ -4545,7 +4563,7 @@ describe('Operation (Generators)', function () { // BEGIN // Get the collection var col = db.collection('find_one_and_update_with_generators'); - var r = yield col.insertMany([{ a: 1, b: 1 }], { w: 1 }); + var r = yield col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }); test.equal(1, r.result.n); r = yield col.findOneAndUpdate( @@ -4603,7 +4621,7 @@ describe('Operation (Generators)', function () { capped: true, size: 100000, max: 10000, - w: 1 + writeConcern: { w: 1 } }); var docs = []; for (var i = 0; i < 1000; i++) docs.push({ a: i }); diff --git a/test/functional/operation_promises_example.test.js b/test/functional/operation_promises_example.test.js index 6caf9ed9a42..71fb3319e39 100644 --- a/test/functional/operation_promises_example.test.js +++ b/test/functional/operation_promises_example.test.js @@ -70,7 +70,7 @@ describe('Operation (Promises)', function () { // Insert the docs return collection - .insertMany(docs, { w: 1 }) + .insertMany(docs, { writeConcern: { w: 1 } }) .then(function (result) { test.ok(result); @@ -152,7 +152,7 @@ describe('Operation (Promises)', function () { let cursor; // Insert the docs return collection - .insertMany(docs, { w: 1 }) + .insertMany(docs, { writeConcern: { w: 1 } }) .then(function (result) { test.ok(result); @@ -220,7 +220,7 @@ describe('Operation (Promises)', function () { // Insert documents to perform distinct against return collection - .insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4, b: 1 }], { w: 1 }) + .insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4, b: 1 }], { writeConcern: { w: 1 } }) .then(function (ids) { test.ok(ids); @@ -286,7 +286,10 @@ describe('Operation (Promises)', function () { test.ok(result); // Create an index on the a field - return collection.createIndex({ a: 1, b: 1 }, { unique: true, background: true, w: 1 }); + return collection.createIndex( + { a: 1, b: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } } + ); }) .then(function (indexName) { test.ok(indexName); @@ -557,13 +560,16 @@ describe('Operation (Promises)', function () { { a: 3, b: 3 }, { a: 4, b: 4 } ], - { w: 1 } + { writeConcern: { w: 1 } } ) .then(function (result) { test.ok(result); // Create an index on the a field - return collection.ensureIndex({ a: 1, b: 1 }, { unique: true, background: true, w: 1 }); + return collection.ensureIndex( + { a: 1, b: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } } + ); }) .then(function (indexName) { test.ok(indexName); @@ -632,7 +638,7 @@ describe('Operation (Promises)', function () { return db.ensureIndex( 'ensureIndexExample1_with_promise', { a: 1, b: 1 }, - { unique: true, background: true, w: 1 } + { unique: true, background: true, writeConcern: { w: 1 } } ); }) .then(function (indexName) { @@ -690,13 +696,16 @@ describe('Operation (Promises)', function () { { a: 3, b: 3 }, { a: 4, b: 4 } ], - { w: 1 } + { writeConcern: { w: 1 } } ) .then(function (result) { test.ok(result); // Create an index on the a field - return collection.ensureIndex({ a: 1, b: 1 }, { unique: true, background: true, w: 1 }); + return collection.ensureIndex( + { a: 1, b: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } } + ); }) .then(function (indexName) { test.ok(indexName); @@ -947,7 +956,7 @@ describe('Operation (Promises)', function () { { d: 1 }, [['b', 1]], { d: 1, f: 1 }, - { new: true, upsert: true, w: 1 } + { new: true, upsert: true, writeConcern: { w: 1 } } ); }) .then(function (doc) { @@ -1103,7 +1112,7 @@ describe('Operation (Promises)', function () { // Insert some documents to perform map reduce over return collection - .insertMany([{ user_id: 1 }, { user_id: 2 }], { w: 1 }) + .insertMany([{ user_id: 1 }, { user_id: 2 }], { writeConcern: { w: 1 }}) .then(function () { // Map function var map = function () { @@ -1182,7 +1191,7 @@ describe('Operation (Promises)', function () { // Insert some test documents return collection - .insertMany([{ user_id: 1 }, { user_id: 2 }], { w: 1 }) + .insertMany([{ user_id: 1 }, { user_id: 2 }], { writeConcern: { w: 1 } }) .then(function () { // Execute map reduce and return results inline return collection.mapReduce(map, reduce, { out: { inline: 1 }, verbose: true }); @@ -1266,7 +1275,7 @@ describe('Operation (Promises)', function () { { user_id: 1, timestamp: new Date() }, { user_id: 2, timestamp: new Date() } ], - { w: 1 } + { writeConcern: { w: 1 } } ) .then(function () { return collection.mapReduce(map, reduce, o); @@ -1359,7 +1368,7 @@ describe('Operation (Promises)', function () { { user_id: 1, timestamp: new Date() }, { user_id: 2, timestamp: new Date() } ], - { w: 1 } + { writeConcern: { w: 1 } } ) .then(function () { return collection.mapReduce(map, reduce, o); @@ -1550,7 +1559,10 @@ describe('Operation (Promises)', function () { test.ok(result); // Create an index on the a field - return collection.ensureIndex({ a: 1, b: 1 }, { unique: true, background: true, w: 1 }); + return collection.ensureIndex( + { a: 1, b: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } } + ); }) .then(function (indexName) { test.ok(indexName); @@ -1617,13 +1629,16 @@ describe('Operation (Promises)', function () { { a: 3, b: 3 }, { a: 4, b: 4 } ], - { w: 1 } + { writeConcern: { w: 1 } } ) .then(function (result) { test.ok(result); // Create an index on the a field - return collection.ensureIndex({ a: 1, b: 1 }, { unique: true, background: true, w: 1 }); + return collection.ensureIndex( + { a: 1, b: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } } + ); }) .then(function (indexName) { test.ok(indexName); @@ -1852,7 +1867,7 @@ describe('Operation (Promises)', function () { { name: 'Sarah', title: 'Princess' }, { name: 'Gump', title: 'Gump' } ], - { w: 1, keepGoing: true } + { writeConcern: { w: 1 }, keepGoing: true } ); }) .catch(function () { @@ -1982,7 +1997,7 @@ describe('Operation (Promises)', function () { // Insert a bunch of documents return collection - .insertMany([{ a: 1 }, { b: 2 }], { w: 1 }) + .insertMany([{ a: 1 }, { b: 2 }], { writeConcern: { w: 1 } }) .then(function (result) { test.ok(result); @@ -2255,7 +2270,7 @@ describe('Operation (Promises)', function () { // Update the document using an upsert operation, ensuring creation if it does not exist return collection - .updateOne({ a: 1 }, { $set: { b: 2, a: 1 } }, { upsert: true, w: 1 }) + .updateOne({ a: 1 }, { $set: { b: 2, a: 1 } }, { upsert: true, writeConcern: { w: 1 } }) .then(function (result) { test.equal(1, result.result.n); @@ -2412,21 +2427,24 @@ describe('Operation (Promises)', function () { { a: 4, b: 4, c: 4 } ], { - w: 1 + writeConcern: { w: 1 } } ) .then(function (result) { test.ok(result); // Create an index on the a field - return collection.ensureIndex({ a: 1, b: 1 }, { unique: true, background: true, w: 1 }); + return collection.ensureIndex( + { a: 1, b: 1 }, + { unique: true, background: true, writeConcern: { w: 1 } } + ); }) .then(function (indexName) { test.ok(indexName); // Create an additional index return collection.ensureIndex( { c: 1 }, - { unique: true, background: true, sparse: true, w: 1 } + { unique: true, background: true, sparse: true, writeConcern: { w: 1 } } ); }) .then(function (indexName) { @@ -2826,7 +2844,7 @@ describe('Operation (Promises)', function () { capped: true, size: 10000, max: 1000, - w: 1 + writeConcern: { w: 1 } }) .then(function (collection) { // Insert a document in the capped collection @@ -2877,7 +2895,7 @@ describe('Operation (Promises)', function () { capped: true, size: 10000, max: 1000, - w: 1 + writeConcern: { w: 1 } }); }) .then(function (collection) { @@ -3069,7 +3087,7 @@ describe('Operation (Promises)', function () { return db.createIndex( 'more_complex_index_test_with_promise', { a: 1, b: 1 }, - { unique: true, background: true, w: 1 } + { unique: true, background: true, writeConcern: { w: 1 } } ); }) .then(function (indexName) { @@ -3139,7 +3157,7 @@ describe('Operation (Promises)', function () { return db.ensureIndex( 'more_complex_ensure_index_db_test_with_promise', { a: 1, b: 1 }, - { unique: true, background: true, w: 1 } + { unique: true, background: true, writeConcern: { w: 1 } } ); }) .then(function (indexName) { @@ -3302,10 +3320,10 @@ describe('Operation (Promises)', function () { // Write a record into each and then count the records stored return multipleColl1 - .insertOne({ a: 1 }, { w: 1 }) + .insertOne({ a: 1 }, { writeConcern: { w: 1 } }) .then(function (result) { test.ok(result); - return multipleColl2.insertOne({ a: 1 }, { w: 1 }); + return multipleColl2.insertOne({ a: 1 }, { writeConcern: { w: 1 } }); }) .then(function (result) { test.ok(result); @@ -3439,7 +3457,7 @@ describe('Operation (Promises)', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted return collection - .insertOne({ a: 1 }, { w: 1 }) + .insertOne({ a: 1 }, { writeConcern: { w: 1 } }) .then(function (doc) { test.ok(doc); // Use the admin database for the operation @@ -3490,7 +3508,7 @@ describe('Operation (Promises)', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted return collection - .insertOne({ a: 1 }, { w: 1 }) + .insertOne({ a: 1 }, { writeConcern: { w: 1 } }) .then(function (doc) { test.ok(doc); // Set the profiling level to only profile slow queries @@ -3571,7 +3589,7 @@ describe('Operation (Promises)', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted return collection - .insertOne({ a: 1 }, { w: 1 }) + .insertOne({ a: 1 }, { writeConcern: { w: 1 } }) .then(function (doc) { test.ok(doc); // Use the admin database for the operation @@ -3638,7 +3656,7 @@ describe('Operation (Promises)', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted return collection - .insertOne({ a: 1 }, { w: 1 }) + .insertOne({ a: 1 }, { writeConcern: { w: 1 } }) .then(function (doc) { test.ok(doc); // Use the admin database for the operation @@ -3855,7 +3873,7 @@ describe('Operation (Promises)', function () { // Force the creation of the collection by inserting a document // Collections are not created until the first document is inserted return collection - .insertOne({ a: 1 }, { w: 1 }) + .insertOne({ a: 1 }, { writeConcern: { w: 1 } }) .then(function (doc) { test.ok(doc); // Add the new user to the admin database @@ -4729,7 +4747,7 @@ describe('Operation (Promises)', function () { { deleteMany: { filter: { c: 1 } } }, { replaceOne: { filter: { c: 3 }, replacement: { c: 4 }, upsert: true } } ], - { ordered: true, w: 1 } + { ordered: true, writeConcern: { w: 1 } } ) .then(function (r) { test.equal(1, r.nInserted); @@ -4772,7 +4790,7 @@ describe('Operation (Promises)', function () { return col .bulkWrite( [{ insertOne: { document: { _id: 1 } } }, { insertOne: { document: { _id: 1 } } }], - { ordered: true, w: 1 } + { ordered: true, writeConcern: { w: 1 } } ) .catch(function (err) { test.equal(true, err.result.hasWriteErrors()); @@ -4812,7 +4830,7 @@ describe('Operation (Promises)', function () { // Get the collection var col = db.collection('find_one_and_delete_with_promise'); return col - .insertMany([{ a: 1, b: 1 }], { w: 1 }) + .insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }) .then(function (r) { test.equal(1, r.result.n); @@ -4857,7 +4875,7 @@ describe('Operation (Promises)', function () { // BEGIN // Get the collection var col = db.collection('find_one_and_replace_with_promise'); - return col.insertMany([{ a: 1, b: 1 }], { w: 1 }).then(function (r) { + return col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }).then(function (r) { test.equal(1, r.result.n); return col @@ -4913,7 +4931,7 @@ describe('Operation (Promises)', function () { // Get the collection var col = db.collection('find_one_and_update_with_promise'); return col - .insertMany([{ a: 1, b: 1 }], { w: 1 }) + .insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }) .then(function (r) { test.equal(1, r.result.n); @@ -4973,7 +4991,7 @@ describe('Operation (Promises)', function () { capped: true, size: 100000, max: 10000, - w: 1 + writeConcern: { w: 1 } }) .then(function (_collection) { collection = _collection; diff --git a/test/functional/promises_collection.test.js b/test/functional/promises_collection.test.js index 0ff90092686..a2e568939c0 100644 --- a/test/functional/promises_collection.test.js +++ b/test/functional/promises_collection.test.js @@ -66,7 +66,7 @@ describe('Promises (Collection)', function () { // BEGIN // Get the collection var col = db.collection('find_one_and_delete_with_promise_no_option'); - col.insertMany([{ a: 1, b: 1 }], { w: 1 }).then(function (r) { + col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }).then(function (r) { test.equal(1, r.result.n); col @@ -113,7 +113,7 @@ describe('Promises (Collection)', function () { // BEGIN // Get the collection var col = db.collection('find_one_and_update_with_promise_no_option'); - col.insertMany([{ a: 1, b: 1 }], { w: 1 }).then(function (r) { + col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }).then(function (r) { test.equal(1, r.result.n); col @@ -162,7 +162,7 @@ describe('Promises (Collection)', function () { // BEGIN // Get the collection var col = db.collection('find_one_and_replace_with_promise_no_option'); - col.insertMany([{ a: 1, b: 1 }], { w: 1 }).then(function (r) { + col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }).then(function (r) { test.equal(1, r.result.n); col diff --git a/test/functional/raw.test.js b/test/functional/raw.test.js index 5afef456105..b4d9a9bdf17 100644 --- a/test/functional/raw.test.js +++ b/test/functional/raw.test.js @@ -26,30 +26,34 @@ describe('Raw', function () { ) { expect(err).to.not.exist; // Insert some documents - collection.insert([{ a: 1 }, { b: 2000 }, { c: 2.3 }], { w: 1 }, function (err) { - expect(err).to.not.exist; - // You have to pass at least query + fields before passing options - collection.find({}, { raw: true, batchSize: 2 }).toArray(function (err, items) { - var objects = []; + collection.insert( + [{ a: 1 }, { b: 2000 }, { c: 2.3 }], + { writeConcern: { w: 1 } }, + function (err) { + expect(err).to.not.exist; + // You have to pass at least query + fields before passing options + collection.find({}, { raw: true, batchSize: 2 }).toArray(function (err, items) { + var objects = []; - for (var i = 0; i < items.length; i++) { - test.ok(Buffer.isBuffer(items[i])); - objects.push(BSON.deserialize(items[i])); - } + for (var i = 0; i < items.length; i++) { + test.ok(Buffer.isBuffer(items[i])); + objects.push(BSON.deserialize(items[i])); + } - test.equal(1, objects[0].a); - test.equal(2000, objects[1].b); - test.equal(2.3, objects[2].c); + test.equal(1, objects[0].a); + test.equal(2000, objects[1].b); + test.equal(2.3, objects[2].c); - // Execute findOne - collection.findOne({ a: 1 }, { raw: true }, function (err, item) { - test.ok(Buffer.isBuffer(item)); - var object = BSON.deserialize(item); - test.equal(1, object.a); - client.close(done); + // Execute findOne + collection.findOne({ a: 1 }, { raw: true }, function (err, item) { + test.ok(Buffer.isBuffer(item)); + var object = BSON.deserialize(item); + test.equal(1, object.a); + client.close(done); + }); }); - }); - }); + } + ); }); }); } @@ -70,29 +74,33 @@ describe('Raw', function () { { raw: true }, function (err, collection) { // Insert some documents - collection.insert([{ a: 1 }, { b: 2000 }, { c: 2.3 }], { w: 1 }, function (err) { - expect(err).to.not.exist; + collection.insert( + [{ a: 1 }, { b: 2000 }, { c: 2.3 }], + { writeConcern: { w: 1 } }, + function (err) { + expect(err).to.not.exist; - collection.find({}, { batchSize: 2 }).toArray(function (err, items) { - var objects = []; - for (var i = 0; i < items.length; i++) { - test.ok(Buffer.isBuffer(items[i])); - objects.push(BSON.deserialize(items[i])); - } + collection.find({}, { batchSize: 2 }).toArray(function (err, items) { + var objects = []; + for (var i = 0; i < items.length; i++) { + test.ok(Buffer.isBuffer(items[i])); + objects.push(BSON.deserialize(items[i])); + } - test.equal(1, objects[0].a); - test.equal(2000, objects[1].b); - test.equal(2.3, objects[2].c); + test.equal(1, objects[0].a); + test.equal(2000, objects[1].b); + test.equal(2.3, objects[2].c); - // Execute findOne - collection.findOne({ a: 1 }, { raw: true }, function (err, item) { - test.ok(Buffer.isBuffer(item)); - var object = BSON.deserialize(item); - test.equal(1, object.a); - client.close(done); + // Execute findOne + collection.findOne({ a: 1 }, { raw: true }, function (err, item) { + test.ok(Buffer.isBuffer(item)); + var object = BSON.deserialize(item); + test.equal(1, object.a); + client.close(done); + }); }); - }); - }); + } + ); } ); }); diff --git a/test/functional/remove.test.js b/test/functional/remove.test.js index 5ed74771f04..8b6e4d85f8b 100644 --- a/test/functional/remove.test.js +++ b/test/functional/remove.test.js @@ -28,10 +28,10 @@ describe('Remove', function () { db.collection('test_clear', function (err, collection) { expect(err).to.not.exist; - collection.insert({ i: 1 }, { w: 1 }, function (err) { + collection.insert({ i: 1 }, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; - collection.insert({ i: 2 }, { w: 1 }, function (err) { + collection.insert({ i: 2 }, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; collection.count(function (err, count) { @@ -39,7 +39,7 @@ describe('Remove', function () { expect(count).to.equal(2); // Clear the collection - collection.remove({}, { w: 1 }, function (err, r) { + collection.remove({}, { writeConcern: { w: 1 } }, function (err, r) { expect(err).to.not.exist; expect(r).property('deletedCount').to.equal(2); @@ -81,22 +81,29 @@ describe('Remove', function () { db.collection('test_remove_regexp', function (err, collection) { expect(err).to.not.exist; - collection.insert({ address: '485 7th ave new york' }, { 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/ }, { 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); + }); }); - }); - }); + } + ); }); }); }); @@ -124,19 +131,26 @@ describe('Remove', function () { db.collection('shouldCorrectlyRemoveOnlyFirstDocument', function (err, collection) { expect(err).to.not.exist; - collection.insert([{ a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }], { 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 }, { 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/sessions.test.js b/test/functional/sessions.test.js index eb25a46ec87..acf45e1684a 100644 --- a/test/functional/sessions.test.js +++ b/test/functional/sessions.test.js @@ -200,7 +200,7 @@ describe('Sessions', function () { context('unacknowledged writes', () => { it('should not include session for unacknowledged writes', { metadata: { requires: { topology: 'single', mongodb: '>=3.6.0' } }, - test: withMonitoredClient('insert', { clientOptions: { w: 0 } }, function ( + test: withMonitoredClient('insert', { clientOptions: { writeConcern: { w: 0 } } }, function ( client, events, done @@ -219,7 +219,7 @@ describe('Sessions', function () { }); it('should throw error with explicit session', { metadata: { requires: { topology: 'replicaset', mongodb: '>=3.6.0' } }, - test: withMonitoredClient('insert', { clientOptions: { w: 0 } }, function ( + test: withMonitoredClient('insert', { clientOptions: { writeConcern: { w: 0 } } }, function ( client, events, done diff --git a/test/functional/unicode.test.js b/test/functional/unicode.test.js index 908ec1f3c58..5f94b0e49f9 100644 --- a/test/functional/unicode.test.js +++ b/test/functional/unicode.test.js @@ -67,7 +67,7 @@ describe('Unicode', function () { ) { doc['_id'] = 'felixge'; - collection.insertOne(doc, { w: 1 }, function (err) { + collection.insertOne(doc, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; collection.findOne(function (err, doc) { test.equal('felixge', doc._id); @@ -91,21 +91,31 @@ describe('Unicode', function () { var db = client.db(configuration.db); db.createCollection('unicode_test_collection', function (err, collection) { var test_strings = ['ouooueauiOUOOUEAUI', 'öüóőúéáűíÖÜÓŐÚÉÁŰÍ', '本荘由利地域に洪水警報']; - collection.insert({ id: 0, text: test_strings[0] }, { w: 1 }, function (err) { + collection.insert({ id: 0, text: test_strings[0] }, { writeConcern: { w: 1 } }, function ( + err + ) { expect(err).to.not.exist; - collection.insert({ id: 1, text: test_strings[1] }, { w: 1 }, function (err) { - expect(err).to.not.exist; - collection.insert({ id: 2, text: test_strings[2] }, { w: 1 }, function (err) { + collection.insert( + { id: 1, text: test_strings[1] }, + { writeConcern: { w: 1 } }, + function (err) { expect(err).to.not.exist; - collection.find().each(function (err, item) { - if (item != null) { - test.equal(test_strings[item.id], item.text); - } else { - client.close(done); + collection.insert( + { id: 2, text: test_strings[2] }, + { writeConcern: { w: 1 } }, + function (err) { + expect(err).to.not.exist; + collection.find().each(function (err, item) { + if (item != null) { + test.equal(test_strings[item.id], item.text); + } else { + client.close(done); + } + }); } - }); - }); - }); + ); + } + ); }); }); }); @@ -128,7 +138,7 @@ describe('Unicode', function () { expect(err).to.not.exist; db.collection('create_object_with_chinese_object_name', function (err, collection) { expect(err).to.not.exist; - collection.insert(object, { w: 1 }, function (err) { + collection.insert(object, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; collection.findOne(function (err, item) { test.equal(object['客家话'], item['客家话']); @@ -156,7 +166,7 @@ describe('Unicode', function () { client.connect(function (err, client) { var db = client.db(configuration.db); db.createCollection('test_utf8_key_name', function (err, collection) { - collection.insert({ šđžčćŠĐŽČĆ: 1 }, { w: 1 }, function (err) { + collection.insert({ šđžčćŠĐŽČĆ: 1 }, { writeConcern: { w: 1 } }, function (err) { expect(err).to.not.exist; collection .find({}) diff --git a/test/tools/runner/config.js b/test/tools/runner/config.js index 9fae11607c5..2d408e85669 100644 --- a/test/tools/runner/config.js +++ b/test/tools/runner/config.js @@ -33,7 +33,7 @@ class NativeConfiguration { ); this.writeConcern = function () { - return { w: 1 }; + return { writeConcern: { w: 1 } }; //TODO HANA }; } From ed0868f3e6b24f3904e9868f9d13537079411692 Mon Sep 17 00:00:00 2001 From: Hana Pearlman Date: Fri, 20 Nov 2020 15:23:14 -0500 Subject: [PATCH 5/5] small cleanup --- src/cmap/wire_protocol/command.ts | 3 ++- src/gridfs-stream/upload.ts | 8 +++++--- src/mongo_client.ts | 1 - src/operations/connect.ts | 19 +++++++++++-------- src/write_concern.ts | 1 - test/tools/runner/config.js | 2 +- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/cmap/wire_protocol/command.ts b/src/cmap/wire_protocol/command.ts index a97c4342dfb..c87774a34bc 100644 --- a/src/cmap/wire_protocol/command.ts +++ b/src/cmap/wire_protocol/command.ts @@ -12,8 +12,9 @@ import type { WriteConcernOptions } from '../../write_concern'; import type { WriteCommandOptions } from './write_command'; /** @internal */ -// FIXME: NODE-2781 export interface CommandOptions extends BSONSerializeOptions, WriteConcernOptions { + // FIXME: NODE-2781 + command?: boolean; slaveOk?: boolean; /** Specify read preference if command supports it */ diff --git a/src/gridfs-stream/upload.ts b/src/gridfs-stream/upload.ts index b839edebd57..c69f45cee10 100644 --- a/src/gridfs-stream/upload.ts +++ b/src/gridfs-stream/upload.ts @@ -515,9 +515,11 @@ function doWrite( function getWriteOptions(stream: GridFSBucketWriteStream): WriteConcernOptions { const obj: WriteConcernOptions = {}; if (stream.writeConcern) { - obj.writeConcern = { w: stream.writeConcern.w }; - obj.writeConcern.wtimeout = stream.writeConcern.wtimeout; - obj.writeConcern.j = stream.writeConcern.j; + obj.writeConcern = { + w: stream.writeConcern.w, + wtimeout: stream.writeConcern.wtimeout, + j: stream.writeConcern.j + }; } return obj; } diff --git a/src/mongo_client.ts b/src/mongo_client.ts index 41a9f003e56..2a0364b8207 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -127,7 +127,6 @@ export interface MongoURIOptions { retryWrites?: boolean; /** Allow a driver to force a Single topology type with a connection string containing one host */ directConnection?: boolean; - // TODO: but now mongo client options accept these at the top level... /** The journal write concern */ journal?: boolean; /** The write concern */ diff --git a/src/operations/connect.ts b/src/operations/connect.ts index 9aea7c75287..d1d44a19e5b 100644 --- a/src/operations/connect.ts +++ b/src/operations/connect.ts @@ -552,23 +552,26 @@ function transformUrlOptions(connStrOptions: any) { connStrOpts.readConcern = new ReadConcern(connStrOpts.readConcernLevel); } + if (connStrOpts.wTimeoutMS) { + connStrOpts.wtimeout = connStrOpts.wTimeoutMS; + connStrOpts.wTimeoutMS = undefined; + } + if (connStrOptions.srvHost) { connStrOpts.srvHost = connStrOptions.srvHost; } - const wc_keys = ['w', 'j', 'journal', 'wtimeout', 'wtimeoutMS', 'fsync']; - const writeConcern = connStrOpts.writeConcern ?? {}; - for (const key of wc_keys) { + // Any write concern options from the URL will be top-level, so we manually + // move them options under `object.writeConcern` + const wcKeys = ['w', 'wtimeout', 'j', 'journal', 'fsync']; + for (const key of wcKeys) { if (connStrOpts[key] !== undefined) { - writeConcern[key] = connStrOpts[key]; + if (connStrOpts.writeConcern === undefined) connStrOpts.writeConcern = {}; + connStrOpts.writeConcern[key] = connStrOpts[key]; connStrOpts[key] = undefined; } } - connStrOpts.writeConcern = writeConcern; - if (connStrOpts.writeConcern.wTimeoutMS) { - connStrOpts.writrConcern.wtimeout = connStrOpts.writeConcern.wTimeoutMS; - } return connStrOpts; } diff --git a/src/write_concern.ts b/src/write_concern.ts index 6f31787afc3..52a70738fb2 100644 --- a/src/write_concern.ts +++ b/src/write_concern.ts @@ -88,7 +88,6 @@ export class WriteConcern { ) { return new WriteConcern(w, wtimeout ?? wtimeoutMS, j ?? journal, fsync); } - return undefined; } } diff --git a/test/tools/runner/config.js b/test/tools/runner/config.js index 2d408e85669..8efa793766c 100644 --- a/test/tools/runner/config.js +++ b/test/tools/runner/config.js @@ -33,7 +33,7 @@ class NativeConfiguration { ); this.writeConcern = function () { - return { writeConcern: { w: 1 } }; //TODO HANA + return { writeConcern: { w: 1 } }; }; }