From 5ce9b25c965e2be633bf4095647e73a152d9914c Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 2 Feb 2021 18:23:48 -0500 Subject: [PATCH] fix(operations): avoid hardcoding `checkKeys` for insert operations (#2726) --- src/operations/insert.ts | 2 +- test/functional/collection.test.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/operations/insert.ts b/src/operations/insert.ts index 43418ab434..17a9ee1b5b 100644 --- a/src/operations/insert.ts +++ b/src/operations/insert.ts @@ -18,7 +18,7 @@ export class InsertOperation extends CommandOperation { constructor(ns: MongoDBNamespace, documents: Document[], options: BulkWriteOptions) { super(undefined, options); - this.options = { ...options, checkKeys: true }; + this.options = { ...options, checkKeys: options.checkKeys ?? true }; this.ns = ns; this.documents = documents; } diff --git a/test/functional/collection.test.js b/test/functional/collection.test.js index 3f2fc4548e..42b96b575b 100644 --- a/test/functional/collection.test.js +++ b/test/functional/collection.test.js @@ -269,6 +269,16 @@ describe('Collection', function () { }); }); + it('should permit insert of dot and dollar keys if requested', function () { + const collection = db.collection('test_invalid_key_names'); + return Promise.all([ + collection.insertOne({ hel$lo: 0 }, { checkKeys: false }), + collection.insertOne({ hello: { $hello: 0 } }, { checkKeys: false }), // embedded document can have a leading dollar + collection.insertOne({ 'hel.lo': 0 }, { checkKeys: false }), + collection.drop() + ]); + }); + it('should fail due to illegal listCollections', function (done) { db.collection(5, err => { expect(err.message).to.equal('collection name must be a String');