nodejs连接mongodb(密码)

const MongoClient = require('mongodb').MongoClient;

const url = 'mongodb://user:password@localhost:27017/?authMechanism=SCRAM-SHA-1&authSource=database';

// Database Name
const dbName = 'aaaa';

// Create a new MongoClient
const op = {
    useNewUrlParser: true
}

const client = new MongoClient(url, op);

// Use connect method to connect to the Server

(async function () {
    await client.connect();
    const db = client.db(dbName);
    const res = await db.collection('users').find().toArray();
    console.log(res);
    client.close()
})();

  

原文地址:https://www.cnblogs.com/xiaosongJiang/p/11208965.html