I’ve been trying to use positional operators to update an object property value to no avail. The code is straightforward and should work based on what I’ve watched video-wise and read documentation-wise. However, I’ve received an error every time without being able to make any progress in solving it.
However, I’ve tried what Pavel suggested before I made this post, and not even that worked. The expected output is actually to increment totalWishes, but I thought there was an issue with using the $inc operator so I tried using $set to no avail.
Thanks for the suggestion, however, I tried a similar method before making this post without any success. Though, I will continue to try today with the array filters you mentioned earlier and see if that is the solution I’ve been looking for.
Tested with no errors! Though, the document itself doesn’t increment whatsoever. I’m quite confused now, I’ve double-checked that the types are correct, and made sure it’s fetching the correct document. Any clue why it isn’t incrementing? If it’s any help, I’m using mongoose ver 6.1.6
Make sure you replace “collection” by the collection name, and that you’re in the right database. It may be useful to create a test collection if you haven’t done it already.
Also, from mongoose docs:
Model.update: Updates one document in the database without returning it.
I do know for 100% certainty that both player and application.user return what’s requested. I think it has to pertain with the mongo call itself. Previously I had to use .findIndex() on the player object and do pity.${typeIndex}.totalWishes and that seemingly worked. But it’s a bit slower than using strictly mongo methods.
After doing a bit of looking, acknowledge false seems to be when the call failed to write to the database.
I took a break for awhile, to take a look at the problem with fresh eyes. Using the code you provided above, provides this error Error: Could not find path "pity.0.type" in schema, do you think it may be smarter to use regular properties or objects instead of an array of objects?
It is not very clear t me from the mongoose docs if the arrayFilters should be in the same document. But now it looks the same than MongoDB docs at least.
The last thing I could think of it is that some of those packages (maybe mongodb native driver) is outdated. You can check with npm outdated.
I don’t think array of objects is a big deal, this is just part of learning how to use it. But you should try to make the Schema as simple as possible, keeping it useful.
should work so if it works from the shell, but not from your code then you’ll know that you need to figure out what’s wrong in your code. But if it gives an error in the shell then you’ll know that something is wrong with the data and you can stop debugging the code and debug data+update syntax on its own.