首页 新闻 会员 周边

windows上nodejs连接mongodb

0
悬赏园豆:200 [已解决问题] 解决于 2011-11-22 18:29

公司在Windows上用nodejs开发web应用,要连接mongodb数据库,在mongodb.org下了node-mongodb-native的zip包,不知道怎么配置,那位高人给指点一下,拜谢!!!在线等啊

wangjianhui的主页 wangjianhui | 初学一级 | 园豆:7
提问于:2011-11-17 16:39
< >
分享
最佳答案
1
收获园豆:200
artwl | 专家六级 |园豆:16736 | 2011-11-17 17:48

将以下代码保存在test.js文件里,执行 node test.js,提示Server running at http://127.0.0.1:3000/,用浏览器访问http://127.0.0.1:3000/提示Cannot call method ‘collection’ of null,郁闷!

代码如下:

var http = require('http');
var mongodb = require('./mongodb');

http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain;charset=utf-8'});
mongodb.connect('mongodb://localhost:40202/log', function(err, conn){
conn.collection('log', function(err, coll){
coll.count({'action': 772}, function(err, count){
res.write('The total of action 772 is ' + count + ".\n");
res.end();
});
});
});
}).listen(3000, '127.0.0.1');

console.log('Server running at http://127.0.0.1:3000/');

wangjianhui | 园豆:7 (初学一级) | 2011-11-18 16:03
其他回答(1)
0

之前在MongoDB萌阔论坛上看到过关于nodejs操作mongodb的相关讨论,你可以去看看,希望能帮助到你。

论坛地址:http://forum.foxera.com/mongodb/topic/442/nodejs操作mongodb?un=Jesse

JesseShi | 园豆:274 (菜鸟二级) | 2017-06-20 09:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册