diff --git a/lib/model.js b/lib/model.js index eebc957e3a4..a9f92b2f612 100644 --- a/lib/model.js +++ b/lib/model.js @@ -63,6 +63,7 @@ const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscrim const pushNestedArrayPaths = require('./helpers/model/pushNestedArrayPaths'); const removeDeselectedForeignField = require('./helpers/populate/removeDeselectedForeignField'); const setDottedPath = require('./helpers/path/setDottedPath'); +const STATES = require('./connectionstate'); const util = require('util'); const utils = require('./utils'); @@ -1352,17 +1353,30 @@ Model.init = function init(callback) { } const Promise = PromiseProvider.get(); - const autoIndex = utils.getOption('autoIndex', - this.schema.options, this.db.config, this.db.base.options); - const autoCreate = utils.getOption('autoCreate', - this.schema.options, this.db.config, this.db.base.options); - - const _ensureIndexes = autoIndex ? - cb => this.ensureIndexes({ _automatic: true }, cb) : - cb => cb(); - const _createCollection = autoCreate ? - cb => this.createCollection({}, cb) : - cb => cb(); + + const model = this; + const _ensureIndexes = function(cb) { + const autoIndex = utils.getOption('autoIndex', + model.schema.options, model.db.config, model.db.base.options); + if (autoIndex) model.ensureIndexes({ _automatic: true }, cb); + else cb(); + }; + const _createCollection = function(cb) { + const conn = model.db; + + if ((conn.readyState === STATES.connecting || conn.readyState === STATES.disconnected) && conn._shouldBufferCommands()) { + conn._queue.push({ fn: _createCollection, ctx: conn, args: [cb] }); + } else { + try { + const autoCreate = utils.getOption('autoCreate', + model.schema.options, model.db.config, model.db.base.options); + if (autoCreate) model.createCollection({}, cb); + else cb(); + } catch (err) { + return cb(err); + } + } + }; this.$init = new Promise((resolve, reject) => { _createCollection(error => { diff --git a/test/connection.test.js b/test/connection.test.js index d27c7879605..71d44efb207 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -1526,4 +1526,23 @@ describe('connections:', function() { const connectionIds = m.connections.map(c => c.id); assert.deepEqual(connectionIds, [1, 2, 3, 4, 5]); }); + + it('with autoCreate = false after schema create (gh-12940)', async function() { + const m = new mongoose.Mongoose(); + + const schema = new Schema({ name: String }, { + collation: { locale: 'en_US', strength: 1 }, + collection: 'gh12940_Conn' + }); + const Model = m.model('gh12940_Conn', schema); + + await m.connect(start.uri, { + autoCreate: false + }); + + await Model.init(); + + const res = await m.connection.db.listCollections().toArray(); + assert.ok(!res.map(c => c.name).includes('gh12940_Conn')); + }); });