MongoDB:如何在 NodeJS 中运行 db.adminCommand()
问题:
你想使用 node-mongodb-native 客户端在 NodeJS 中运行 db.adminCommand(),例如你想运行 NodeJS 等效的
admin_command_example.js
db.adminCommand({setParameter: 1, internalQueryExecMaxBlockingSortBytes: 100151432});解决方案
使用 conn.executeDbAdminCommand(),其中 db 是 MongoDB 数据库对象。
admin_command_example_full.js
db.executeDbAdminCommand({setParameter: 1, internalQueryExecMaxBlockingSortBytes: 100151432});完整示例:
mongodb_full_example.js
// 要安装,使用 npm i --save mongodb
const MongoClient = require('mongodb').MongoClient;
async function configureMongoDB() {
// 连接到 MongoDB
const conn = await MongoClient.connect('mongodb://localhost:27017/', { useNewUrlParser: true });
const db = await conn.db('mydb');
// 配置 MongoDB 设置
await db.executeDbAdminCommand({
setParameter: 1,
internalQueryExecMaxBlockingSortBytes: 100151432
});
// 清理
return conn.close();
}
// 运行 configureMongoDB()
configureMongoDB().then(() => {}).catch(console.error)If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow