3

I have User collection:

[
  {
    _id: '5b3935c2d4850aa2d9f0ae25',
    feedBucket: [
      {
        _id: '2a3535c2d4852ba2d9f0ae52',
        createdAt: '2018-06-30 21:52:22.681'
      },
      {
        _id: '9f342c2d4852ba2d9f0ae52',
        createdAt: '2018-06-30 21:52:22.681'
      }
    ]
  },
  {
    _id: '3d2343c2d4850aa2d9f0ae33',
    feedBucket: [
      {
        _id: '2a3535c2d4852ba2d9f0ae52',
        createdAt: '2018-02-30 21:52:22.681'
      },
      {
        _id: '9f342c2d4852ba2d9f0ae52',
        createdAt: '2018-06-30 21:52:22.681'
      }
    ]
  }
]

And I am trying to pull document with id - '9f342c2d4852ba2d9f0ae52' from users feedBucket.

await User.updateMany(
  {},
  { $pull: { feedBucket: { _id: '9f342c2d4852ba2d9f0ae52' } } },
  { multi: true }
)

but it doesn't work, result is { n: 2, nModified: 0, ok: 1 }

2
  • Is this feedBucket._id a string or an ObjectId? If it's an ObjectID, then you'll have to change your modifier. Commented Jul 2, 2018 at 11:56
  • Yes it is an ObjectId, what do you mean 'then you'll have to change your modifier.'? Commented Jul 2, 2018 at 12:01

1 Answer 1

2

If your feedBucket._id is ObjectId then you need to $pull it by converting to mongoose ObjectId

  const user = await User.update(
    { },
    { $pull: { feedBucket: { _id: mongoose.Types.ObjectId('5b07fa836569d356ba5bd2fa') } } },
    { multi: true }
  )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.