[英]My express application could not connect to mongodb
Hi i connect my mongodb with command prompt on windows. 嗨,我将我的mongodb与Windows上的命令提示符连接。 But i could not with express application.
但是我不能用快速申请。 I read too many blog post about node,express and mongodb but could not find the problem.
我读了太多有关node,express和mongodb的博客文章,但找不到问题。 So i need advise.
所以我需要建议。
My database folder : c:/data/db My mongodb folder: c:/mongodb 我的数据库文件夹:c:/ data / db我的mongodb文件夹:c:/ mongodb
In command prompt i type:cd bin mongod Then i type: mongo and it says : 在命令提示符下,输入:cd bin mongod然后输入:mongo,它说:
And i can do whatever i want with command prompt. 而且我可以用命令提示符执行我想做的任何事情。
But when i try to connect mongodb from my node express application i fail always. 但是,当我尝试从我的节点快速应用程序连接mongodb时,我总是失败。 My code is shown below:
我的代码如下所示:
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:51241/ecmarketing');
var ObjectID = require('mongodb').ObjectID;
var collection = db.usercollection.findOne();
res.render('index', { title: 'index' });
};
Always i get: 我总是得到:
package.json: package.json:
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.4",
"ejs": "~0.8.4",
"ejs-locals": "~1.0.2",
"mongodb": "~1.3.19",
"monk": "~0.7.1"
}
}
I installed package with npm install. 我用npm install安装了软件包。 Also i created ecmarketing file in db folder.
另外我在数据库文件夹中创建了ecmarketing文件。 Thats all.
就这样。
A few comments: 一些评论:
27017
; 27017
; with monk
, you need to explicitly get a reference to the collection: 使用
monk
,您需要显式获取对该集合的引用:
var collection = db.get('usercollection');
you don't have to create a folder for the database yourself, MongoDB will do that for you; 您不必自己为数据库创建文件夹,MongoDB会为您完成;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.