1

我正在尝试在 Node.js 环境中运行 Mosca MQTT 代理,其 MongoDB 后端位于 Microsoft 的 Azure 云中。DocumentDB 存储有一个 MongoDB API。

首先,我从 Mosca 网站https://github.com/mcollina/mosca/wiki/Mosca-basic-usage#lets-put-it-all-together-now复制了示例代码

var mosca = require('mosca')

var ascoltatore = {
    type: 'mongo',        
    url: 'mongodb://localhost:27017/mqtt',
    pubsubCollection: 'ascoltatori',
    mongo: {}
};

var moscaSettings = {
    port: 1883,
    backend: ascoltatore,
    persistence: {
        factory: mosca.persistence.Mongo,
        url: 'mongodb://localhost:27017/mqtt'
    }
};

var server = new mosca.Server(moscaSettings);
server.on('ready', setup);

server.on('clientConnected', function(client) {
    console.log('client connected', client.id);     
});

server.on('published', function(packet, client) {
    console.log('Published', packet.payload);
});

function setup() {
    console.log('Mosca server is up and running')
}

...这适用于本地安装的 MongoDB 服务器。

然后我用url:可以从 Azure 门户获取的 Node.js 连接字符串替换了这两个匹配项。之后,与 Azure DocumentDB 的连接失败并出现以下错误:

$ node index.js 
/[...]/node_modules/mongodb/lib/utils.js:98
process.nextTick(function() { throw err; });
                              ^
Error: Cannot recover. Collection is not capped.
at /[...]/node_modules/ascoltatori/lib/mongo_ascoltatore.js:241:26
at handleCallback (/[...]/node_modules/mongodb/lib/utils.js:95:56)
at /[...]/node_modules/mongodb/lib/collection.js:1559:5
at handleCallback (/[...]/node_modules/mongodb/lib/utils.js:95:56)
at /[...]/node_modules/mongodb/lib/collection.js:1528:5
at handleCallback (/[...]/node_modules/mongodb/lib/utils.js:95:56)
at /[...]/node_modules/mongodb/lib/cursor.js:852:16
at handleCallback (/[...]/node_modules/mongodb-core/lib/cursor.js:171:5)
at setCursorDeadAndNotified (/[...]/node_modules/mongodb-core/lib cursor.js:506:3)
at nextFunction (/[...]/node_modules/mongodb-core/lib/cursor.js:652:7)
at Cursor.next [as _next] (/[...]/node_modules/mongodb-core/lib cursor.js:693:3)
at fetchDocs (/[...]/node_modules/mongodb/lib/cursor.js:848:10)
at /[...]/node_modules/mongodb/lib/cursor.js:871:7
at handleCallback (/[...]/node_modules/mongodb-core/lib/cursor.js:171:5)
at nextFunction (/[...]/node_modules/mongodb-core/lib/cursor.js:683:5)
at /[...]/node_modules/mongodb-core/lib/cursor.js:594:7
at queryCallback (/[...]/node_modules/mongodb-core/lib/cursor.js:253:5)
at /[...]/node_modules/mongodb-core/lib/connection/pool.js:457:18
at nextTickCallbackWith0Args (node.js:419:9)
at process._tickCallback (node.js:348:13)

知道这里会缺少什么吗?

4

1 回答 1

0

我不确定,但我假设 Azure DocumentDB 并没有实现 MongoDB 的所有功能,它只是提供相同的网络 API,因此您可以使用现有的客户端与之对话。

在这种情况下,它似乎不支持mosca 将用来限制最终存储在数据库中的数据量的上限集合(基本上就像循环缓冲区)。Mosca 正在测试它创建的集合,发现它没有按预期设置并退出。虽然您可以删除此测试代码,但这可能意味着您最终将获得一个快速增长的集合,该集合期望数据库自动删除旧记录。

于 2017-01-25T16:49:24.757 回答