3

I am trying to run this simple embedded document using mongoose:

var mongoose = require('mongoose');
var PageSchema = new mongoose.Schema({
    url:String
});
var AlbumSchema = new mongoose.Schema({
    pages:[ PageSchema ]
});

mongoose.model('Album', AlbumSchema);
var Album = mongoose.model('Album');
var album = new Album({pages:[{url:"1"}]});
album.save(function(err, a) {
    console.log(err);
});

After I run this code the second time I get this Error:

{ 
    [MongoError: E11000 duplicate key error index: doalbums.albums.$pages.id_1  dup key: { : null }]
    name: 'MongoError',
    err: 'E11000 duplicate key error index: doalbums.albums.$pages.id_1  dup key: { : null }',
    code: 11000,
    n: 0,
    connectionId: 161,
    ok: 1 
}

What am I doing wrong?

1 Answer 1

9

I am not sure what you are doing wrong here,but what is happening is: An index is created for field 'pages' so it does not allow duplicates.To check this you may give this command in mongo shell doalbums.albums.getIndexes()(I think your DB name is doalbums and collection name is albums ) this will list all indexes in "albums". Then remove index that is not required,using db.albums.dropIndex().This will allow duplication. You can refer http://docs.mongodb.org/manual/administration/indexes/

Sign up to request clarification or add additional context in comments.

2 Comments

There was indeed some leftover index, I dropped the table and it was fixed, Thanks @dany
I'm curius how could a field take indeses by itself? I too got this err and found out that there is an index on one of the fields. But I didnt give any indexes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.