0

我试过下面提到的代码来使用 mongoskin 节点模块连接 mongo db

var mongo = require('mongoskin');
var db = mongo.db("localhost:27017/mydb");
db.bind('mycollection');
db.mycollection.find().toArray(function(err, items) {
console.log(items)
        db.close();
});

我收到下面提到的错误。

/usr/local/lib/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/url_parser.js:15
    throw Error("URL must be in the format mongodb://user:pass@host:port/dbnam
          ^
Error: URL must be in the format mongodb://user:pass@host:port/dbname
    at Error (<anonymous>)
    at exports.parse (/usr/local/lib/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/url_parser.js:15:11)
    at Function.MongoClient.connect (/usr/local/lib/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/mongo_client.js:164:16)
    at SkinClass.SkinDb._open (/usr/local/lib/node_modules/mongoskin/lib/db.js:36:25)
    at SkinClass.open (/usr/local/lib/node_modules/mongoskin/lib/utils.js:162:14)
    at SkinClass.SkinCollection._open (/usr/local/lib/node_modules/mongoskin/lib/collection.js:49:17)
    at SkinClass.open (/usr/local/lib/node_modules/mongoskin/lib/utils.js:162:14)
    at SkinClass.SkinCursor._open (/usr/local/lib/node_modules/mongoskin/lib/cursor.js:28:25)
    at SkinClass.open (/usr/local/lib/node_modules/mongoskin/lib/utils.js:162:14)
    at SkinClass.(anonymous function) [as toArray] (/usr/local/lib/node_modules/mongoskin/lib/utils.js:116:14)

在那个错误中它提到了输入用户名和密码。我没有用户名和密码。我必须输入什么请帮助我。

  • 我的mongodb版本是v1.8.2,
  • 节点版本为 v0.10.28,
  • mongoskin 版本是 v1.4.4。
4

2 回答 2

0

答案在于错误本身:

throw Error("URL must be in the format mongodb://user:pass@host:port/dbnam
      ^
Error: URL must be in the format mongodb://user:pass@host:port/dbname

如果您不明白,请按照以下说明进行操作:

var mongo = require('mongoskin');
var db = mongo.db("mongodb://localhost:27017/mydb");
db.bind('mycollection');
db.mycollection.find().toArray(function(err, items) 
{
    console.log(items)
    db.close();
});
于 2014-06-24T13:12:45.643 回答
0

错误说:

 throw Error("URL must be in the format mongodb://user:pass@host:port/dbnam")

您是否尝试过添加协议?(IE:)

    var db = mongo.db("mongodb://localhost:27017/mydb");
于 2014-06-05T10:28:50.797 回答