Mongo duplicate key error on upsert or save - node.js

I'm running into a problem inserting data into mongo via nodejs. I'm loading json objects into documents through either upsert:true, or .save() called on a returned mongoose document.
EDIT: I forgot to point out one important point, that this does work. I update 30-40,000 documents correctly. It will run for a while, then eventually throw this error. The "unique key (xId) is a different string each time, so I don't think it's caused by the data actually being loaded...
Here's the schema:
var rosterSchema = new Schema({
name : String,
xId : {type:String, unique: true},
event : {type:ObjectId, ref:'Event'},
team : {type:ObjectId, ref:'Team'},
division : {type:ObjectId, ref:'Division'},
place : String,
players : [{type:ObjectId,ref:"Player"}],
staff : [{type:ObjectId,ref:"Player"}],
matches : [{type:ObjectId,ref:"Match"}],
});
Error:
MongoError: E11000 duplicate key error collection: r_fix.rosters index: xId_1 dup key: { : "6RNoYBSsCAJRsjxs" }
at Function.MongoError.create
Each run of the parse/load function targets a single roster page, which references other rosters in their matches.
Most of the rosters already exist from loading other data.
I can't guarantee the order that the rosters will be parsed, so I may need to create a 'match' against a roster that doesn't exist yet, which requires the new roster to be created, hence why I use findOneAndUpdate as opposed to find
Any idea what might be causing this? I'm trying to avoid pasting the whole source so these are each of the individual calls, with what I believe to be relevant info:
var rosterObj = {
xId : id,
name : rosterJson.team_name,
};
Roster.findOneAndUpdate({xId:rosterObj.xId},{$set:rosterObj},{new: true, upsert: true, setDefaultsOnInsert: true})
.exec((err,roster)=>{
if(err)throw(err);
}).then((roster)=>{
...
The above roster returns the document used in all subsequent save()'s
roster.event = event._id;
roster.save((err)=>{if(err)throw(err)})
...
roster.team = team._id;
roster.save((err)=>{if(err)throw(err)})
...
if(pObj.staff == "No")
roster.players.addToSet(player._id);
else
roster.staff.addToSet(player._id);
roster.save((err)=>{if(err)throw(err)});
...
if(!roster.event)
if(oppRoster.event){
roster.event = oppRoster.event;
roster.save((err)=>{if(err)throw(err)});
}
...
var rosterObj = {
xId:mObj.vs.roster_id,
event:roster.event,
}
Roster.findOneAndUpdate({xId:rosterObj.xId},{$set:rosterObj},{new: true, upsert: true, setDefaultsOnInsert: true}).exec((err,oppRoster)=>{
if(err)throw(err);
return oppRoster;
})
As far as I understand it, when I use a single key for the find, and it's the only unique:true value in the document, then doc.save() and doc.findOneAndUpdate({ ... , {upsert :true...}) should never return a duplicate key error.
My catch() at the end of the promise chain doesn't catch these thrown errors either, but that is an entirely different problem.
But I don't know anything, so that's why I'm here!
EDIT: I should point out that I'm doing this over a large number of documents, but they're all promise-chained, so only one 'roster' should be getting updated at one time.

The unique index constraint does not itself protect you from duplicate key errors, only from duplicate records. You need to catch the exception and retry. The duplicate key error should not reoccur as the race condition danger has passed at that point. See: https://docs.mongodb.org/manual/reference/method/db.collection.findAndModify/#behavior

Related

node.js to check out duplication value in mongoose

Now I'd like to save my json data into mongoose but the duplicate value had to be filtered.
my_json = [
{"name":"michael","age":21,"sports":"basketball"},
{"name":"nick","age":31,"sports":"golf"},
{"name":"joan","age":41,"sports":"soccer"},
{"name":"henry","age":51,"sports":"baseball"},
{"name":"joe","age":61,"sports":"dance"},
];
Database data is :
{
"name":"joan","age":41,"sports":"soccer"
}
Is there some specific method to avoid duplicate data insert to mongoose directly? It might be saved 4 of values except "joan" value.
Once I suppose to try to use "for statement", it was fine.
However I just want to make a simple code for that what could happen in a variety possible code.
for(var i = 0; i < my_json.length; i++){
// to check out duplicate value
db.json_model.count({"name":my_json[i].name}, function(err, cat){
if(cat.length == 0){
my_json_vo.savePost(function(err) {
});
}
})
};
As you see I need to use count method whether the value is duplicated or not. I don't want to use count method but make it more simple..
Could you give me an advice for that?
You can mark field as unique in mongoose schema:
var schema = new Schema({
name: {type: String, required: true, unique: true}
//...
});
Also, you can add unique index for name field into your database:
db.js_model.createIndex( {"name": 1}, { unique: true, background: true } );
then, if new entity with the same name will be asked to save - mongo won't save it, and respond an error.
In Addition to #Alex answer about adding unique key on the name field.
You can use insertMany() method with ordered parameter set to
false. Like this...
let my_json = [
{"name":"michael","age":21,"sports":"basketball"},
{"name":"nick","age":31,"sports":"golf"},
{"name":"joan","age":41,"sports":"soccer"},
{"name":"henry","age":51,"sports":"baseball"},
{"name":"joe","age":61,"sports":"dance"},
];
User.insertMany(my_json ,{ordered :false});
This query will successfully run and insert unique documents, And also
produces error later after successful insertion. So You will come to
know that there were duplicate records But now in the database, all
records are unique.
Reference InsertMany with ordered parameter

Unable to create documents with the same object ID in different arrays due to "duplicate key error"

I am trying to create a new Group with an array of user ID's but getting the error E11000 duplicate key error index: bullet-time.groups.$users_1 dup key
I can create the first group fine and get the following object returned:
{
"_id": "5acddf7a599e250f79cebc21",
"users": [
ObjectId("5ac4ab6e951bb2153ca2749e")
]
}
But upon trying to create a new group with the same user ObjectId is when I get the aforementioned error.
My group model is simple:
const GroupSchema = new Schema({
users: [{ type : mongoose.Schema.Types.ObjectId, ref: 'User' }],
});
And upon creating, I simply create a new group, push the user ObjectId into the users array and call save like so:
async create(user_id){
try {
const newGroup = new GroupModel()
newGroup.users.push(user_id)
return await newGroup.save()
}catch (err) {
return err
}
}
I can't see any reason why 1 user can't be in multiple groups.
As far as I can see, there are no unique or dedup flags in my models.
EDIT:
If I change my model to accept users as an array of strings as opposed to ObjectId's like so users: [{ type : String, ref: 'User' }], then I don't get the duplication error
I dropped my Groups collections and it has seemed to resolve the issue.
I had played around with making the users array unique when attempting to make sure there were no duplicate ID's in the array. This was obviously the wrong way to do it and when it created the Groups collection it had obviously saved that unique attribute.
This is news to me so hopefully this helps anybody else who has a similar issue.

MongoError: E11000 duplicate key error collection cms_demo1.posts index: username_1 dup key: { : null } [duplicate]

Following is my user schema in user.js model -
var userSchema = new mongoose.Schema({
local: {
name: { type: String },
email : { type: String, require: true, unique: true },
password: { type: String, require:true },
},
facebook: {
id : { type: String },
token : { type: String },
email : { type: String },
name : { type: String }
}
});
var User = mongoose.model('User',userSchema);
module.exports = User;
This is how I am using it in my controller -
var user = require('./../models/user.js');
This is how I am saving it in the db -
user({'local.email' : req.body.email, 'local.password' : req.body.password}).save(function(err, result){
if(err)
res.send(err);
else {
console.log(result);
req.session.user = result;
res.send({"code":200,"message":"Record inserted successfully"});
}
});
Error -
{"name":"MongoError","code":11000,"err":"insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.users.$email_1 dup key: { : null }"}
I checked the db collection and no such duplicate entry exists, let me know what I am doing wrong ?
FYI - req.body.email and req.body.password are fetching values.
I also checked this post but no help STACK LINK
If I removed completely then it inserts the document, otherwise it throws error "Duplicate" error even I have an entry in the local.email
The error message is saying that there's already a record with null as the email. In other words, you already have a user without an email address.
The relevant documentation for this:
If a document does not have a value for the indexed field in a unique index, the index will store a null value for this document. Because of the unique constraint, MongoDB will only permit one document that lacks the indexed field. If there is more than one document without a value for the indexed field or is missing the indexed field, the index build will fail with a duplicate key error.
You can combine the unique constraint with the sparse index to filter these null values from the unique index and avoid the error.
unique indexes
Sparse indexes only contain entries for documents that have the indexed field, even if the index field contains a null value.
In other words, a sparse index is ok with multiple documents all having null values.
sparse indexes
From comments:
Your error says that the key is named mydb.users.$email_1 which makes me suspect that you have an index on both users.email and users.local.email (The former being old and unused at the moment). Removing a field from a Mongoose model doesn't affect the database. Check with mydb.users.getIndexes() if this is the case and manually remove the unwanted index with mydb.users.dropIndex(<name>).
If you are still in your development environment, I would drop the entire db and start over with your new schema.
From the command line
➜ mongo
use dbName;
db.dropDatabase();
exit
I want to explain the answer/solution to this like I am explaining to a 5-year-old , so everyone can understand .
I have an app.I want people to register with their email,password and phone number .
In my MongoDB database , I want to identify people uniquely based on both their phone numbers and email - so this means that both the phone number and the email must be unique for every person.
However , there is a problem : I have realized that everyone has a phonenumber but not everyone has an email address .
Those that don`t have an email address have promised me that they will have an email address by next week. But I want them registered anyway - so I tell them to proceed registering their phonenumbers as they leave the email-input-field empty .
They do so .
My database NEEDS an unique email address field - but I have a lot of people with 'null' as their email address . So I go to my code and tell my database schema to allow empty/null email address fields which I will later fill in with email unique addresses when the people who promised to add their emails to their profiles next week .
So its now a win-win for everyone (but you ;-] ): the people register, I am happy to have their data ...and my database is happy because it is being used nicely ...but what about you ? I am yet to give you the code that made the schema .
Here is the code :
NOTE : The sparse property in email , is what tells my database to allow null values which will later be filled with unique values .
var userSchema = new mongoose.Schema({
local: {
name: { type: String },
email : { type: String, require: true, index:true, unique:true,sparse:true},
password: { type: String, require:true },
},
facebook: {
id : { type: String },
token : { type: String },
email : { type: String },
name : { type: String }
}
});
var User = mongoose.model('User',userSchema);
module.exports = User;
I hope I have explained it nicely .
Happy NodeJS coding / hacking!
In this situation, log in to Mongo find the index that you are not using anymore (in OP's case 'email'). Then select Drop Index
Check collection indexes.
I had that issue due to outdated indexes in collection for fields, which should be stored by different new path.
Mongoose adds index, when you specify field as unique.
Well basically this error is saying, that you had a unique index on a particular field for example: "email_address", so mongodb expects unique email address value for each document in the collection.
So let's say, earlier in your schema the unique index was not defined, and then you signed up 2 users with the same email address or with no email address (null value).
Later, you saw that there was a mistake. so you try to correct it by adding a unique index to the schema. But your collection already has duplicates, so the error message says that you can't insert a duplicate value again.
You essentially have three options:
Drop the collection
db.users.drop();
Find the document which has that value and delete it. Let's say the value was null, you can delete it using:
db.users.remove({ email_address: null });
Drop the Unique index:
db.users.dropIndex(indexName)
I Hope this helped :)
Edit: This solution still works in 2023 and you don't need to drop your collection or lose any data.
Here's how I solved same issue in September 2020. There is a super-fast and easy way from the mongodb atlas (cloud and desktop). Probably it was not that easy before? That is why I feel like I should write this answer in 2020.
First of all, I read above some suggestions of changing the field "unique" on the mongoose schema. If you came up with this error I assume you already changed your schema, but despite of that you got a 500 as your response, and notice this: specifying duplicated KEY!. If the problem was caused by schema configuration and assuming you have configurated a decent middleware to log mongo errors the response would be a 400.
Why this happens (at least the main reason)
Why is that? In my case was simple, that field on the schema it used to accept only unique values but I just changed it to accept repeated values. Mongodb creates indexes for fields with unique values in order to retrieve the data faster, so on the past mongo created that index for that field, and so even after setting "unique" property as "false" on schema, mongodb was still using that index, and treating it as it had to be unique.
How to solve it
Dropping that index. You can do it in 2 seconds from Mongo Atlas or executing it as a command on mongo shell. For the sack of simplicity I will show the first one for users that are not using mongo shell.
Go to your collection. By default you are on "Find" tab. Just select the next one on the right: "Indexes". You will see how there is still an index given to the same field is causing you trouble. Just click the button "Drop Index". Done.
So don't drop your database everytime this happens
I believe this is a better option than just dropping your entire database or even collection. Basically because this is why it works after dropping the entire collection. Because mongo is not going to set an index for that field if your first entry is using your new schema with "unique: false".
I faced similar issues ,
I Just clear the Indexes of particular fields then its works for me .
https://docs.mongodb.com/v3.2/reference/method/db.collection.dropIndexes/
This is my relavant experience:
In 'User' schema, I set 'name' as unique key and then ran some execution, which I think had set up the database structure.
Then I changed the unique key as 'username', and no longer passed 'name' value when I saved data to database. So the mongodb may automatically set the 'name' value of new record as null which is duplicate key. I tried the set 'name' key as not unique key {name: {unique: false, type: String}} in 'User' schema in order to override original setting. However, it did not work.
At last, I made my own solution:
Just set a random key value that will not likely be duplicate to 'name' key when you save your data record. Simply Math method '' + Math.random() + Math.random() makes a random string.
I had the same issue. Tried debugging different ways couldn't figure out. I tried dropping the collection and it worked fine after that. Although this is not a good solution if your collection has many documents. But if you are in the early state of development try dropping the collection.
db.users.drop();
I have solved my problem by this way.
Just go in your mongoDB account -> Atlast collection then drop your database column. Or go mongoDB compass then drop your database,
It happed sometimes when you have save something null inside database.
This is because there is already a collection with the same name with configuration..Just remove the collection from your mongodb through mongo shell and try again.
db.collectionName.remove()
now run your application it should work
I had a similar problem and I realized that by default mongo only supports one schema per collection. Either store your new schema in a different collection or delete the existing documents with the incompatible schema within the your current collection. Or find a way to have more than one schema per collection.
I got this same issue when I had the following configuration in my config/models.js
module.exports.models = {
connection: 'mongodb',
migrate: 'alter'
}
Changing migrate from 'alter' to 'safe' fixed it for me.
module.exports.models = {
connection: 'mongodb',
migrate: 'safe'
}
same issue after removing properties from a schema after first building some indexes on saving. removing property from schema leads to an null value for a non existing property, that still had an index. dropping index or starting with a new collection from scratch helps here.
note: the error message will lead you in that case. it has a path, that does not exist anymore. im my case the old path was ...$uuid_1 (this is an index!), but the new one is ....*priv.uuid_1
I have also faced this issue and I solved it.
This error shows that email is already present here. So you just need to remove this line from your Model for email attribute.
unique: true
This might be possible that even if it won't work. So just need to delete the collection from your MongoDB and restart your server.
It's not a big issue but beginner level developers as like me, we things what kind of error is this and finally we weast huge time for solve it.
Actually if you delete the db and create the db once again and after try to create the collection then it's will be work properly.
➜ mongo
use dbName;
db.dropDatabase();
exit
Drop you database, then it will work.
You can perform the following steps to drop your database
step 1 : Go to mongodb installation directory, default dir is "C:\Program Files\MongoDB\Server\4.2\bin"
step 2 : Start mongod.exe directly or using command prompt and minimize it.
step 3 : Start mongo.exe directly or using command prompt and run the following command
i) use yourDatabaseName (use show databases if you don't remember database name)
ii) db.dropDatabase()
This will remove your database.
Now you can insert your data, it won't show error, it will automatically add database and collection.
I had the same issue when i tried to modify the schema defined using mangoose. I think the issue is due to the reason that there are some underlying process done when creating a collection like describing the indices which are hidden from the user(at least in my case).So the best solution i found was to drop the entire collection and start again.
If you are in the early stages of development: Eliminate the collection. Otherwise: add this to each attribute that gives you error (Note: my English is not good, but I try to explain it)
index:true,
unique:true,
sparse:true
in my case, i just forgot to return res.status(400) after finding that user with req.email already exists
Go to your database and click on that particular collection and delete all the indexes except id.

Duplicate key error when creating record

This error is really confusing. I can create record once but second time no matter what, error would appear:
E11000 duplicate key error index: test_db.students.$code_1 dup key: { : null }
This is my schema:
var Schema = {
"stuCode" : String,
"firstName" : Boolean,
"lastName" : Boolean,
"email" : String
};
Before I had stuCode as code and _codeand it was required it's unique. That could maybe cause a problem.
I've tried to delete all the data from that collection, remove collection in mongoose.connection.on('open', function(){ }); but the problem is still there. I can only save first record and second one is impossible to save.
What would be the solution for this problem?
Before I had stuCode as code and _codeand it was required it's unique.
That could maybe cause a problem.
Yes that old index on code is probably still there, can't you drop it ?

Inserting Multiple Documents into MongoDB while Requiring Unique Index

My collection P has a unique index on the phone field:
db.P.ensureIndex( { phone: 1 }, { unique: true, dropDups: true } )
db.P.insert( {phone:"555-1234"} )
If I insert an array of documents, where even one document has a duplicate key:
db.P.insert( [{phone:"911"},{phone:"555-1234"}] )
> E11000 duplicate key error index: test.P.$phone_1 dup key: { : "555-1234" }
The entire insert fails, and the valid number is not inserted.
Question: How can I do bulk inserts, make sure the valid documents are inserted, and get information on which inserts failed? Bonus points for showing code with the nodejs api.
MongoDB drivers have a "ContinueOnError" option that would cause mongo to skip records with duplicate unique keys. As far as identifying what was skipped, per documentation: "If multiple errors occur during a bulk insert, clients only receive the last error generated. (http://docs.mongodb.org/manual/core/bulk-inserts/).
For Node.js, it would be something like
db.P.insert( [{phone:"911"},{phone:"555-1234"}], {continueOnError:true} )
http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#insert

Resources