Assume I have a collection with documents that look like this:
{
"_id": ";lkajsdflhkadfhaewifjasdkfjalksdfjs",
"tree": [
{ "id": "a", "name": "One" },
{ "id": "b", "name": "Two" },
{ "id": "c", "name": "Three" },
{ "id": "d", "name": "Four" }
]
}
Now let's say I want to replace the a, b, and c entries in my tree array with e, f, and c entries. Is it possible to do that replace with an update query? If not, is there a way to select the document such that the tree array only contains the c and d entries (or just the d entry)? I want my document to look like this:
{
"_id": ";lkajsdflhkadfhaewifjasdkfjalksdfjs",
"tree": [
{ "id": "e", "name": "Five" },
{ "id": "f", "name": "Six" },
{ "id": "c", "name": "Three" },
{ "id": "d", "name": "Four" }
]
}
Order of the tree array matters. I'm aware of $splice, but I do not know ahead of time the index of the c entry. Also, the index may vary between documents. Can I do a query inside of $splice that lets me find the index?