Search code examples
javamongodbmorphia

How can I find and delete MongoDb Collection Entity's in one move and save system resources


I have a mongodb collection with realtime data with a short lifecycle. When I need them I also want to delete the once I find,

Here's pseudo code using morphia wrapper:

for(LogEntity log: mongo.find(LogEntity.class, "grabMe", true)){
    mongo.delete(mongo.createQuery(LogEntity.class).filter("logId", log.id));
    //Do work and dispose the log
}

I'm running some tests on this and its really difficult to simulate a heavy load because this is part of Google GCM.

This is running in a Runnable that are fed from an ScheduledExecutorService scheduleAtFixedRate every 2 sec.

Is this going to work or is there a better way to do this. How is the find() working in this situation. Will I mess up the find()'s internal array by deleting from it. If there are many LogEntity I cannot hold them in memory.


Solution

  • heavy load logging with short lifecycle, I will choose capped collection. in this situation, you cannot delete a document in the capped collection.

    on the other hand, if you wanna choose normal collection for logging and do a timed clean up, your find and delete loop is slow. if your code is foreach item in collection.find(query) {collection.remove({_id:item._id};} use collection.remove(query) method instead.