Dammi
Dammi

Reputation: 1278

MongoDB query returning wrong results

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

Answers (1)

Huu-Danh Pham
Huu-Danh Pham

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

Related Questions