We have a mongoDB replica set of 3 running in EKS cluster. One primary and 2 secondary.
MongoDB server version: 3.4.10
MongoDB shell version v3.4.10
Application code is in nestjs framework.
We have a requirement that some of the queries should execute on the secondary member. So we are attaching a code snippet below that we tried in an aggregate query,
aggregate(query).read('secondary').exec()
But when we test the operation, sometimes we can see the log in the primary member. Some times log is not showing in any of the 3 members.
Unfortunately, it’s difficult to say what is happening as the MongoDB 3.4 series is out of support. I would recommend you upgrade to a supported version of MongoDB using the WiredTiger storage engine as soon as possible.
Let me know if you have any further questions or concerns.
Is there any way to know if a query is run on primary or secondary?
@Sebin_Sebastian , you can use the Node driver’s command monitoring functionality to instrument your application to emit events as commands are executed, which can contain details about the host address that executed the command (see commandSucceededEvent as an example).
This won’t tell you outright if the host that was targeted is the PRIMARY or SECONDARY, but if you know the current topology of the cluster at the time the commands ran you’ll know which host is in what state.