I'm running a script that pushes some data to a MongoDB database, now i'm trying to have another Python script printing the new entries on my DB each time one is added.
For example, if the number 80 is added to the DB, the script should fetch 80 from the collection and print it to my console as soon as it's added on the database.
My actual work is running fine, the only problem is that if i remove the time.sleep() it will start printing every entry quickly, and right now, instead of printing the new entry, it prints the whole collections + the new entry, instead of printing only the new one (i'm trying to do that because in the future i want my script to fetch the data and feed it later to a Python array).
a) i can't use change_stream since my DB is not a replica set, i'm fairly new to this so i don't know much about replica sets b) i could use a tailable cursor, but using a capped database wouldn't be the best choice, since i will be pushing data every 5 second, and having a "limit" (isn't that what capped means?) would not be the best
Any advice?
from pymongo import MongoClient
import time
import random
from pprint import pprint
client = MongoClient(port=27017)
arr = []
db = client.one
mycol = client["coll"]
while True:
cursor = db.mycol.find()
for document in cursor:
print(document['num'])
time.sleep(2)
from Python - how to 'stream' data from my MongoDB collection?
No comments:
Post a Comment