Register unique embedded documents

1

I want to add an embedded document, I am using $push of update, in schema I put the attribute as index: { unique : true } , but not respecting the rule this is registering item as the same name

Can anyone help me?

var funcionario = new Schema({
    nome : { type : String, index : { unique : true } }
});

var empresa = new Schema({
    nome : { type : String },
    funcionario : [ funcionario ]
});
    
asked by anonymous 25.07.2017 / 21:46

1 answer

0

Instead of $ push, try to use the $ addToSet operator (for $ push I assumed that your field is an array). The unique index will make you do not have two documents with the same value in the field, will not care for repeated values within the array.

    
28.07.2017 / 13:36