I have a structure like the following and am trying to remove one item from the array. I have not been able to figure this out so far. Any suggestions?
{
"_id" : "4fd4466c03644ec6ec6d5fe1",
"fruit" : [
{ "id" : "4fd4466c03644ec6ec6d5fdf", "name" : "avocado", "quantity" : "2" },
{ "id" : "4fd4466c03644ec6ec6d5fe0", "name" : "apple", "quantity" : "34" },
]
}
I have a model of "cart" that has an embedded document called "fruit". Yes they are bogus examples, but it's simpler to explain a cart of fruit than what I am working on. The furthest I got with the calls.
MorphiaQuery q = Cart.q().filter("_id",cartid);
Cart.o().removeAll("fruit", ?????).update(q);
Any guidance would be awesome here.
First find out the Cart from which you want to remove one fruit:
Cart cart = Cart.findById(cartid);
Second locate the fruit and remove it:
for (Fruit f: cart.fruit) {
if (f.name.equals("apple")) {
cart.fruit.remove(f);
cart.save();
break;
}
}