1,连接单个mongodb的实例,可以参看node mongodb的文档
https://github.com/mongodb/node-mongodb-native
2, 连接mongodb 的mongos服务,只需要修改一下连接配置即可,如下
var MongoClient = require('mongodb').MongoClient
, format = require('util').format;
var url = format("mongodb://%s,%s/%s"
, "192.168.0.60:27017"
, "192.168.0.64:27017"
, "test");
//var url = "mongodb://192.168.0.60/test";
console.log(url);
MongoClient.connect(url, function(err, db) {
if(!err) {
var collection = db.collection('test');
console.log(collection);
collection.find({}).toArray(function(errfind, items) {
if(!errfind) {
console.log(items);
} else {
console.log(errfind);
}
db.close();
});
console.log("We are connected");
}
// db.close();
});