How do I delete all the chunks
documents which are returned as a result of this aggregation?
db.getCollection('chunks').aggregate([
{
$lookup:
{
from: "files",
localField: "files_id",
foreignField: "_id",
as: "file"
}
},
{
$match:
{
"file.uploadDate":
{
$lt: ISODate("2017-06-10T00:00:00.000Z")
}
}
}
])
My schema has a collection named files
, which contains file metadata (name, uploadDate) and chunks, which contain the actual data (binary, files_id)
I am aware of db.collection.deleteMany({})
however it accepts only a match filter.
I have MongoDB 3.2
Loop the results:
var ops = [];
db.getCollection('chunks').aggregate([
{
$lookup:
{
from: "files",
localField: "files_id",
foreignField: "_id",
as: "file"
}
},
{
$match:
{
"file.uploadDate":
{
$lt: ISODate("2017-06-10T00:00:00.000Z")
}
}
}
]).forEach(doc => {
ops = [
...ops,
{ "deleteOne": {
"filter": { "_id": doc._id }
}}
];
if ( ops.length >= 1000 ) {
db.getCollection('chunks').bulkWrite(ops);
ops = [];
}
});
if ( ops.length > 0 ) {
db.getCollection('chunks').bulkWrite(ops);
ops = [];
}
Or in environments without ES6:
var ops = [];
db.getCollection('chunks').aggregate([
{
$lookup:
{
from: "files",
localField: "files_id",
foreignField: "_id",
as: "file"
}
},
{
$match:
{
"file.uploadDate":
{
$lt: ISODate("2017-06-10T00:00:00.000Z")
}
}
}
]).forEach(function(doc) {
ops.push({ "deleteOne": { "filter": { "_id": doc._id } } });
if ( ops.length >= 1000 ) {
db.getCollection('chunks').bulkWrite(ops);
ops = [];
}
});
if ( ops.length > 0 ) {
db.getCollection('chunks').bulkWrite(ops);
ops = [];
}
Using .bulkWrite()
then you are basically "batching" the requests in lots of 1000. So the actual writes and responses from the database happen only at tha time and not for all entries.
You cannot supply an aggregation pipeline as the query argument to the general .remove**()
methods. So what you do instead is loop the cursor with an action like this.
After you getting aggregate result you can use map
function to get all chunk
ids and then you can use db.collection.remove()
with $in
operator.
var pipeline = [
{$lookup:{
from: "files",
localField: "files_id",
foreignField: "_id",
as: "file"
}
},
{$match:{
"file.uploadDate":
{
$lt: ISODate("2017-06-10T00:00:00.000Z")
}
}
}
];
var cursor = db.chunks.aggregate(pipeline);
var chunkIds = cursor.map(function (chunk) { return chunk._id; });
db.chunks.remove({"_id": { "$in": chunkIds }});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With