6

Actually I need to remove an element from the array based on its position. Using $pop we can remove element from top or bottom (considering it as a stack. 0th element at top) as explained here.

We can also remove element from array based on the value of the elements in the array using $pull as explained here.

But I need to remove element from the array based on position. So is there any way I can do this.

Community
  • 1
  • 1
aditya_gaur
  • 3,209
  • 6
  • 32
  • 43
  • 1
    That's not a particularly safe operation to do in a multitasking environment. What if two threads try to remove element 4 at almost the same time? Idempotent or atomic actions are generally what you want for all database operations. – Ian Mercer Feb 11 '11 at 16:33

3 Answers3

11

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}}
Andrew Orsich
  • 52,935
  • 16
  • 139
  • 134
1

That is how possible to do that with latest mongodb v4.2, ugly but working

     _id: ObjectId("5e4539cb6aebbeaa0a6b8fe7") 
   }, [
        { $set: 
          { notes: 
            { 
              $concatArrays: [
                { $slice: ['$notes', 4] }, 
                { $slice: ['$notes', { $add: [1, 4] }, { $size: '$notes' }]}
              ] 
            } 
          } 
        }
      ])```
Ivan Cherviakov
  • 528
  • 2
  • 12
0

Here's your answer: MongoDB pull array element from a collection

To remove specific element from an array of some document first you need to identify this element. For instance I have a document with array and every element of this array is an object:

{
    "_id" : ObjectId("5140f34888dd50971900002d"),
    "_permissions" : {
        "edit" : [
            {
                "profile_id" : NumberLong(123),
                "comment" : "app/project owner"
            },
            {
                "profile_id" : NumberLong("153579099841888257"),
                "comment" : "project admin"
            },
            {
                "profile_id" : NumberLong("153579095869882369"),
                "comment" : "project admin"
            }
        ],
        "view" : [
            {
                "profile_id" : NumberLong(123),
                "comment" : "app/project owner"
            },
            {
                "profile_id" : NumberLong("153579099841888257"),
                "comment" : "project admin"
            },
            {
                "profile_id" : NumberLong("153579095869882369"),
                "comment" : "project admin"
            }
        ]
    }
}

So let's remove profile_id with "153579099841888257" value from _permissions.view array. Here we go

db.collection.update({_id: ObjectId("5140f34888dd50971900002d")}, {$pull:{"_permissions.view": {"profile_id": NumberLong("153579099841888257")}}});
  1. I define scope of the object (to make sure id doesn't affect any other first found document)
  2. Identify needed element to pull out: profile_id with "153579099841888257" value in the _permissions.view array
Jim Buck
  • 2,383
  • 23
  • 42
Dmitry
  • 1,484
  • 2
  • 15
  • 23