0

I have an Array of ints in mongo db. Some of the values come up multiple times like this:

{binder : [4,7,9,7]}

I use pull on the collection like this

{ $pull: { binder: 7} }

It will remove all the 7 and I end up with:

{binder : [4,9]}

However I want to just remove one of the sevens to get something like this:

{binder : [4,7,9]}

How would I go about this. The indices of the numbers are not known and duplicates are not always on last/first spots.

After trying and searching for a long time I found a way with $indexOfArray which is not supported where I need to use it.

2 Answers 2

1

There is currently no way to only remove one item from an array, as said in the documention "The $pull operator removes from an existing array all instances of a value or values that match a specified condition."

You can however use this work around:

Find and unset one item from the array:

> db.arrays.save({ s : [ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 ] })
WriteResult({ "nInserted" : 1 })

> db.arrays.update({ "s" : 5 }, { $unset : { "s.$" : true } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

This will give us the following

> db.arrays.find()
{ "_id" : ObjectId("584c707f1c86f44b7300b223"), "s" : [ 1, 2, 3, 4, null, 5, 6, 7, 8, 9, 10 ] }

Then we can just pull the nulls

> db.arrays.update({ }, { $pull: { "s" : null } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Our 5 that we remove now will be gone:

> db.arrays.find()
{ "_id" : ObjectId("584c707f1c86f44b7300b223"), "s" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] }

It's a bit of a work around but it's pretty safe with multiple atomic operations (as long as you don't use null as a valid value)

Sign up to request clarification or add additional context in comments.

2 Comments

This worked great. Thanks. Just one more question. Is using an array like that just not good? Is there a better way to simply store numbers - or did they just not implement this as function yet because there are workarounds?
It totally depends on your use case, if you had a more complex object within the array you could just append a unique _id so that you could get a handle to the item you wish to remove. Just had a quick search and there is an issue in Jira for this - jira.mongodb.org/browse/SERVER-1014
0

Use $pullAll in newer version of mongo.

https://docs.mongodb.com/manual/reference/operator/update/pullAll/

The top answer is outdated.

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.