[SERVER-825] $pullAll API is not consistent with $pull API Created: 24/Mar/10  Updated: 10/Jun/11  Resolved: 08/Apr/10

Status: Closed
Project: Core Server
Component/s: Usability
Affects Version/s: 1.4.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Michael Sheakoski Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

I'm trying to pull multiple hashes from an array but only know the value of one of the hash keys. I expected $pullAll to work in a similar manner to $pull but got an error message instead.

use temp
db.dropDatabase()

db.bands.save({
name: 'The Beatles',
members: [

{name: 'John', instrument: 'guitar'}

,

{name: 'Paul', instrument: 'bass'}

,

{name: 'George', instrument: 'guitar'}

,

{name: 'Ringo', instrument: 'drums'}

]
})

db.bands.update({}, {$pull: {members:

{instrument: 'drums'}

}})
// Ringo is removed

db.bands.update({}, {$pullAll: {members:

{instrument: ['guitar', 'bass']}

}})
// John, Paul, and George should be removed but get this error instead:
// Modifier $pushAll/pullAll allowed for arrays only

db.bands.update({}, {
$pullAll: {members: [

{name: 'John', instrument: 'guitar'}

,

{name: 'Paul', instrument: 'bass'}

,

{name: 'George', instrument: 'guitar'}

]}
})
// It works if you specify the EXACT hash

db.bands.find()



 Comments   
Comment by Moses Nakamura [ 10/Jun/11 ]

I also ran into this--it is confusing that pullAll has different behavior than pull. There should at least be a note in the API that pullAll only supports exact matches.

Comment by Eliot Horowitz (Inactive) [ 13/May/10 ]

Please see above comment.
I don't think this case makes sense so is resolved as "Works as Designed"
If there is a case where you can't use the example solution, please post the details.

Comment by wangdebing [ 13/May/10 ]

please re-open I also encountered this problem v 1.4.1

Comment by Eliot Horowitz (Inactive) [ 08/Apr/10 ]

see for an example
http://github.com/mongodb/mongo/blob/master/jstests/pullall2.js

please re-open if there is something i missed

Comment by Eliot Horowitz (Inactive) [ 08/Apr/10 ]

on futher though, i'm not sure this makes sense.

$pullAll seems to make more sense to me as exact matches

instead of
db.bands.update({},
{
$pullAll: {members: [

{instrument: 'guitar'}

,

{instrument: 'bass'}

]
}})

you should be able to do
db.bands.update({},
{
$pull: {members: { instrument :

{ $in : [ "guitar" , "bass" ] }

} }
}

Comment by Michael Sheakoski [ 24/Mar/10 ]

After speaking with Michael Dirolf, this is the API he suggested:

db.bands.update({},
{
$pullAll: {members: [

{instrument: 'guitar'}

,

{instrument: 'bass'}

]
}})

This would make it consistent with $pull once it is implemented.

Comment by Michael Dirolf [ 24/Mar/10 ]

oops - closed prematurely. didn't realize the behavior for $pull had changed. still i think the array in the example will need to be moved up one level.

Comment by Michael Dirolf [ 24/Mar/10 ]

$pullAll takes an array, not a document - it pulls all items in the array.

Generated at Thu Feb 08 02:55:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.