如何在Cloud9中连接MongoDB?

alf*_*red 4 mongodb node.js cloud9-ide

我在Cloud9中连接数据库MongoDB有问题请帮助解决此问题!

var MongoClient = require("mongodb").MongoClient;
var port = process.env.PORT;
var ip   = process.env.IP;
MongoClient.connect("mongodb://"+ip+":"+port+"/test",function(error,db){
        if(!error){
         console.log("We are connected");   
        }
        else{
         console.dir(error); //failed to connect to [127.4.68.129:8080]               
        }
});
Run Code Online (Sandbox Code Playgroud)

输出:

Running Node Process
Your code is running at 'http://demo-project.alfared1991.c9.io'.
Important: use 'process.env.PORT' as the port and 'process.env.IP' as the host in your scripts!
[Error: failed to connect to [127.4.68.129:8080]]
Run Code Online (Sandbox Code Playgroud)

小智 7

如果您按照https://docs.c9.io/setting_up_mongodb.html此链接进行操作,您将在工作区下设置并运行mongodb守护程序.

如果你看看输出./mongod,你会发现这个输出:

2015-08-22T12:46:47.120+0000 [initandlisten] MongoDB starting : pid=7699 port=27017 dbpath=data 64-bit host=velvetdeth-express-example-1804858
Run Code Online (Sandbox Code Playgroud)

只需将主机和端口值复制到您的mongodb配置,设置数据库URL,在这种情况下是:

mongodb://velvetdeth-express-example-1804858:27017
Run Code Online (Sandbox Code Playgroud)