Reputation: 36573
I have the following structure for a document
{
_id: 12342342
items: [
{
ownerId: 123,
dates: ['2014-10-01', '2014-10-02']
},
{
ownerId: 234,
dates: ['2014-10-01', '2014-10-02']
},
]
}
I want to pull a specific value from dates where the ownerId of the parent object is a specific value.
I have
var myDate = '2014-10-01'
db.collection('things').update({'items.dates': {$in: [myDate]}},
{$pull: { 'items.$.dates': myDate } }, {multi: true});
But this will pull for any ownerId, which is not what I want. Can someone help me get the right query syntax?
Thank you.
Upvotes: 3
Views: 1237
Reputation: 4421
As you want to pull on specific ownerId, you need to specify that ownerId in the query part of update method. This is a reference for you:
var myDate = '2014-10-01';
var ownerId = 123;
db.things.update({
'items' : {
'$elemMatch' : {
ownerId : ownerId,
dates : {
$in : [ myDate ]
}
}
}
}, {
$pull : {
'items.$.dates' : myDate
}
}, {
multi : true
});
Actually, operator $in is unnecessary here.
var myDate = '2014-10-01';
var ownerId = 123;
db.things.update({
'items' : {
'$elemMatch' : {
ownerId : ownerId,
dates : myDate
}
}
}, {
$pull : {
'items.$.dates' : myDate
}
}, {
multi : true
});
Upvotes: 4