MongoDB: How to run db.adminCommand() in NodeJS
Problem:
You want to run a db.adminCommand()
in NodeJS using the node-mongodb-native client, e.g. you want to run the NodeJS equivalent of
db.adminCommand({setParameter: 1, internalQueryExecMaxBlockingSortBytes: 100151432});
Solution
Use conn.executeDbAdminCommand()
where db
is a MongoDB database object.
db.executeDbAdminCommand({setParameter: 1, internalQueryExecMaxBlockingSortBytes: 100151432});
Full example:
// To install, use npm i --save mongodb
const MongoClient = require('mongodb').MongoClient;
async function configureMongoDB() {
// Connect to MongoDB
const conn = await MongoClient.connect('mongodb://localhost:27017/', { useNewUrlParser: true });
const db = await conn.db('mydb');
// Configure MongoDB settings
await db.executeDbAdminCommand({
setParameter: 1,
internalQueryExecMaxBlockingSortBytes: 100151432
});
// Cleanup
return conn.close();
}
// Run configureMongoDB()
configureMongoDB().then(() => {}).catch(console.error)