MongoDb will pull using positional operator
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 infer a specific value from dates where the ownerId of the parent 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 it will pull any ownerId, which I don't want. Can anyone help me get the query syntax correct?
Thank.
+3
source to share
1 answer
How you want to pull for a specific ownerId , you need to specify the ownerId in the update request part . This is the link 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, the $ operator is not needed here.
var myDate = '2014-10-01';
var ownerId = 123;
db.things.update({
'items' : {
'$elemMatch' : {
ownerId : ownerId,
dates : myDate
}
}
}, {
$pull : {
'items.$.dates' : myDate
}
}, {
multi : true
});
+3
source to share