I am wondering how I can delete certain elements from an array in mongoDB.
I have the following json saved in the collection geojsons:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"ID": "1753242",
"TYPE": "8003"
}
},
{
"type": "Feature",
"properties": {
"ID": "4823034",
"TYPE": "7005"
}
},
{
"type": "Feature",
"properties": {
"ID": "4823034",
"TYPE": "8003"
}
}
]
}
And I want to delete every element in the array features, where properties.TYPE equals 8003.
I tried it with the following statement, but it does not delete anything.
db.geojsons.aggregate([
{$match: {'features': {$elemMatch : {"properties.TYPE": '8003' }}}},
{$project: {
features: {$filter: {
input: '$features',
as: 'feature',
cond: {$eq: ['$$feature.properties.TYPE', '8003']}
}}
}}
])
.forEach(function(doc) {
doc.features.forEach(function(feature) {
db.collection.deleteOne(
{ "feature.properties.TYPE": "8003" }
);
print( "in ")
});
});
Does anybody know, why this does not delete anything or how to delete the matching elements?
For me it seems that the failure is inside the forEach, since the print statement gets executed as expected.