Skip to content

Commit

Permalink
Merge pull request #13286 from jakesjews/master
Browse files Browse the repository at this point in the history
add clusteredIndex to schema options
  • Loading branch information
vkarpov15 committed Apr 20, 2023
2 parents 0df2af3 + b3c64e5 commit dee524b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,14 @@ Model.createCollection = async function createCollection(options) {
}
}

const clusteredIndex = this &&
this.schema &&
this.schema.options &&
this.schema.options.clusteredIndex;
if (clusteredIndex != null) {
options = Object.assign({ clusteredIndex: { ...clusteredIndex, unique: true } }, options);
}

try {
await this.db.createCollection(this.$__collection.collectionName, options);
} catch (err) {
Expand Down
31 changes: 31 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5122,6 +5122,37 @@ describe('Model', function() {
assert.ok(collOptions.timeseries);
});

it('createCollection() respects clusteredIndex', async function() {
const version = await start.mongodVersion();
if (version[0] < 6) {
this.skip();
return;
}

const schema = Schema({ name: String, timestamp: Date, metadata: Object }, {
clusteredIndex: {
key: { _id: 1 },
name: 'clustered test'
},
autoCreate: false,
autoIndex: false
});

const Test = db.model('Test', schema, 'Test');
await Test.init();

await Test.collection.drop().catch(() => {});
await Test.createCollection();

const collections = await Test.db.db.listCollections().toArray();
const coll = collections.find(coll => coll.name === 'Test');
assert.ok(coll);
assert.deepEqual(coll.options.clusteredIndex.key, { _id: 1 });
assert.equal(coll.options.clusteredIndex.name, 'clustered test');

await Test.collection.drop().catch(() => {});
});

it('mongodb actually removes expired documents (gh-11229)', async function() {
this.timeout(1000 * 80); // 80 seconds, see later comments on why
const version = await start.mongodVersion();
Expand Down

0 comments on commit dee524b

Please sign in to comment.