Reputation: 1278
I have two computers that communicate to the same MongoDB collection. I will give a simple example on what they do, I use PyMongo as the CLI interface
Computer A: Every 1s:
from bson import ObjectId
from pymongo import MongoClient
# Create payload
client = MongoClient(mongo_html)
collection_post = client["MyCollection"].posts
pay_load = {
"number": 10,
"is_processed": False
}
collection_post.insert_one(pay_load)
Computer B: Every 10s:
from bson import ObjectId
from pymongo import MongoClient
# Create payload
client = MongoClient(mongo_html)
collection_post = client["MyCollection"].posts
all_docs = collection_post.find({"is_processed": False})
for doc in all_docs:
do_work(doc)
collection_post.update({"_id": ObjectID(doc["_id"])}, {"is_processed", True})
But the problem I face is that in computer B, I sometimes get documents that have "is_processed" True into the "all_docs" cursor. I assume this is related to MongoDB replication procedure?
Is there a better way to handle this? Can I somehow "force" Computer B to get a "updated" version of the collection?
Upvotes: 0
Views: 1029
Reputation: 291
You should know the fact in MongoDB that:
Reads may miss matching documents that are updated during the course of the read operation.
You can read David Glasser's post for more information.
Upvotes: 1