pymongo.errors.OperationFailure: The $changeStream stage is only supported on replica sets

Hello! I try to implement pymongo changeStream using mongodb.db.collection.watch method. My MongoDB is working on standalone mode on windows 11.
Belows are my simple pymongo codes.

from pymongo import MongoClient

CHANGE_STREAM_DB='mongodb://localhost:27017/mongodb_pipe?retryWrites=true'
mongo = MongoClient(CHANGE_STREAM_DB)

students = [
    { 'name': 'aaa', 'address': 'Seoul', 'course': 'Statistics', 'score': 96 },
    { 'name': 'bbb', 'address': 'Pusan', 'course': 'Biology', 'score': 83 }]

mongo[mongodb_pipe][collection].insert_many(students)
print('student inserted.............................')

pipeline = [{'$match': {'operationType': 'insert'}}]
results = mongo[mongodb_pipe][collection].watch(pipeline=pipeline)

But, watch() method throws Exception,

pymongo.errors.OperationFailure: The $changeStream stage is only supported on replica sets, full error: {'ok': 0.0, 'errmsg': 'The $changeStream stage is only supported on replica sets', 'code': 40573, 'codeName': 'Location40573'}

Now I have a question. Is it possible that the $changeStream stage can be supported on standalone mode? If possible, how to configure? Or if the $changeStream stage is possible only on the replica sets, kindly inform me how to set the mongo mode to replica sets mode with pymongo codes. Any reply will be thanksful! Best regards

You may convert your stand alone to a replica set.

See https://www.mongodb.com/docs/manual/tutorial/convert-standalone-to-replica-set/

1 Like