this works great if the player already has at least one of each item, but if they for instance don’t have any wood yet, wood doesn’t get added. how would i go about creating any items that don’t already exist.
I’ve looked around and I’m almost entirely certain I’ll need to use aggregation but having only picked up Mongo a couple days ago can’t figure out which parts I need.
You have embedded documents in an array and you want to update the document in that array. If there is a document doesn’t yet exist in that array, you can use $addToSet in your update statement.
The issue i have when using $addToSet is that even if I have the item, if the amount is different it still adds a new entry into the array since the objects are not fully equal.
This question here is almost exactly what i need. the example on the post shows how to add or ignore multiple objects based on keys. and the linked video shows to increment a value or create an entry using a single object’s key. but i have yet to find out how to combine them into one.