如何通过使用本地node.js驱动程序连接MongoDB与用户名和密码来检索数据

如何通过使用本地node.js驱动程序连接MongoDB与用户名和密码来检索数据

问题描述:

我已经有我的mongodb的用户名和密码。如何通过使用本地node.js驱动程序连接MongoDB与用户名和密码来检索数据

我想通过使用node.js本地驱动程序从mongodb中的集合中检索数据。

那么如何连接使用Node.js的

感谢

+0

为Y ou使用猫鼬或没有猫鼬? –

Using mongoClient到MongoDB的:

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

var url = 'mongodb://localhost:27017/test'; 

MongoClient.connect(url, function(err, db) { 
    console.log("Connected correctly to server."); 
    db.close(); 
}); 

Using Mongoose

//Import the mongoose module 
var mongoose = require('mongoose'); 
//Set up default mongoose connection 
var mongoDB = 'mongodb://127.0.0.1/my_database'; 
mongoose.connect(mongoDB, { 
    useMongoClient: true 
}); 
//Get the default connection 
var db = mongoose.connection; 
//Bind connection to error event (to get notification of connection errors) 
db.on('error', console.error.bind(console, 'MongoDB connection error:')); 
+0

Thx for reply - @Vishnu Mishra。我得到了解决方案 – Nani