From 8f03a7e78c2c20ba49239e0776a72d59444c8274 Mon Sep 17 00:00:00 2001 From: Hana Pearlman Date: Fri, 20 Nov 2020 10:39:44 -0500 Subject: [PATCH 01/10] remove top-level wc options --- src/db.ts | 5 +---- src/gridfs-stream/upload.ts | 6 +++--- src/mongo_client.ts | 4 ++-- src/operations/connect.ts | 32 +++++++++++++++++--------------- src/write_concern.ts | 27 +++++++++++++++------------ 5 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/db.ts b/src/db.ts index 600bf1c8044..f76b138e551 100644 --- a/src/db.ts +++ b/src/db.ts @@ -55,10 +55,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 3c6fff80ff3..d47fa1ab189 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 ebb4f8b04da..ebf0152925e 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, ReadPreferenceModeId } from './read_preference'; import { MongoError, AnyError } from './error'; -import { WriteConcern, WriteConcernOptions } from './write_concern'; +import { WriteConcern, WriteConcernOptions, W } from './write_concern'; import { maybePromise, MongoDBNamespace, Callback, resolveOptions } from './utils'; import { deprecate } from 'util'; import { connect, validOptions } from './operations/connect'; @@ -60,7 +60,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. */ diff --git a/src/operations/connect.ts b/src/operations/connect.ts index cf19c0f9908..4b0923bfdff 100644 --- a/src/operations/connect.ts +++ b/src/operations/connect.ts @@ -40,9 +40,7 @@ const validOptionNames = [ 'acceptableLatencyMS', 'connectWithNoPrimary', 'authSource', - 'w', - 'wtimeout', - 'j', + 'writeConcern', 'forceServerObjectId', 'serializeFunctions', 'ignoreUndefined', @@ -68,7 +66,6 @@ const validOptionNames = [ 'password', 'authMechanism', 'compression', - 'fsync', 'readPreferenceTags', 'numberOfRetries', 'auto_reconnect', @@ -216,12 +213,6 @@ export function connect( delete finalOptions.db_options.auth; } - // `journal` should be translated to `j` for the driver - if (finalOptions.journal != null) { - finalOptions.j = finalOptions.journal; - finalOptions.journal = undefined; - } - // resolve tls options if needed resolveTLSOptions(finalOptions); @@ -412,7 +403,9 @@ function createUnifiedOptions(finalOptions: any, options: any) { const noMerge = ['readconcern', 'compression', 'autoencryption']; for (const name in options) { - if (noMerge.indexOf(name.toLowerCase()) !== -1) { + if (name === 'writeConcern') { + finalOptions[name] = { ...finalOptions[name], ...options[name] }; + } else if (noMerge.indexOf(name.toLowerCase()) !== -1) { finalOptions[name] = options[name]; } else if (childOptions.indexOf(name.toLowerCase()) !== -1) { finalOptions = mergeOptions(finalOptions, options[name], false); @@ -549,14 +542,23 @@ function transformUrlOptions(connStrOptions: any) { connStrOpts.readConcern = new ReadConcern(connStrOpts.readConcernLevel); } - if (connStrOpts.wTimeoutMS) { - connStrOpts.wtimeout = connStrOpts.wTimeoutMS; - } - 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) { + if (connStrOpts[key] !== undefined) { + 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 a97b914f8e2..aa7ecb1d5d7 100644 --- a/src/write_concern.ts +++ b/src/write_concern.ts @@ -3,6 +3,12 @@ export type W = number | 'majority'; /** @public */ export interface WriteConcernOptions { + /** Write Concern as an object */ + writeConcern?: WriteConcern | WriteConcernSettings; +} + +/** @public */ +export interface WriteConcernSettings { /** The write concern */ w?: W; /** The write concern timeout */ @@ -15,8 +21,6 @@ export interface WriteConcernOptions { journal?: boolean; /** The file sync write concern */ fsync?: boolean | 1; - /** Write Concern as an object */ - writeConcern?: WriteConcernOptions | WriteConcern | W; } /** @@ -64,19 +68,17 @@ export class WriteConcern { /** Construct a WriteConcern given an options object. */ static fromOptions( - options?: WriteConcernOptions | WriteConcern | W, + options?: WriteConcernOptions | WriteConcern, inherit?: WriteConcernOptions | WriteConcern ): WriteConcern | undefined { - const { fromOptions } = WriteConcern; if (typeof options === 'undefined') return undefined; - if (typeof options === 'number') return fromOptions({ ...inherit, w: options }); - if (typeof options === 'string') return fromOptions({ ...inherit, w: options }); - if (options instanceof WriteConcern) return fromOptions({ ...inherit, ...options }); - if (options.writeConcern) { - const { writeConcern, ...viable } = { ...inherit, ...options }; - return fromOptions(writeConcern, viable); - } - const { w, wtimeout, j, fsync, journal, wtimeoutMS } = { ...inherit, ...options }; + inherit = inherit ?? {}; + const opts: WriteConcern | WriteConcernSettings | undefined = + options instanceof WriteConcern ? options : options.writeConcern; + const parentOpts: WriteConcern | WriteConcernSettings | undefined = + inherit instanceof WriteConcern ? inherit : inherit.writeConcern; + + const { w, wtimeout, j, fsync, journal, wtimeoutMS } = { ...parentOpts, ...opts }; if ( w != null || wtimeout != null || @@ -87,6 +89,7 @@ export class WriteConcern { ) { return new WriteConcern(w, wtimeout ?? wtimeoutMS, j ?? journal, fsync); } + return undefined; } } From 5506e6a5d7cf8190a069a2b23508f3bc95f8ad79 Mon Sep 17 00:00:00 2001 From: Hana Pearlman Date: Fri, 20 Nov 2020 10:40:03 -0500 Subject: [PATCH 02/10] changes to pass tests --- test/functional/apm.test.js | 9 +---- test/functional/bulk.test.js | 10 ++--- test/functional/collection.test.js | 6 +-- test/functional/find_and_modify.test.js | 39 +++++++++++-------- test/functional/insert.test.js | 7 +++- 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 ++++- 11 files changed, 70 insertions(+), 51 deletions(-) diff --git a/test/functional/apm.test.js b/test/functional/apm.test.js index a80e40826c5..05fad9e341a 100644 --- a/test/functional/apm.test.js +++ b/test/functional/apm.test.js @@ -971,13 +971,8 @@ describe('APM', function () { } 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 6248cee1681..69c9315607b 100644 --- a/test/functional/bulk.test.js +++ b/test/functional/bulk.test.js @@ -78,7 +78,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); @@ -622,7 +622,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); @@ -1128,7 +1128,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); @@ -1195,7 +1195,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); @@ -1297,7 +1297,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 7c4f95dfe18..7cd3ea75785 100644 --- a/test/functional/collection.test.js +++ b/test/functional/collection.test.js @@ -666,7 +666,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); @@ -850,7 +850,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); @@ -936,7 +936,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/find_and_modify.test.js b/test/functional/find_and_modify.test.js index 53a91e6b7ed..9b7266333fe 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 0ad97c97a9e..105a4620ea3 100644 --- a/test/functional/insert.test.js +++ b/test/functional/insert.test.js @@ -1439,7 +1439,12 @@ describe('Insert', function () { client.connect(function (err, client) { var db = client.db(configuration.db); var collection = db.collection('gh-completely3'); - collection.update({ a: 1 }, { $set: { a: 2 } }, { upsert: true, w: 0 }, cb); + collection.update( + { a: 1 }, + { $set: { 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 4ce2dce6958..16be1024734 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' }, forceServerObjectId: true, diff --git a/test/functional/operation_promises_example.test.js b/test/functional/operation_promises_example.test.js index 45f14e9b903..8e031d83f7b 100644 --- a/test/functional/operation_promises_example.test.js +++ b/test/functional/operation_promises_example.test.js @@ -2020,12 +2020,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 da772b606a0..c67c39a5405 100644 --- a/test/functional/promises_collection.test.js +++ b/test/functional/promises_collection.test.js @@ -264,7 +264,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() @@ -297,7 +299,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 9bfcad3f5b3..ba8a990af8f 100644 --- a/test/functional/spec-runner/index.js +++ b/test/functional/spec-runner/index.js @@ -180,7 +180,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; }) @@ -188,7 +188,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; @@ -196,13 +196,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 }; } @@ -211,7 +213,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 1a02980a6a7..9196cb97b6f 100644 --- a/test/tools/runner/config.js +++ b/test/tools/runner/config.js @@ -105,6 +105,12 @@ class NativeConfiguration { Object.assign(dbOptions, { replicaSet: this.options.replicaSet }); } + // 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, @@ -210,10 +216,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 ad397c5c007376754ef70563648695c216c89102 Mon Sep 17 00:00:00 2001 From: Hana Pearlman Date: Fri, 20 Nov 2020 13:07:11 -0500 Subject: [PATCH 03/10] 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/spec-runner/index.js | 11 ++++- 10 files changed, 112 insertions(+), 74 deletions(-) diff --git a/src/operations/map_reduce.ts b/src/operations/map_reduce.ts index 503d99099ef..5ad1c6c7d1d 100644 --- a/src/operations/map_reduce.ts +++ b/src/operations/map_reduce.ts @@ -24,9 +24,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 939cbd93bc5..299180c3f3c 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.insertMany(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.insertMany(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.insertMany(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.insertMany(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(err).to.not.exist; expect(result).to.exist; @@ -426,7 +426,7 @@ describe('Aggregation', function () { // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs - collection.insertMany(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(result).to.exist; expect(err).to.not.exist; @@ -515,7 +515,7 @@ describe('Aggregation', function () { // Create a collection const collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs - collection.insertMany(docs, { w: 1 }, (err, result) => { + collection.insert(docs, { writeConcern: { w: 1 } }, (err, result) => { expect(result).to.exist; expect(err).to.not.exist; @@ -604,7 +604,7 @@ describe('Aggregation', function () { // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs - collection.insertMany(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(result).to.exist; expect(err).to.not.exist; @@ -689,7 +689,7 @@ describe('Aggregation', function () { // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGet'); // Insert the docs - collection.insertMany(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(result).to.exist; expect(err).to.not.exist; @@ -885,7 +885,7 @@ describe('Aggregation', function () { // Create a collection var collection = db.collection('shouldCorrectlyDoAggWithCursorGetStream'); // Insert the docs - collection.insertMany(docs, { w: 1 }, function (err, result) { + collection.insert(docs, { writeConcern: { w: 1 } }, function (err, result) { expect(result).to.exist; expect(err).to.not.exist; @@ -1027,7 +1027,7 @@ describe('Aggregation', function () { // Create a collection const collection = db.collection('shouldCorrectlyDoAggWithCursorMaxTimeMSSet'); // Insert the docs - collection.insertMany(docs, { w: 1 }, (err, result) => { + collection.insert(docs, { writeConcern: { w: 1 } }, (err, result) => { expect(result).to.exist; expect(err).to.not.exist; @@ -1197,7 +1197,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; @@ -1249,7 +1249,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 05fad9e341a..f9d55c704ce 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).property('insertedCount').to.equal(6); @@ -528,7 +530,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).property('insertedCount').to.equal(6); diff --git a/test/functional/causal_consistency.test.js b/test/functional/causal_consistency.test.js index d187b3e21e8..7a2093b5e55 100644 --- a/test/functional/causal_consistency.test.js +++ b/test/functional/causal_consistency.test.js @@ -203,7 +203,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 cb77a6cb956..db64a3f4cd6 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)) + ); + } + ); }); }); } @@ -2536,7 +2540,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 => { @@ -2564,7 +2568,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({ @@ -2584,7 +2588,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; @@ -2629,7 +2633,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 } }); }); }); } @@ -2669,8 +2673,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 7cd3ea75785..b824690f30c 100644 --- a/test/functional/collection.test.js +++ b/test/functional/collection.test.js @@ -491,23 +491,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(); + }); + } + ); }); }); @@ -628,7 +633,7 @@ describe('Collection', function () { ) { collection.createIndex( { createdAt: 1 }, - { expireAfterSeconds: 1, w: 1 }, + { expireAfterSeconds: 1, writeConcern: { w: 1 } }, errorCallBack ); } else if ( @@ -636,7 +641,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 5747f461b88..98c40d66083 100644 --- a/test/functional/connection.test.js +++ b/test/functional/connection.test.js @@ -42,18 +42,22 @@ describe('Connection', function () { expect(err).to.not.exist; var db = client.db(configuration.db); - 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 () { expect(err).to.not.exist; var db = client.db(configuration.db); - 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/spec-runner/index.js b/test/functional/spec-runner/index.js index ba8a990af8f..4a601598e6e 100644 --- a/test/functional/spec-runner/index.js +++ b/test/functional/spec-runner/index.js @@ -31,7 +31,16 @@ function escape(string) { 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 982896f8152c43320e4295c23d50af9375f193d5 Mon Sep 17 00:00:00 2001 From: Hana Pearlman Date: Fri, 20 Nov 2020 14:56:21 -0500 Subject: [PATCH 04/10] 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/index.test.js | 141 +++++---- test/functional/insert.test.js | 69 ++--- test/functional/maxtimems.test.js | 6 +- test/functional/object_id.test.js | 42 +-- test/functional/operation_example.test.js | 255 +++++++++-------- .../operation_generators_example.test.js | 106 ++++--- .../operation_promises_example.test.js | 100 ++++--- 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 | 17 +- test/tools/runner/config.js | 2 +- 23 files changed, 761 insertions(+), 590 deletions(-) diff --git a/src/bulk/common.ts b/src/bulk/common.ts index 65c3882557f..7136512686e 100644 --- a/src/bulk/common.ts +++ b/src/bulk/common.ts @@ -1084,7 +1084,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 7b215dc1c79..210cd2034df 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 c0c599bd965..3e8847c19a5 100644 --- a/test/functional/crud_api.test.js +++ b/test/functional/crud_api.test.js @@ -323,7 +323,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('insertedId').to.exist; insertMany(); @@ -335,7 +335,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; expect(r).property('insertedCount').to.equal(2); @@ -348,7 +348,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); @@ -361,7 +364,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) { expect(err).to.not.exist; test.equal(3, r.nInserted); @@ -387,49 +390,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; - expect(r).property('insertedCount').to.equal(3); - - 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; + expect(r).property('insertedCount').to.equal(3); + + 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); @@ -442,7 +449,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); @@ -467,7 +474,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); @@ -480,7 +490,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); @@ -538,7 +548,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); @@ -593,35 +606,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(); @@ -646,51 +663,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(); @@ -715,7 +744,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); @@ -737,7 +769,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); @@ -766,7 +801,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); @@ -835,28 +873,34 @@ 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; expect(result).property('acknowledged').to.be.false; expect(result).property('insertedId').to.exist; - 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; @@ -888,7 +932,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); @@ -958,7 +1002,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); }); @@ -989,7 +1035,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 d40d7b8339e..fe3975ee7e8 100644 --- a/test/functional/cursor.test.js +++ b/test/functional/cursor.test.js @@ -2627,7 +2627,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 => { @@ -3193,7 +3193,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 @@ -3280,7 +3280,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 211d6fa8c3c..58f5fe585f1 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 0c3a314505b..0526810bddc 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 b9ecf3a2113..98d2103504f 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 7c0a3c40ba9..e30a75e27d8 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 9fd335aef22..d21f73909b3 100644 --- a/test/functional/find.test.js +++ b/test/functional/find.test.js @@ -666,17 +666,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); + } + ); }); }); }); @@ -995,7 +996,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( @@ -1201,33 +1204,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); + } + ); + } + ); + } + ); }); }); } @@ -1735,7 +1742,9 @@ describe('Find', function () { expect(err).to.not.exist; var id = r.insertedIds[1]; // 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/index.test.js b/test/functional/index.test.js index 10b3a702469..99b0a504cef 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); }); @@ -1303,19 +1323,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 105a4620ea3..b90fa4ef84d 100644 --- a/test/functional/insert.test.js +++ b/test/functional/insert.test.js @@ -517,19 +517,20 @@ 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.findOne({ _id: result.insertedIds[0] }, function (err, object) { + collection.insert( + { i: 1, z: func }, + { writeConcern: { w: 1 }, serializeFunctions: true }, + function (err, result) { expect(err).to.not.exist; - test.equal(normalizedFunctionString(func), object.z.code); - test.equal(1, object.i); - client.close(done); - }); - }); + + collection.findOne({ _id: result.insertedIds[0] }, function (err, object) { + expect(err).to.not.exist; + test.equal(normalizedFunctionString(func), object.z.code); + test.equal(1, object.i); + client.close(done); + }); + } + ); }); } }); @@ -553,7 +554,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; @@ -953,7 +954,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) { expect(err).to.not.exist; expect(result).property('matchedCount').to.equal(1); @@ -1201,23 +1202,27 @@ 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; - expect(result).property('upsertedCount').to.equal(1); - - // Upsert an existing doc - collection.update({ a: 1 }, { $set: { a: 1 } }, { upsert: true, w: 1 }, function ( - err, - result - ) { + collection.update( + { a: 1 }, + { $set: { a: 1 } }, + { upsert: true, writeConcern: { w: 1 } }, + function (err, result) { expect(err).to.not.exist; - expect(result).property('matchedCount').to.equal(1); - client.close(done); - }); - }); + expect(result).property('upsertedCount').to.equal(1); + + // Upsert an existing doc + collection.update( + { a: 1 }, + { $set: { a: 1 } }, + { upsert: true, writeConcern: { w: 1 } }, + function (err, result) { + expect(err).to.not.exist; + expect(result).property('matchedCount').to.equal(1); + client.close(done); + } + ); + } + ); }); } }); @@ -1416,7 +1421,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); }); } }); @@ -1467,7 +1472,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 ce477692e83..166a7c1bb84 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 6c2cb43f2f0..2c1ec2e5e48 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) { expect(r).property('insertedCount').to.equal(1); const id = r.insertedIds[0]; @@ -37,7 +37,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) { expect(r).property('insertedCount').to.equal(1); const id = r.insertedIds[0]; @@ -54,22 +54,26 @@ 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) { - expect(err).to.not.exist; - expect(r).property('insertedCount').to.equal(1); + collection.insert( + { _id: objectId, name: 'Donald', age: 95 }, + { writeConcern: { w: 1 } }, + function (err, r) { + expect(err).to.not.exist; + expect(r).property('insertedCount').to.equal(1); - const id = r.insertedIds[0]; - expect(id.toHexString().length).to.equal(24); - expect(id.toHexString()).to.equal(objectId.toHexString()); + const id = r.insertedIds[0]; + expect(id.toHexString().length).to.equal(24); + expect(id.toHexString()).to.equal(objectId.toHexString()); - // Locate the first document inserted - collection.findOne(id, function (err, document) { - expect(err).to.not.exist; - expect(id.toHexString()).to.equal(document._id.toHexString()); - expect(objectId.toHexString()).to.equal(document._id.toHexString()); - number_of_tests_done++; - }); - }); + // Locate the first document inserted + collection.findOne(id, function (err, document) { + expect(err).to.not.exist; + expect(id.toHexString()).to.equal(document._id.toHexString()); + expect(objectId.toHexString()).to.equal(document._id.toHexString()); + number_of_tests_done++; + }); + } + ); var intervalId = setInterval(function () { if (number_of_tests_done === 3) { @@ -128,7 +132,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); @@ -192,12 +196,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 1fd9c95aad3..1404d5ea3ec 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; - expect(result).property('upsertedCount').to.equal(1); - - // 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; @@ -3856,7 +3867,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; @@ -3928,7 +3939,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; @@ -4104,11 +4115,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; @@ -4284,7 +4295,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; @@ -4335,7 +4346,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; @@ -4404,7 +4415,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; @@ -4657,7 +4668,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; @@ -4706,7 +4717,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; @@ -5613,7 +5624,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; @@ -5680,7 +5691,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; @@ -6483,7 +6494,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); @@ -6536,7 +6547,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; expect(r).property('insertedCount').to.equal(1); @@ -6584,7 +6595,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; expect(r).property('insertedCount').to.equal(1); @@ -6640,7 +6651,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; expect(r).property('insertedCount').to.equal(1); @@ -6699,7 +6710,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 6bfe1b263ff..fe3caf5661d 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 } } ); expect(result).property('upsertedCount').to.equal(1); @@ -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,8 +4463,8 @@ 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 }); - expect(r).property('insertedCount').to.equal(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 } }); test.equal(1, r.lastErrorObject.n); @@ -4490,8 +4508,8 @@ 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 }); - expect(r).property('insertedCount').to.equal(1); + var r = yield col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }); + test.equal(1, r.result.n); r = yield col.findOneAndReplace( { a: 1 }, @@ -4545,8 +4563,8 @@ 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 }); - expect(r).property('insertedCount').to.equal(1); + var r = yield col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }); + test.equal(1, r.result.n); r = yield col.findOneAndUpdate( { a: 1 }, @@ -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 8e031d83f7b..52f86c121e2 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); @@ -285,7 +285,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); @@ -556,13 +559,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); @@ -630,7 +636,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) { @@ -688,13 +694,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); @@ -941,7 +950,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) { @@ -1095,7 +1104,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 () { @@ -1174,7 +1183,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 }); @@ -1258,7 +1267,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); @@ -1351,7 +1360,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); @@ -1539,7 +1548,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); @@ -1606,13 +1618,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); @@ -1841,7 +1856,7 @@ describe('Operation (Promises)', function () { { name: 'Sarah', title: 'Princess' }, { name: 'Gump', title: 'Gump' } ], - { w: 1, keepGoing: true } + { writeConcern: { w: 1 }, keepGoing: true } ); }) .catch(function () { @@ -1971,7 +1986,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); @@ -2243,7 +2258,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) { expect(result).property('upsertedCount').to.equal(1); @@ -2400,21 +2415,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) { @@ -2807,7 +2825,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 @@ -2857,7 +2875,7 @@ describe('Operation (Promises)', function () { capped: true, size: 10000, max: 1000, - w: 1 + writeConcern: { w: 1 } }); }) .then(function (collection) { @@ -3046,7 +3064,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) { @@ -3115,7 +3133,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) { @@ -3277,10 +3295,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); @@ -3414,7 +3432,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 @@ -3465,7 +3483,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 @@ -3546,7 +3564,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 @@ -3613,7 +3631,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 @@ -3830,7 +3848,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 @@ -4688,7 +4706,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); @@ -4730,7 +4748,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()); @@ -4769,7 +4787,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) { expect(r).property('insertedCount').to.equal(1); return col.findOneAndDelete({ a: 1 }, { projection: { b: 1 }, sort: { a: 1 } }); @@ -4812,8 +4830,8 @@ 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) { - expect(r).property('insertedCount').to.equal(1); + return col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }).then(function (r) { + test.equal(1, r.result.n); return col .findOneAndReplace( @@ -4867,7 +4885,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) { expect(r).property('insertedCount').to.equal(1); @@ -4926,7 +4944,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 c67c39a5405..510c252003a 100644 --- a/test/functional/promises_collection.test.js +++ b/test/functional/promises_collection.test.js @@ -64,7 +64,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) { expect(r).property('insertedCount').to.equal(1); col @@ -110,7 +110,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) { expect(r).property('insertedCount').to.equal(1); col @@ -158,7 +158,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) { expect(r).property('insertedCount').to.equal(1); col diff --git a/test/functional/raw.test.js b/test/functional/raw.test.js index e604f1d08d6..c3cf59f015d 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 7042d13da79..72caf51842a 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 f4d92dd5f1a..11a3763c8e7 100644 --- a/test/functional/sessions.test.js +++ b/test/functional/sessions.test.js @@ -199,7 +199,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 @@ -218,7 +218,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 5d1368f4a69..45594841abd 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); @@ -92,11 +92,14 @@ 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().forEach( doc => { @@ -131,7 +134,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['客家话']); @@ -159,7 +162,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 9196cb97b6f..ab8e9fcd991 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 382339919a846df2faa69ec9a67ec272c20658ae Mon Sep 17 00:00:00 2001 From: Hana Pearlman Date: Fri, 20 Nov 2020 15:23:14 -0500 Subject: [PATCH 05/10] small cleanup --- src/gridfs-stream/upload.ts | 8 +++++--- src/operations/connect.ts | 19 +++++++++++-------- src/write_concern.ts | 1 - test/tools/runner/config.js | 2 +- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/gridfs-stream/upload.ts b/src/gridfs-stream/upload.ts index d47fa1ab189..c605b8226e8 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/operations/connect.ts b/src/operations/connect.ts index 4b0923bfdff..3e7768068ca 100644 --- a/src/operations/connect.ts +++ b/src/operations/connect.ts @@ -542,23 +542,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 aa7ecb1d5d7..921cdd282b1 100644 --- a/src/write_concern.ts +++ b/src/write_concern.ts @@ -89,7 +89,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 ab8e9fcd991..c0578e7770e 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 } }; }; } From d5af9f76ff89a32824e4f310b3e8ad98362d6deb Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Thu, 3 Dec 2020 16:41:27 -0500 Subject: [PATCH 06/10] rebase fixes --- test/functional/find.test.js | 4 +-- test/functional/insert.test.js | 4 +-- test/functional/unicode.test.js | 52 +++++++++++++-------------------- 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/test/functional/find.test.js b/test/functional/find.test.js index d21f73909b3..c3cbb12eaef 100644 --- a/test/functional/find.test.js +++ b/test/functional/find.test.js @@ -1330,8 +1330,8 @@ describe('Find', function () { ) { var _lowerId = new ObjectId(); var _higherId = new ObjectId(); - var lowerId = new Long.fromString('133118461172916224', 10); - var higherId = new Long.fromString('133118461172916225', 10); + var lowerId = Long.fromString('133118461172916224', 10); + var higherId = Long.fromString('133118461172916225', 10); var lowerDoc = { _id: _lowerId, id: lowerId }; var higherDoc = { _id: _higherId, id: higherId }; diff --git a/test/functional/insert.test.js b/test/functional/insert.test.js index b90fa4ef84d..f9686feaa8f 100644 --- a/test/functional/insert.test.js +++ b/test/functional/insert.test.js @@ -700,11 +700,11 @@ describe('Insert', function () { collection.find().toArray(function (err, items) { test.equal('shouldCorrectlyInsertDBRefWithDbNotDefined', items[1].ref.namespace); test.equal(doc._id.toString(), items[1].ref.oid.toString()); - expect(items[1].ref.db).to.be.null; + expect(items[1].ref.db).to.not.exist; test.equal('shouldCorrectlyInsertDBRefWithDbNotDefined', items[2].ref.namespace); test.equal(doc._id.toString(), items[2].ref.oid.toString()); - expect(items[2].ref.db).to.be.null; + expect(items[2].ref.db).to.not.exist; client.close(done); }); diff --git a/test/functional/unicode.test.js b/test/functional/unicode.test.js index 45594841abd..bde972e70b2 100644 --- a/test/functional/unicode.test.js +++ b/test/functional/unicode.test.js @@ -79,43 +79,33 @@ describe('Unicode', function () { } }); - it('shouldCorrectlyInsertUnicodeCharacters', { - metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } - }, - - test: function (done) { - var configuration = this.configuration; - var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 }); - client.connect(function (err, client) { + it('should Correctly Insert Unicode Characters', function (done) { + const client = this.configuration.newClient(this.configuration.writeConcernMax(), { + maxPoolSize: 1 + }); + client.connect((err, client) => { + expect(err).to.not.exist; + const db = client.db(this.configuration.db); + db.createCollection('unicode_test_collection', (err, collection) => { expect(err).to.not.exist; - 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] }, { writeConcern: { w: 1 } }, function ( - err - ) { + const test_strings = ['ouooueauiOUOOUEAUI', 'öüóőúéáűíÖÜÓŐÚÉÁŰÍ', '本荘由利地域に洪水警報']; + collection.insert({ id: 0, text: test_strings[0] }, { writeConcern: { w: 1 } }, err => { + expect(err).to.not.exist; + collection.insert({ id: 1, text: test_strings[1] }, { writeConcern: { w: 1 } }, err => { expect(err).to.not.exist; - collection.insert( - { id: 1, text: test_strings[1] }, - { writeConcern: { w: 1 } }, - function (err) { + collection.find().forEach( + doc => { + expect(doc).property('text').to.equal(test_strings[doc.id]); + }, + err => { expect(err).to.not.exist; - collection.find().forEach( - doc => { - expect(doc).property('text').to.equal(test_strings[doc.id]); - }, - err => { - expect(err).to.not.exist; - client.close(done); - } - ); - }); - }); + client.close(done); + } + ); }); }); }); - } + }); }); it('shouldCreateObjectWithChineseObjectName', { From f35bbd4f2277a52e80af525895d447dd69747066 Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Thu, 3 Dec 2020 18:43:19 -0500 Subject: [PATCH 07/10] rebase fixes --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 625d0eb4a56..11594192fc6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -295,7 +295,7 @@ export type { InterruptibleAsyncInterval, BufferPool } from './utils'; -export type { WriteConcern, W, WriteConcernOptions } from './write_concern'; +export type { WriteConcern, W, WriteConcernOptions, WriteConcernSettings } from './write_concern'; export type { ExecutionResult } from './operations/execute_operation'; export type { InternalAbstractCursorOptions } from './cursor/abstract_cursor'; export type { From fd9b1f5d757806d822df31f635be61be7d9b003e Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Fri, 4 Dec 2020 10:40:26 -0500 Subject: [PATCH 08/10] bring back WriteConcern keys array --- src/operations/connect.ts | 4 ++-- src/write_concern.ts | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/operations/connect.ts b/src/operations/connect.ts index 3e7768068ca..bfd8bc1cb45 100644 --- a/src/operations/connect.ts +++ b/src/operations/connect.ts @@ -14,6 +14,7 @@ import type { MongoClient } from '../mongo_client'; import { ConnectionOptions, Connection } from '../cmap/connection'; import { AuthMechanism, AuthMechanismId } from '../cmap/auth/defaultAuthProviders'; import { Server } from '../sdam/server'; +import { WRITE_CONCERN_KEYS } from '../write_concern'; const validOptionNames = [ 'poolSize', @@ -553,8 +554,7 @@ function transformUrlOptions(connStrOptions: any) { // 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) { + for (const key of WRITE_CONCERN_KEYS) { if (connStrOpts[key] !== undefined) { if (connStrOpts.writeConcern === undefined) connStrOpts.writeConcern = {}; connStrOpts.writeConcern[key] = connStrOpts[key]; diff --git a/src/write_concern.ts b/src/write_concern.ts index 921cdd282b1..622fdd1c234 100644 --- a/src/write_concern.ts +++ b/src/write_concern.ts @@ -23,6 +23,8 @@ export interface WriteConcernSettings { fsync?: boolean | 1; } +export const WRITE_CONCERN_KEYS = ['w', 'wtimeout', 'j', 'journal', 'fsync']; + /** * A MongoDB WriteConcern, which describes the level of acknowledgement * requested from MongoDB for write operations. From 92cb64ce2252ac15ae0641d04e1348fc42e7839a Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Mon, 7 Dec 2020 13:55:21 -0500 Subject: [PATCH 09/10] rebase fixes --- test/functional/operation_example.test.js | 2 +- test/functional/operation_generators_example.test.js | 6 +++--- test/functional/operation_promises_example.test.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/functional/operation_example.test.js b/test/functional/operation_example.test.js index 1404d5ea3ec..2f160c98c14 100644 --- a/test/functional/operation_example.test.js +++ b/test/functional/operation_example.test.js @@ -2934,7 +2934,7 @@ describe('Operation Examples', function () { { upsert: true, writeConcern: { w: 1 } }, function (err, result) { expect(err).to.not.exist; - test.equal(1, result.result.n); + expect(result).property('upsertedCount').to.equal(1); // Fetch the document that we modified and check if it got inserted correctly collection.findOne({ a: 1 }, function (err, item) { diff --git a/test/functional/operation_generators_example.test.js b/test/functional/operation_generators_example.test.js index fe3caf5661d..144d33676d8 100644 --- a/test/functional/operation_generators_example.test.js +++ b/test/functional/operation_generators_example.test.js @@ -4464,7 +4464,7 @@ describe('Operation (Generators)', function () { // Get the collection var col = db.collection('find_one_and_delete_with_generators'); var r = yield col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }); - test.equal(1, r.result.n); + expect(r).property('insertedCount').to.equal(1); r = yield col.findOneAndDelete({ a: 1 }, { projection: { b: 1 }, sort: { a: 1 } }); test.equal(1, r.lastErrorObject.n); @@ -4509,7 +4509,7 @@ describe('Operation (Generators)', function () { // Get the collection var col = db.collection('find_one_and_replace_with_generators'); var r = yield col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }); - test.equal(1, r.result.n); + expect(r).property('insertedCount').to.equal(1); r = yield col.findOneAndReplace( { a: 1 }, @@ -4564,7 +4564,7 @@ describe('Operation (Generators)', function () { // Get the collection var col = db.collection('find_one_and_update_with_generators'); var r = yield col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }); - test.equal(1, r.result.n); + expect(r).property('insertedCount').to.equal(1); r = yield col.findOneAndUpdate( { a: 1 }, diff --git a/test/functional/operation_promises_example.test.js b/test/functional/operation_promises_example.test.js index 52f86c121e2..8d27856c6bd 100644 --- a/test/functional/operation_promises_example.test.js +++ b/test/functional/operation_promises_example.test.js @@ -4831,7 +4831,7 @@ describe('Operation (Promises)', function () { // Get the collection var col = db.collection('find_one_and_replace_with_promise'); return col.insertMany([{ a: 1, b: 1 }], { writeConcern: { w: 1 } }).then(function (r) { - test.equal(1, r.result.n); + expect(r).property('insertedCount').to.equal(1); return col .findOneAndReplace( From e1246db8c94d1019d42d20c8905379c120e5ccd3 Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Tue, 15 Dec 2020 11:44:43 -0500 Subject: [PATCH 10/10] Fix wc spread at the mongoclient level remains --- src/connection_string.ts | 39 +++++++++++++++++++++++++-------------- src/mongo_client.ts | 16 +++++++++++----- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/connection_string.ts b/src/connection_string.ts index 5307776d021..c0676bf2125 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -1199,8 +1199,10 @@ export const OPTIONS = { target: 'writeConcern', transform({ name, options, values: [value] }): WriteConcern { const wc = WriteConcern.fromOptions({ - ...options.writeConcern, - fsync: getBoolean(name, value) + writeConcern: { + ...options.writeConcern, + fsync: getBoolean(name, value) + } }); if (!wc) throw new TypeError(`Unable to make a writeConcern from fsync=${value}`); return wc; @@ -1216,10 +1218,11 @@ export const OPTIONS = { j: { target: 'writeConcern', transform({ name, options, values: [value] }): WriteConcern { - console.warn('j is deprecated'); const wc = WriteConcern.fromOptions({ - ...options.writeConcern, - journal: getBoolean(name, value) + writeConcern: { + ...options.writeConcern, + journal: getBoolean(name, value) + } }); if (!wc) throw new TypeError(`Unable to make a writeConcern from journal=${value}`); return wc; @@ -1229,8 +1232,10 @@ export const OPTIONS = { target: 'writeConcern', transform({ name, options, values: [value] }): WriteConcern { const wc = WriteConcern.fromOptions({ - ...options.writeConcern, - journal: getBoolean(name, value) + writeConcern: { + ...options.writeConcern, + journal: getBoolean(name, value) + } }); if (!wc) throw new TypeError(`Unable to make a writeConcern from journal=${value}`); return wc; @@ -1516,7 +1521,7 @@ export const OPTIONS = { w: { target: 'writeConcern', transform({ values: [value], options }) { - return WriteConcern.fromOptions({ ...options.writeConcern, w: value as W }); + return WriteConcern.fromOptions({ writeConcern: { ...options.writeConcern, w: value as W } }); } }, waitQueueTimeoutMS: { @@ -1528,8 +1533,10 @@ export const OPTIONS = { transform({ values: [value], options }) { if (isRecord(value)) { return WriteConcern.fromOptions({ - ...options.writeConcern, - ...value + writeConcern: { + ...options.writeConcern, + ...value + } }); } throw new MongoParseError(`WriteConcern must be an object, got ${JSON.stringify(value)}`); @@ -1539,8 +1546,10 @@ export const OPTIONS = { target: 'writeConcern', transform({ values: [value], options }) { const wc = WriteConcern.fromOptions({ - ...options.writeConcern, - wtimeout: getUint('wtimeout', value) + writeConcern: { + ...options.writeConcern, + wtimeout: getUint('wtimeout', value) + } }); if (wc) return wc; throw new MongoParseError(`Cannot make WriteConcern from wtimeout`); @@ -1550,8 +1559,10 @@ export const OPTIONS = { target: 'writeConcern', transform({ values: [value], options }) { const wc = WriteConcern.fromOptions({ - ...options.writeConcern, - wtimeoutMS: getUint('wtimeoutMS', value) + writeConcern: { + ...options.writeConcern, + wtimeoutMS: getUint('wtimeoutMS', value) + } }); if (wc) return wc; throw new MongoParseError(`Cannot make WriteConcern from wtimeout`); diff --git a/src/mongo_client.ts b/src/mongo_client.ts index ebf0152925e..cdfc8f984cd 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, ReadPreferenceModeId } from './read_preference'; import { MongoError, AnyError } from './error'; -import { WriteConcern, WriteConcernOptions, W } from './write_concern'; +import { WriteConcern, W, WriteConcernSettings } from './write_concern'; import { maybePromise, MongoDBNamespace, Callback, resolveOptions } from './utils'; import { deprecate } from 'util'; import { connect, validOptions } from './operations/connect'; @@ -134,13 +134,19 @@ export interface MongoURIOptions { // username and password in Authority section not query string. username?: string; password?: string; + + // remove in NODE-2704 + fsync?: boolean; + w?: W; + j?: boolean; + journal?: boolean; + wtimeout?: number; + wtimeoutMS?: number; + writeConcern?: WriteConcern | WriteConcernSettings; } /** @public */ -export interface MongoClientOptions - extends WriteConcernOptions, - MongoURIOptions, - BSONSerializeOptions { +export interface MongoClientOptions extends MongoURIOptions, BSONSerializeOptions { /** Validate mongod server certificate against Certificate Authority */ sslValidate?: boolean; /** SSL Certificate store binary buffer. */