These are chat archives for Automattic/mongoose

15th
Feb 2017
Ben Fletcher
@benfletcher
Feb 15 2017 15:39
Is there an atomic method of removing a sub-document (single element of sub-array) where the parent ID and sub ID are both known? Sorry if this should be obvious from the docs.
Ben Fletcher
@benfletcher
Feb 15 2017 15:49
(and side question are the list of atomic updates for use in update() listed anywhere in the docs? Is this list from mongodb fully supported in mongoose? https://docs.mongodb.com/manual/reference/operator/update/ )
Ben Fletcher
@benfletcher
Feb 15 2017 16:17
I've tried:
    ParentModel.update(
      { _id: parentId },
      { $pull: { 'children._id': childId } }
    )
And I get the error:
MongoError: cannot use the part (children of children._id) to traverse the element
Ben Fletcher
@benfletcher
Feb 15 2017 16:24

Also tried:

    ParentModel.update(
      { _id: parentId },
      { children: { $pull: { _id: childId } } }
    )

Doesn't throw an error and returns a nModified: 1, but the change isn't saved regardless of using exec() or then(). Haven't tried using a callback.

But the _id of the item changes... strangely. Thanks for any help on this and your patience!
Ben Fletcher
@benfletcher
Feb 15 2017 18:53
A colleague helped me see the error. Had $pull and children reversed.