-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 3.0.6
-
Component/s: js-bson
-
Empty show more show less
Description
In https://docs.mongodb.com/manual/core/document/ I saw that "The field names cannot contain the dot (.) character." but I have managed to insert broken documents using the default code options of the nodejs 3.0.6 driver.
Steps to reproduce
- Install the environment :
1. mongodb 3.2.18, node v8.11.1, npm 5.6.0, ubuntu 12.04
2. package.json"dependencies": { "mongodb": "^3.0.6", }
- Run the following index.js file:
const {MongoClient} = require('mongodb'); const uri = 'mongodb://localhost:27017/admin'; (async () => { const client = await MongoClient.connect(uri); const db = client.db('test'); let records = [{ "a.b.c": 1 }, { "d.e": 1 } ]; await db.collection("sample").insertMany(records); let dbRecords = await db.collection("sample").find({}).toArray(); console.log(dbRecords); })();
Found results
[ { _id: 5ad05493f5f7cfd420a2ac44, 'a.b.c': 1 }, { _id: 5ad05493f5f7cfd420a2ac45, 'd.e': 1 } ]
Expected Results
An error should be thrown by the driver like in version 2.2.X
Workaround
From what I see, the users must explicitly pass the :
{
checkKeys:true
}
to the options parameter of the insertMany function.
Notes
- From my research, the source of the bug is here : https://github.com/mongodb-js/mongodb-core/blob/3.0.0/lib/wireprotocol/3_2_support.js#L68
- It seems that checkKeys defaults to false
- If this is intended, please update the documentation ( https://github.com/mongodb/node-mongodb-native/blob/3.0.0/CHANGES_3.0.0.md )