Consider the following object:
var child = {
title: "Shoes",
active: true,
sort: 1
}
And the following update:
db.Category.update(
{ _id: this._id },
{ $addToSet: { children: child }}, function(error) {
if (error) {
console.log(error.reason);
} else {
console.log("success");
}
}
);
Is there a way to add a more specific condition on this? Basically, I only want an item added to the array if it doesn't already exist, but Mongo determines this by all the properties of the child.
I basically want the update to fail if children.title exists, in uppercase, lowercase, etc.
So if I have a document such as:
{
title: "Clothing",
active: true,
sortorder: 1,
children: [
{
title: "Shoes",
active: true,
sortorder: 1
}
]
}
I want any kind of addition to this array to fail if the title: "Shoes" exists... shoes, ShoEs, SHOES, should all fail. Or, regardless if the other properties of this child are different such as active, or sort order, I want it to fail based on just the title.
Is there a conditional method I'm missing?