示例代码
var mongo = require("mongodb"),
Server = mongo.Server,
mongoServer = new mongo.Server('localhost', 27017,server_options),
db=new mongo.Db('test',mongoServer , db_options);
db.open(function(err){
if(!err){
db.collection('blog',{safe:true},function(err,coll){
if(!err){
coll.find().toArray(function(err,docs){
console.log(docs);
db.close();
});
}
});
}
});
mongoskin
var db = require('mongoskin').db('localhost:27017/test');
db.collection('blog').find().toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
php
$mg=new Mongo('localhost');
$coll=$mg->selectCollection('test','blog');
$rows=$coll->find();
我用楼主提供的那段MongoSkin代码在node.js里运行,总是出现如下error:
var db = require(‘mongoskin’).db(‘localhost:27017/test’); db.collection(‘blog’).find().toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); });
报错信息:
TypeError: Cannot read property ‘name’ of undefined at Object.makeSkinClass (d:\progra~1\nodejs\node_modules\mongoskin\lib\utils.js:33:43) at Object.<anonymous> (d:\progra~1\nodejs\node_modules\mongoskin\lib\mongo_client.js:7:29) at Module._compile (module.js:456:26) at Object.Module._extensions…js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.require (module.js:364:17) at require (module.js:380:17) at d:\progra~1\nodejs\node_modules\mongoskin\lib\index.js:25:15 at Array.forEach (native)