Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (47.6k points)

I'm creating a sort of background job queue system with MongoDB as the data store. How can I "listen" for inserts to a MongoDB collection before spawning workers to process the job? Do I need to poll every few seconds to see if there are any changes from last time, or is there a way my script can wait for inserts to occur? This is a PHP project that I am working on, but feel free to answer in Ruby or language agnostic.

1 Answer

0 votes
by (106k points)

To listen for changes to a MongoDB collection since the release of MongoDB 3.6 there is a new notifications API called Change Streams which you can use for this. See this example for it below:-

cursor = client.my_db.my_collection.changes([ 

{'$match': {

 'operationType': {'$in': ['insert', 'replace']} 

}}, 

{'$match': { 

'newDocument.n': {'$gte': 1} 

}} 

]) 

for change in cursor: 

print(change['newDocument'])

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...