简体   繁体   中英

How do I connect to an external MongoDB Collection in a Meteor App

By default the Meteor app has access to collections in a database called meteor, but I would like to connect to a collection contained in a different database.

Looking around, I found some snippets which try to do the following:

var mongoUrl = 'mongodb://127.0.0.1:27018/meteor';
var oplogUrl = 'mongodb://127.0.0.1:27018/local';

var remoteDriver = new MongoInternals.RemoteCollectionDriver(mongoUrl, {
    oplogUrl: oplogUrl,
});

var remoteCollection = new Mongo.Collection('extract', {
    _driver: remoteDriver,
});

But doing so my app is crashing giving the following error:

> Exited with code: 8
? (STDERR)
? (STDERR) /Users/test/.meteor/packages/meteor-tool/.1.3.3.ems6rm++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:280
? (STDERR)                          throw(ex);
? (STDERR)                                ^
? (STDERR) Error: $MONGO_OPLOG_URL must be set to the 'local' database of a Mongo replica set
? (STDERR)     at Error (<anonymous>)
? (STDERR)     at OplogHandle._startTailing (packages/mongo/oplog_tailing.js:216:13)
? (STDERR)     at new OplogHandle (packages/mongo/oplog_tailing.js:76:8)
? (STDERR)     at new MongoConnection (packages/mongo/mongo_driver.js:216:25)
? (STDERR)     at new MongoInternals.RemoteCollectionDriver (packages/mongo/remote_collection_driver.js:4:16)
? (STDERR)     at meteorInstall.collections.job_extract.js (collections/job_extract.js:10:20)
? (STDERR)     at fileEvaluate (packages/modules-runtime/.npm/package/node_modules/install/install.js:153:1)
? (STDERR)     at require (packages/modules-runtime/.npm/package/node_modules/install/install.js:82:1)
? (STDERR)     at /Users/test/app_meteor/.meteor/local/build/programs/server/app/app.js:1453:1
? (STDERR)     at /Users/test/app_meteor/.meteor/local/build/programs/server/boot.js:291:10
? (STDERR)     at Array.forEach (native)
? (STDERR)     at Function._.each._.forEach (/Users/arj/.meteor/packages/meteor-tool/.1.3.3.ems6rm++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
? (STDERR)     at /Users/test/app_meteor/.meteor/local/build/programs/server/boot.js:133:5

You can do it through running command inside an meteor app export MONGO_URL=URL. Then your app will be connected to the URL database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM