From 2eb93140f086d5e1c8a227b78bc2cf59e5bad4c6 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 1 Jul 2022 14:33:03 -0400 Subject: [PATCH] fix: keep autoIndex & autoCreate as true by default if read preference is primaryPreferred Fix #11976 Re: #9941 --- lib/helpers/processConnectionOptions.js | 2 +- test/connection.test.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/helpers/processConnectionOptions.js b/lib/helpers/processConnectionOptions.js index 214a0096050..a9d862b1030 100644 --- a/lib/helpers/processConnectionOptions.js +++ b/lib/helpers/processConnectionOptions.js @@ -9,7 +9,7 @@ function processConnectionOptions(uri, options) { ? opts.readPreference : getUriReadPreference(uri); - const resolvedOpts = (readPreference && readPreference !== 'primary') + const resolvedOpts = (readPreference && readPreference !== 'primary' && readPreference !== 'primaryPreferred') ? resolveOptsConflicts(readPreference, opts) : opts; diff --git a/test/connection.test.js b/test/connection.test.js index c586f589982..332fb8e05af 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -1074,6 +1074,13 @@ describe('connections:', function() { assert.equal(conn2.get('autoCreate'), false); }); + it('keeps autoIndex & autoCreate as true by default if read preference is primaryPreferred (gh-9374)', function() { + const conn = new mongoose.createConnection(start.uri, { readPreference: 'primaryPreferred' }); + + assert.equal(conn.get('autoIndex'), undefined); + assert.equal(conn.get('autoCreate'), undefined); + }); + it('throws if options try to set autoIndex to true', function() { const opts = { readPreference: 'secondary',