Skip to content

Commit

Permalink
fix(collection): skip creating capped collection if autoCreate set …
Browse files Browse the repository at this point in the history
…to `false`

Fix #8566
  • Loading branch information
vkarpov15 committed Feb 15, 2020
1 parent dd01335 commit 447936b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/drivers/node-mongodb-native/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ NativeCollection.prototype.onOpen = function() {
return _this.collection;
}

if (_this.opts.autoCreate === false) {
_this.collection = _this.conn.db.collection(_this.name);
return _this.collection;
}

// capped
return _this.conn.db.collection(_this.name, function(err, c) {
if (err) return callback(err);
Expand Down
1 change: 1 addition & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4700,6 +4700,7 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
const collectionOptions = {
bufferCommands: bufferCommands,
capped: schema.options.capped,
autoCreate: schema.options.autoCreate,
Promise: model.base.Promise
};

Expand Down
26 changes: 26 additions & 0 deletions test/collection.capped.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const start = require('./common');

const assert = require('assert');
const co = require('co');
const random = require('../lib/utils').random;

const mongoose = start.mongoose;
Expand Down Expand Up @@ -92,4 +93,29 @@ describe('collections: capped:', function() {
});
});
});

it('skips when setting autoCreate to false (gh-8566)', function() {
const db = start();
this.timeout(30000);

return co(function*() {
yield db.dropDatabase();

const schema = new mongoose.Schema({
name: String
}, {
capped: { size: 1024 },
bufferCommands: false,
autoCreate: false // disable `autoCreate` since `bufferCommands` is false
});

const Model = db.model('Test', schema);
// Explicitly create the collection before using it
// so the collection is capped.
yield Model.createCollection({ capped: true, size: 1024 });

// Should not throw
yield Model.create({ name: 'test' });
});
});
});

0 comments on commit 447936b

Please sign in to comment.