1
var UserSchema = new Schema({
    "username": {type: String, unique: true },
    "password": String,

})

so I changed the property name to username, and ever since then things have been going to SHIT.

No matter what I do, I keep getting the error

E11000 duplicate key error index: test.users.$name_1 dup key: { : null }

Even when I try dropping the database, changing the name of the database im connecting to, and remove unique: true from the schema, I STILL GET THIS ERROR.

I literally can't get this error removed no matter what I do, why is it still throwing this error even after I delete every database AND remove the unique property...

Here's the save function -

router.post('/register',  function(req, res){
    var user = new User();

    user.username = req.body.username;

    //hash the password
    bcrypt.genSalt(10, function(err, salt) {
        bcrypt.hash(req.body.password, salt, function(err, hash) {
            user.password = hash;
            user.save(function(err){
                if(err){
                    console.log(err.message);
                    if(err.message === "E11000 duplicate key error index: test.users.$name_1 dup key: { : \"" + user.username + "\" }"){ //this was working fine before i changed user.name to user.username 
                        res.json({
                            error: "name already taken"
                        });
                    }else{
                        res.json({
                            error: "There was an error processing your registration."
                        });
                    }
                    return(err);
                }
                res.json({user: user});
            })
        });
    });


});
joe
  • 1,563
  • 4
  • 16
  • 35

4 Answers4

3

That error appears to be because you already have inserted a document with the name (or username) property as null. Since there's a unique index on username, you can only have one document with a null username.

See this question:

E11000 duplicate key error index in mongodb mongoose

Also, if you remove {unique: true} from the Mongoose schema, that doesn't remove it from the database. You will have to actually drop the index from Mongo as well--the index is still there, your Mongoose Schema will just not ensure that there is an index on startup. Mongoose will run ensureIndex() in MongoDB on startup, to make sure that field is indexed, but removing it from Mongoose does not actually remove it from Mongo, that must be done manually.

Community
  • 1
  • 1
mattschoch
  • 33
  • 3
  • I've dropped the dbs I was using though.. shouldnt that delete the index? – joe Jun 21 '16 at 18:24
  • How do I remove the index? I cleared all the databases I was using and I'm still getting the error – joe Jun 21 '16 at 19:02
  • Still haven't figured this out – joe Jun 21 '16 at 20:23
  • To clarify, you are dropping the database AND removing unique: true from your mongoose schema right? Do you have any other Mongoose schema's that could be interfering (i.e. same collection name)? Because your error references " test.users.$name_1 ", that makes me think the same index keeps being created, on the name field.So, you have an index on the name field, and but you aren't using the name field anymore--you changed it to username--so every username/password you enter has {name: null}, which fails on the unique index. – mattschoch Jun 22 '16 at 18:46
  • 1
    Connect to the Mongo Shell. Your database is called "test" right? And your collection is "users"? If so, then run `use test` and then `db.users.dropIndex({"name": -1})` to make sure you've removed the index. – mattschoch Jun 22 '16 at 18:49
0

Basically this is saying that you have more than 1 document with the Name property with a value of "null". Maybe you still have some docs with that property, or still have a reference to the name property somewhere? This is from the mongo docs

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.

Williz
  • 309
  • 1
  • 9
  • but ive deleted all the dbs that used it, and im also not trying to put in a null value when im saving – joe Jun 21 '16 at 18:51
  • Sounds like the index is still there somehow, Open the mongo shell and you can view all the indexes. If it is there, remove it. https://docs.mongodb.com/manual/tutorial/manage-indexes/ – Williz Jun 21 '16 at 19:13
  • it's not there. i dropped them all. still giving the error. – joe Jun 21 '16 at 19:15
  • are username and password the only 2 in your schema? If not please post the whole schema. Also can you post your save function? – Williz Jun 21 '16 at 19:46
  • I updated it, and yes that's my whole schema for now, im just trying to get that to work before adding new things. – joe Jun 21 '16 at 20:02
  • can you post the results of running this in mongo shell? Of course substitute your collection name – Williz Jun 21 '16 at 20:27
  • db.getCollectionNames().forEach(function(collection) { indexes = db[collection].getIndexes(); print("Indexes for " + collection + ":"); printjson(indexes); }); – Williz Jun 21 '16 at 20:29
0

Never was able to really fix the problem, what I winded up doing was deleting my data folder and recreating it.

joe
  • 1,563
  • 4
  • 16
  • 35
0

You need to remove the index from the mongo database.

At startup mongoose sets collection indices e.g.:

db.collection.createIndex({ fieldA: 1 }, { unique: true, background: true })

To remove the index you have to check your indices:

db.collection.getIndexes()

Use the index name to drop it:

db.collection.dropIndex("index_name")

And make sure you change your mongoose schema to avoid resetting the index.

Albert James Teddy
  • 1,336
  • 1
  • 13
  • 24