lundi 20 avril 2015

Duplicate key error index Mongoose

Im trying to simply create multiple objects with empty arrays into them. Unfortunately there is an error message when i want to create more than one element like that.

Here is my object schema:

var groupSchema = mongoose.Schema({
id: mongoose.Schema.ObjectId,
name: { type: String, required: true },
section: { type: mongoose.Schema.ObjectId, ref:"Section", childPath:"groups" },
users: [ {type : mongoose.Schema.ObjectId, ref : 'User', childPath: "groups"} ],
invitations: [{
    _id:false,
    email: { type: String, required: true },
    isRegistered: { type: Boolean, required: true }
}],

});

Simple create function:

//Create new group
exports.createGroup = function(req, res){
Group.create(req.body, function(err, group){
    if(err){
        console.log(err);
        res.json(err);
        return false;
    }
    res.json({status: true, group: group});
    return true;
});
};

And error message:

{ [MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: skaud_up.groups.$invitations.email_1  dup key: { : null }]
name: 'MongoError',
code: 11000,
err: 'insertDocument :: caused by :: 11000 E11000 duplicate key error index: skaud_up.groups.$invitations.email_1  dup key: { : null }' }

Honestly I don't know why i cant have multiple elements with empty arrays i mnogoDB database.

Can someone explain me what is the cause of issue and what is the proper way of using this sort of objects?

Aucun commentaire:

Enregistrer un commentaire