Could you help to implement a scheduling of remove data from databases by realm

Hello ,
I am new in MongoDB Atlas realm, this is why I am asking for help on the following context:
I have a Mongo shell script that is removing data by bulk, I have tested it manually and it works
SO, I need to schedule this task to be executed one time per week,

I wanted to know if it is possible to adapt the shell script to be run using realm feature (creating a function and scheduling it through a trigger)

Do you have any example to help me to implement this kind of task?

Thank you for your help
And best regards
Sandrine

here is a piece of the script:

let counter = 0;
let q = db[collection_name].find({"theField": myvalue});     // Perform a find 
let total = q.count();
if(total > 0){
let bulk = db[collection_name].initializeUnorderedBulkOp();
// iterate over the collection
q.forEach(function(d) {
    counter++;
    bulk.find({ _id: d._id }).remove();
    if (counter % 500 == 0) {                                  // Execute when we have 500 documents in our bulk operation
        result = bulk.execute();
        sleep(1000);                                            // Sleep for time
        bulk = db[collection_name].initializeUnorderedBulkOp();  // Initialize on collection for next iteration
    }
});
//last block
if(counter > 0){
    bulk.execute();
}

Hi Sandrine - yes you should be able to do this via a scheduled trigger which will run a Realm Function at a scheduled time. Realm functions can provide the functionality you just described.

To get a collection from the function, you can use the following snippet:

const myCollection = context.services.get("mongodb-atlas").db("myDbName").collection("myCollectionName");

For the rest of your code, you might find the snippets in the MongoDB Data Access docs useful as they provide Realm Function snippets for various CRUD operations.

1 Like