In node.js, from one javascript loop, I am trying to insert one json object into one mongodb collection but getting duplicate key error on _id column.
{ MongoError: E11000 duplicate key error collection: app.Tab2017index: id dup key: { : ObjectId('5cbc813227b2ca2864b3c66a') }
Here is my part of my javascript code, which is causing error.
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const url = 'mongodb://localhost:27017';
const dbName = 'app';
var jsonData = {};
MongoClient.connect(url,{useNewUrlParser: true}, function(err, client) {
assert.equal(null, err);
if(err) { return console.dir(err); }
const db = client.db(dbName);
const collection = db.collection('Tab2017')
for (var i = 0; i < 5; i++) {
jsonData["test"] = "line";
console.log('LINE_'+i+'- '+JSON.stringify(jsonData));
collection.insertOne(jsonData, (err, result) => {
if(err) { console.dir(err); }
console.log('mongodb insert done');
})
}
})
Above code is showing error on console,
D:\app\server\routes>node linmon.route-backup3.js
LINE_0- {"test":"line"}
LINE_1- {"test":"line","_id":"5cbc813227b2ca2864b3c66a"}
LINE_2- {"test":"line","_id":"5cbc813227b2ca2864b3c66a"}
LINE_3- {"test":"line","_id":"5cbc813227b2ca2864b3c66a"}
LINE_4- {"test":"line","_id":"5cbc813227b2ca2864b3c66a"}
mongodb insert done
-------------------------------------------
{ MongoError: E11000 duplicate key error collection: app.Tab2017 index: _id_ dup key: { : ObjectId('5cbc813227b2ca2864b3c66a') }
at Function.create (D:\app\server\node_modules\mongodb\node_modules\mongodb-core\lib\error.js:43:12)
at toError (D:\app\server\node_modules\mongodb\lib\utils.js:149:22)
at coll.s.topology.insert (D:\app\server\node_modules\mongodb\lib\operations\collection_ops.js:859:39)
at D:\app\server\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:397:18
at process._tickCallback (internal/process/next_tick.js:61:11)
I am not inserting _id columns value and allowing it to be system generated. Here is the output of getindexes.
> db.Tab2017.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "app.Tab2017"
}
]
>
When i insert same object through mongo shell command line, it works without error. Looks like javascript does not wait for mongodb insert operation to complete.
_id
value ( which is likely assigned in another operation ) does not change in the operation you are actually doing. But in order to correct your code, you actually need to show the offending code which is producing the problem. – Neil LunninsertOne()
actually mutates thejsonData
object to include the_id
value. And of course it stays the same, since every iteration after the first already includes the_id
field and attempts to "insert" it. Basically you should move the declaration ofjsonData = {}
or whatever other valid initialization needs to be done to inside thefor
loop. You should also discoverinsertMany()
, which can take an array of documents. The latter basically means you don't need to "loop" the actual inserts to MongoDB. – Neil Lunn