MongoDB - 错误:无效的模式,期望是mongodb。

43

我刚开始使用MEAN Stack来构建应用程序,我正在尝试构建一个实时聊天应用程序,这是我的服务器端:

console.log("Server running...!");

var mongo=require('mongodb').MongoClient;
var client=require('socket.io').listen(8080).sockets;

mongo.connect('localhost:27017/db/chat',function(err,db){
if(err)  throw err;

client.on('connection',function(socket){
console.log('someone has connected !');

//waiting for input
socket.on('input',function(data){
console.log(data);
});

});

});

我确定我使用mongodb创建了一个名为chat的数据库,mongo也在等待连接。但是当我运行node server.js服务器时,会发生错误:

Server running...!
C:\Users\azus\Desktop\Psirt\codemaster\node_modules\ mongodb\lib\url_parser.js:20
  throw new Error('invalid schema, expected mongodb');
  ^

Error: invalid schema, expected mongodb
at module.exports (C:\Users\azus\Desktop\Psirt\code-master\node_modules\mong
 odb\lib\url_parser.js:20:11)
at connect (C:\Users\azus\Desktop\Psirt\code-master\node_modules\mongodb\lib
 \mongo_client.js:125:16)
at Function.MongoClient.connect (C:\Users\azus\Desktop\Psirt\code-master\nod
e_modules\mongodb\lib\mongo_client.js:109:3)
at Object.<anonymous> (C:\Users\azus\Desktop\Psirt\code-master\server.js:6:8
)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:139:18)

C:\Users\azus\Desktop\Psirt\code-master>

我在这个阶段已经被卡了几周,有没有人能帮忙解决?

谢谢。


1
你可能正在遵循一份旧的教程或文档。那曾经有效,但现在需要mongodb://。 - Adam Mendoza
9个回答

125

优美地在MongoDB 3.6.2和Ubuntu 16.04中工作。 - Rohan_Paul
nosqlclient的用户应该注意,它要求URL以“mongodb://”开头,像“localhost”这样的URL将无法工作。 - jrh

8

我也遇到了这个问题,原因是我协议写错了:

mongo://localhost:27017/test

协议错误也可能引起此错误。应该是这样的:
mongodb://localhost:27017/test

6
有时候,错误可能是由于环境变量周围的引号引起的。删除它们然后再试一下,这可能会有所帮助。
可能出现错误的地方:
 set DATABASE_URI='mongodb://localhost:1000/my_app' && node index.js

正确的命令应该是:

  set DATABASE_URI=mongodb://localhost:1000/my_app && node index.js

这至少在Windows 10上是这种情况,其他系统尚未测试。谢谢! - user25794
2
刚刚遇到了与 docker-compose 环境变量相同的问题。我需要不将其包含在任何引号(单引号或双引号)中。这对我有用。MONGODB_URI=mongodb://mongo:27017/ - Josh Peak
使用mongo-seeding-cli(https://github.com/pkosiec/mongo-seeding/tree/master/cli)。这是我的问题。他们用于指定主机的示例包括引号。但是,包含引号似乎会导致OP的错误。 - BRasmussen

2

试试这个,它有效:

mongoose.connect('mongodb://localhost:27017/shopping');

1

我刚刚解决了同样的问题。该死的Windows在环境变量中保存引号。

因此,如果您使用Windows并以以下方式编写:SET MONGO_URL="mongodb://localhost:27017/{您的数据库名称}",那么这是不正确的。

正确的方法是:SET MONGO_URL=mongodb://localhost:27017/{您的数据库名称},不需要引号。

另外,我发现您必须精确地编写协议 - mongodb。这里有一段代码检查来自文件url_parser.js的协议。

var result = parser.parse(url, true);

if(result.protocol != 'mongodb:') {
    throw new Error('invalid schema, expected mongodb');
}

1

工作代码应该像这样

别忘了替换用户名密码URL

const socketClient = require('socket.io').listen(4000).sockets;
const MongoClient = require('mongodb').MongoClient;

const uri = "mongodb+srv://<username>:<password>@cluster0-saugt.mongodb.net/test?retryWrites=true&w=majority";

const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
    socketClient.on('connection', function (socket) {

        //Need to Get the Database first before trying to access the collections.
        let chat = client.db("test").collection('chats');

        // Get chats from mongo collection
        // perform actions on the collection object
        chat.find().limit(100).sort({ _id: 1 }).toArray(function (err, res) {
            if (err) {
                throw err;
            }

            // Emit the messages
            socket.emit('output', res);
        });


    });

});

0

将此行内容更改为

mongo.connect('localhost:27017/db/chat',function(err,db)

mongo.connect('mongodb://localhost:27017/db/chat',function(err,db)

然后您就可以成功连接 MongoDB 数据库了。


0

请更新您的 MongoDB npm 版本


0

可能看起来很明显,但当您向Mongo客户端传递无效值时,例如undefined,您也会遇到此错误。 当我在引用配置对象上的错误键时,就遇到了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接