2017-10-04 114 views

回答

0

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

相关问题