Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

316
Views
Eliminar documento que tiene un tamaño mayor que un valor específico

Tengo una colección que contiene varios documentos cuyo tamaño ha aumentado de 16 MB o está a punto de alcanzar los 16 MB. Quiero una consulta que encuentre documentos que tengan un tamaño superior a 10 MB y los elimine todos.

Estoy usando lo siguiente para encontrar el tamaño del documento.

 Object.bsonsize(db.test.findOne({type:"auto"}))

¿Hay alguna manera de incrustar esta consulta dentro de la consulta db.test.deleteMany() ?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Esta siguiente consulta elimina los documentos con un tamaño mayor que el tamaño especificado ( el tamaño se especifica en bytes ). Esta consulta es válida con MongoDB v4.4 o superior .

 db.collection.deleteMany( { $expr: { $gt: [ { $bsonSize: "$$ROOT" }, SIZE_LIMIT ] }, type: "auto" } )

El siguiente script se ejecuta para MongoDB v4.2 o anterior:
 const SIZE_LIMIT = 75 // substitute your value here in bytes let idsToDelete = [ ] let crsr = db.collection.find() while(crsr.hasNext()) { let doc= crsr.next() if (Object.bsonsize(doc) > SIZE_LIMIT) { idsToDelete.push(doc._id) } } db.collection.deleteMany( { _id: { $in: idsToDelete } } )
over 4 years ago · Santiago Trujillo Report

0

Creo que tienes que hacerlo así:

 db.test.aggregate([ { $match: { type: "auto" } }, { $project: { bsonSize: { $bsonSize: "$$ROOT" } } }, { $match: { bsonSize: { $gt: 16e6 } } }, ]).forEach(function (doc) { db.test.deleteOne({ _id: doc._id }); })

O si lo prefieres deleteMany:

 var ids = db.test.aggregate([ { $match: { type: "auto" } }, { $project: { bsonSize: { $bsonSize: "$$ROOT" } } }, { $match: { bsonSize: { $lt: 16e6 } } } ]).toArray().map(x => x._id); db.test.deleteMany({ _id: { $in: ids } });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!