-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document.array.pull() does not seem to work for populated arrays when pulling an _id string #1635
Comments
You are correct, you probably don't want to set the entire array and overwrite the entire thing. Currently, re-populating an array modified under these circumstances is not working. Work arounds
|
This is still an issue in 3.8.13... It would be great if there was an |
+1 for this issue. Have to re-query for new doc instance every time. |
+1 for this, looking it up again is inelegant. |
+1 |
3 years passed )) |
point 1 above ( the removal of _ids ) has already been addressed and works as expected here. side note, the request above for doc.unpopulate() has been fulfilled as well, it is doc.depopulate() |
Atomic push and pull isn't possible with MongoDB server in general, consider the following example run with mongo shell. $pull and $push on same array leads to a write conflict error.
However, pushing a non-populated element does seem to still cause the half-populated document issue:
We will likely just depopulate the whole array if pushing a plain ObjectId onto a populated array. |
fix(document): make sure depopulate does not convert hydrated arrays to vanilla arrays
I have the following function designed to update the ObjectIds stored in a given document
doc
, atpath
, to match those provided in a form submissiondata
by using atomicpush
andpull
functionality.(ObjectIds are stored in an array like this:
schema.add({ myPath: [{type: ObjectId, ref: 'OtherSchema'}] });
If the
doc
has already hadpath
populated, two unexpected behaviours occur:Console.log
output demonstrating both in the above function:re: 1, this seems like a bug. The documentation here implies that you should be able to remove ids from an array, although it doesn't mention what happens if the array has been populated.
re: 2, as you can see the newly added object looks populated (is an object) but hasn't been loaded from the database yet (so has no name property). What's the best way to handle this? Repopulate the path after saving the document?
It might be best in my particular case to specifically detect that the path being updated has been populated, unpopulate it, make the atomic changes to the array, then repopulate the path after the document has been saved. There doesn't seem to be any api to unpopulate a path, and I don't want to blindly do this:
... because I think that would cause the document to think that the entire value of
path
has been modified.Any hints on how to best make all this work would be great, Cheers.
The text was updated successfully, but these errors were encountered: