My question is how can I update this document to have equipment.hand moved to first available index of inventory (equal to null), and equipment.hand set to null afterwards?
I tried the following to update index 1, but it sets ALL inventory slots to equipment.hand item.
(I also need it to find the first available inventory slot automatically)
// unequip item
db.characters.updateOne(
// an item is equipped in the hand slot, and there is at least one inventory space
{ name: "foo", "equipment.hand": { $ne: null }, inventory: { $all: [null] } },
[
{ $set:
{
"inventory.1": "$equipment.hand",
"equipment.hand": null
}
}
]
)
It is a hard requirement that items in the inventory can be moved to any position, not just one after another.
So the inventory might look like this:
[ null, null, { name: “item 1” }, null, { name: “item 2” } ]
If you limit your data storage model to what you want at the presentation layer you deprive your self of having the most efficient storage and processing.