MongodDB $pull only one element from array

Viewed 3695

I have a document with an array inside, like this:

"userTags" : [
        "foo",
        "foo",
        "foo",
        "foo",
        "moo",
        "bar"
    ]

If I perform db.products.update({criteriaToGetDocument}, {$push: {userTags: "foo"}}} I can correctly insert another instance of foo into the array.

However, if I do db.products.update({criteriaToGetDocument}, {$pull: {userTags: "foo"}}} then it removes all instances of foo from the array, leaving me with:

"userTags" : [
        "moo",
        "bar"
    ]

This won't do at all, I only want to pull one item from the array rather than all of them. How can I alter the command so that only one foo is removed? Is there some sort of $pullOnce method that can work here?

3 Answers
Related