Register unique embedded documents

Asked

Viewed 43 times

1

I want to add a built-in document, I’m using the $push update, in schema I put the attribute as index: { unique : true }, but not respecting the rule this registering item as the same name

Can someone help me?

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

var empresa = new Schema({
    nome : { type : String },
    funcionario : [ funcionario ]
});

1 answer

0

Instead of $push, try using the $addToSet operator (by $push I assumed your field is an array). The single index will cause you to not have two documents with the same value in the field, it will not take care of repeated values within the array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.