Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用URI和服务器连接到MongoDB服务器_Node.js_Mongodb_Asynchronous_Synchronous - Fatal编程技术网

Node.js 如何使用URI和服务器连接到MongoDB服务器

Node.js 如何使用URI和服务器连接到MongoDB服务器,node.js,mongodb,asynchronous,synchronous,Node.js,Mongodb,Asynchronous,Synchronous,我想对我的mongoDB进行同步查询,npm指示我使用以下命令: var Server = require("mongo-sync").Server; var server = new Server('127.0.0.1'); var result = server.db("test").getCollection("posts").find().toArray(); console.log(result); server.close(); 但是,我的数据库位于远程位置,因此我有一个(uri)

我想对我的mongoDB进行同步查询,npm指示我使用以下命令:

var Server = require("mongo-sync").Server;
var server = new Server('127.0.0.1');
var result = server.db("test").getCollection("posts").find().toArray();
console.log(result);
server.close();
但是,我的数据库位于远程位置,因此我有一个(uri)。通常,我会对MongoClient使用这样的uri,如下所示:

var MongoClient = require('mongodb').MongoClient;
uri = mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

MongoClient.connect(uri, function(err, db) {
    // I'd read/update my DB right here
    // But this is asynchronous...
});
但是mongo sync没有MongoClient;它只有一个服务器。那么,在这种情况下,我如何建立连接

调试更新 我现在得到这个错误:

Error: Error: connect ENOENT //heroku_7cr19362
    at Object.exports._errnoException (util.js:1026:11)
at exports._exceptionWithHostPort (util.js:1049:20)
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1136:14)
Followed by:
    at DB.open (/Users/Chege/NodeJS_Projects/node_modules/mongo-sync/lib/mongo-sync.js:27:13)
at Server.db (/Users/Chege/NodeJS_Projects/node_modules/mongo-sync/lib/mongo-sync.js:50:10)
at /Users/Chege/NodeJS_Projects/Constructing_the_Database/UpdateDB.js:28:22
导致错误的代码是:

var mongoURI = "mongodb://heroku_7cr19362:{PASSWORD}@ds153352.mlab.com:53352/heroku_7cr19362"

Fiber(function() {
    var server = new Server("mongodb://heroku_7cr19362:{PASSWORD}@ds153352.mlab.com:53352/");
    var result = server.db('heroku_7cr19362').getCollection("songs").find().toArray();
    console.log(result);
    console.log("Last statement");
}).run();

所以我现在的问题是:URI的哪一部分用于实例化服务器实例?现在,我正在尝试URI的不同子字符串,但是解释一下什么会出现在哪里以及为什么会出现是非常棒的

只需将连接URI替换为“127.0.0.1”

var Server = require("mongo-sync").Server;
var server = new Server('mongodb://someaksdmkaasdk');
var result = server.db("test").getCollection("posts").find().toArray();
console.log(result);
server.close();

好的,我仍然得到
错误:connectenoint
让我添加更多上下文。mLabs连接字符串的格式为
mongodb://:@ds153352.mlab.com:53352/heroku_7cr459251
。假设我们有一个字符串,比如
mongodb://heroku_7cr459251:h3li8jrubi4gndgl8ec6n0s5us@ds129674.mlab.com:29674/heroku_7cr459251
您在服务器构造函数中放入了什么?是否将数据库置于uri的末尾,然后再次在数据库(“名称”)中指定它?数据库是否存在?数据库正在运行吗?请指定错误…我已经在Github上运行了。从仪表板上,我可以看到一个名为“歌曲”的集合。我认为db的名字是结尾的最后一部分,
hero‌​ku_7cr459251
您是否更熟悉直接连接MongoDB Atlas?我可以调整代码以查询Atlas上托管的数据库,以适应您的专业知识。@切格,如果您不介意,请您明确指定错误,好吗?可以。让我移动到一个新的评论,这样我可以得到正确的格式。