Skip to content

Instantly share code, notes, and snippets.

@elephant
Created December 9, 2011 16:42
Show Gist options
  • Save elephant/1452287 to your computer and use it in GitHub Desktop.
Save elephant/1452287 to your computer and use it in GitHub Desktop.
Delay the execution of MongoDB update queries
//function to be executed on a "delayed" fashion, use on a mongodb query.forEach()
var delayedUpdateFn = function(theDocument) {
var waitMilliseconds = 500; //change to whatever delay you want
var waitUntil = Date.now() + waitMilliseconds;
while(Date.now() < waitUntil) {
//sleep
}
//insert code to execute on this document
//theDocument.something = "hello";
};
//example usage on a query:
db.listings.find({"_types":{$in:["VacationRentalListing"]}}).forEach(delayedUpdateFn);
@yarkovaleksei
Copy link

`function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

//function to be executed on a "delayed" fashion, use on a mongodb query.forEach()
async function delayedUpdateFn(theDocument) {
var waitMilliseconds = 500; //change to whatever delay you want
var waitUntil = Date.now() + waitMilliseconds;
while(Date.now() < waitUntil) {
await sleep(5000);
}
//insert code to execute on this document
//theDocument.something = "hello";
};

//example usage on a query:
db.listings.find({"_types":{$in:["VacationRentalListing"]}}).forEach(delayedUpdateFn);`

Реализация паузы в цикле)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment