I have set up a MongoDB replica set with three nodes in my local environment. The replica set consists of one primary node and two secondary nodes. I’m using Node.js to connect to one of the secondary members in the replica set.
However, when I attempt to run the planCacheListFilters command on the secondary member, I encounter the following error:
Error:
MongoServerError: not primary and secondaryOk=false
This error suggests that the command is unable to run on a secondary node because it requires a primary node, and the secondaryOk option is set to false.
I would like to understand how to properly configure my MongoDB connection or the planCacheListFilters command to execute successfully on a secondary node. Additionally, if there are any specific considerations or permissions required for running this command on secondary members, I would appreciate guidance on that.
Code:
const { MongoClient } = require('mongodb');
// The MongoDB URI for the secondary member in the replica set
const uri = 'mongodb://ak:nomercy@localhost:27018/?authSource=admin&directConnection=true';
const client = new MongoClient(uri);
async function connect() {
try {
await client.connect();
console.log('Connected to MongoDB');
const db = client.db('ak');
const { filters } = await db.command({ planCacheListFilters: 'test' });
filters.forEach(function (plan) {
console.log(plan);
});
} catch (err) {
console.error('Error connecting to MongoDB:', err);
} finally {
await client.close();
}
}
connect();
Version details:
Mongo version: 4.4.24
Node JS version: v20.5.1
Mongo driver version: 6.0.0