Removing the array element in mongoDB based on the position of element

From documentation:

{ $pull : { field : {$gt: 3} } } removes array elements greater than 3

So i suppose that you can do somethig like this for now:

{ $pull : { field : {$gt: 3, $lt: 5} } } // shoud remove elemet in 4 position 

Or try update using position operator, i suppose shoud be something like this:

  { $pull : "field.4" } 

  { $pull : {"field.$": 4}}

It is only a suggestion, because i can’t test it right now.

Update:

Seems you cant do it right know in one step(there is such bug in jira)

But you can remove using unset element in position and that pull elemets with null value:

{$unset : {"array.4" : 1 }}
{$pull : {"array" : null}}

Leave a Comment