简体   繁体   English

我的快速应用程序无法连接到mongodb

[英]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: 一些评论:

  • are you sure your MongoDB is running on port 51241? 您确定您的MongoDB在端口51241上运行吗? The normal port is 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.

相关问题 无法从我的快速应用程序连接到在 localhost:27017 上运行的本地 MongoDB 服务器 - Can't connect to the local MongoDB server running on localhost:27017 from my express application 我正在尝试从Express应用程序连接到MongoDB。 我认为我的连接字符串抛出语法错误 - I am trying to connect to connect to MongoDB from my Express Application. It is throwing a syntax error at my connection string I think 无法将 MongoDB Atlas 连接到我的 goorm IDE - Could not connect MongoDB Atlas to my goorm IDE Node/express 应用程序无法使用 mongoose 连接到 Mongodb Atlas - Node/express application failing to connect to Mongodb Atlas using mongoose 无法将 mongoDb 连接到 Express - Unable to connect mongoDb to Express 使用 mongodb 测试快速应用程序 - Testing express application with mongodb Express-无法连接到MongoDB服务器 - Express - Unable to Connect to MongoDB Server 为什么mongodb无法与我的nodejs简单应用程序连接? - why mongodb doesnt connect with my nodejs simple application? 部署到 azure 的 Express.js 应用程序不会连接到云中的 mongoDB - Express.js application deployed to azure won't connect to mongoDB in cloud 如何让 MongoDB Atlas 云数据库配合并连接到我的 Nodejs/Express 应用程序? - How can I get the MongoDB Atlas cloud database to cooperate and connect to my Nodejs/Express app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM