I am very new to mongodb, I was wondering when adding an embedded document, if there is a way I could only check for person_Id and not the active field. I only care that person_id’s are not duplicates
collection.update({'_id':new BSON.ObjectID(business_id)}, {$addToSet: {members : {person_Id : person_id, active : true }}}, {safe:true}, function(err, result) {
if (err) {
console.log('Error updating person: ' + err);
} else {
console.log('' + result + ' document(s) updated');
callback(result);
}
});
If I understood your question correctly then the answer is yes. You can put the criteria that your members should not contain a member with that specific person_id in your query :
This would push a new member record into your members array if, and only if, the business_id matches and the members array does not contain a member with person_id. Note that this approach is mutually exclusive with upserts.