Pregunta

I have a collection with some bad data. I would like to bulk delete this data based on some query. This is easy in the mongo shell as db.collection.remove() allows you to specify the justOne option. Is there some way to do this in the node.js driver? findAndRemove seems to only delete 1 document and has no option to make it bulk?

db.collection(collection_name, function(err, collection){
  collection.findAndRemove({type: 'LUXURY'}, function(err, result){
    // result is only a single document
    console.log(result._id.toString());
  });
});

I know an alternative to this is to find() all documents that satisfy my query and manually create a BulkOp using initializeUnorderedBulkOp and populate it by iterating over the results of my find, but I feel there should be an easier way.

¿Fue útil?

Solución

If you are using node-mongo-native driver, I can't see any reason why you don't use the remove directly.

db.collection(collection_name, function(err, collection){
  collection.remove({type: 'LUXURY'}, function(err, result){
    // your code here.
  });
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top