I am trying to remove an entry in an array that is a sub property of a document field. The data for a document looks like this:
{
_id: 'user1',
feature: {
enabled: true,
history: [
{
_id: 'abc123'
...
}
]
},
...
}
For some reason I have not been able to remove the element using $pull and I'm not sure what is wrong.
I've looked at the official docs for $pull, this well-known answer, as well this one and another.
I have tried the following query
db.getCollection('userData').update({ _id:'user1' }, {
$pull: {
'feature.history': { _id: 'abc123' }
}
})
and it has no effect. I've double-checked _id and it is a proper match. I've also tried filtering based on the same entry, thinking I need to target the data I'm trying to remove:
db.getCollection('userData')
.update({ _id: 'user1', 'feature.history': { _id: 'abc123' }, { ... })
So far no luck
_id? because if it is objectId then you need to cast your idObjectId. How should it be cast?$pull: { 'feature.history': { _id: mongoose.Types.ObjectId('your_id_field') } }Try this_idfields when querying, but usingObjectId()in my original query modified the record successfully. Thanks! Can you add this as an answer and I will accept it