Using Multiple Mongodb Databases with Meteor.js
Update
It is now possible to connect to remote/multiple databases:
var database = new MongoInternals.RemoteCollectionDriver("<mongo url>");
MyCollection = new Mongo.Collection("collection_name", { _driver: database });
Where <mongo_url>
is a mongodb url such as mongodb://127.0.0.1:27017/meteor
(with the database name)
There is one disadvantage with this at the moment: No Oplog
Old Answer
At the moment this is not possible. Each meteor app is bound to one database.
There are a few ways you can get around this but it may be more complicated that its worth:
One option - Use a separate Meteor App
In your other meteor app (example running at port 6000 on same machine). You can still have reactivity but you need to proxy inserts, removes and updates through a method call
Server:
Cats = Meteor.Collection('cats')
Meteor.publish("cats", function() {
return Cats.find();
});
Meteor.methods('updateCat, function(id, changes) {
Cats.update({_id: id}, {$set:changes});
});
Your current Meteor app:
var connection = DDP.connect("http://localhost:6000");
connection.subscribe("cats");
Cats = Meteor.Collection('cats', {connection: connection});
//To update a collection
Cats.call("updateCat", <cat_id>, <changes);
Another option - custom mongodb connection
This uses the node js mongodb native driver.
This is connecting to the database as if you would do in any other node js app.
There is no reactivity available and you can't use the new Meteor.Collection
type collections.
var mongodb = Npm.require("mongodb"); //or var mongodb = Meteor.require("mongodb") //if you use npm package on atmosphere
var db = mongodb.Db;
var mongoclient = mongodb.MongoClient;
var Server = mongodb.Server;
var db_connection = new Db('cats', new Server("127.0.0.1", 27017, {auto_reconnect: false, poolSize: 4}), {w:0, native_parser: false});
db.open(function(err, db) {
//Connected to db 'cats'
db.authenticate('<db username>', '<db password>', function(err, result) {
//Can do queries here
db.close();
});
});
In Meteor.js, how would I have two development projects use the same Mongo instance?
Yeah, you can just start meteor with the MONGO_URL
parameter like:
$ MONGO_URL="mongodb://localhost:27017/myapp" meteor
or
$ MONGO_URL="mongodb://localhost:27017/myapp" meteor --port 4000
This assumes you have mongodb installed on your system. See this question for ways to make this process a little easier by using environment variables or a start script.
Multiple DB in meteor keeping reactivity
As you say; Default Connection
isn't really an option as you can only have one DB and DDP
is a bit superfluous when you only need a DB and none of the Meteor stuff. I'd think, therefore, your best approach would be to use the MongoInternals
option.
The only thing missing from that option is reactivity; a method of enabling oplog tailing for these additional DB connections is mentioned in this answer. It essentially seems to be a case of passing the oplogUrl when creating the RemoteCollectionDriver
, here's the example given in their answer:
var driver = new MongoInternals.RemoteCollectionDriver(
"mongodb://localhost:27017/db",
{
oplogUrl: "mongodb://localhost:27017/local"
});
var collection = new Mongo.Collection("Coll", {_driver: driver});
Meteor Server-only Web App connecting to Multiple Databases
You should define what a "collection name" is.
So within this code:
const database = new MongoInternals.RemoteCollectionDriver("<mongo url 1>");
const MyCollection = new Mongo.Collection("collection_name", { _driver: database });
const database2 = new MongoInternals.RemoteCollectionDriver("<mongo url 2>");
const MyCollection2 = new Mongo.Collection("collection_name", { _driver: database2 });
From your "meteor app" perspective, MyCollection
and MyCollection2
are two different javascript constants and therefore two different "meteor collections". Based on how javascript works, they cannot be named the same.
But since they connect to two different databases and different databases are completely different things (unless the two mongo url strings are the same), their "database collection" names can be the same. They still are two different collections in two different databases.
From a database perspective, concurrency does not matter either. They can respond to concurrent database calls just fine because they are different databases. But then again, your javascript application code must be able to manage this concurrency and handle its results and exceptions, because javascript is not threaded, but evented.
Can a single Meteor instance listen and react to multiple MongoDB databases?
Yes you can do this by changing how you define the collection on the server side using:
var database = new MongoInternals.RemoteCollectionDriver("<other mongo url>");
MyCollection = new Mongo.Collection("collection_name", { _driver: database });
Related Topics
JavaScript Can't Find Element by Id
Jquery.Getjson - Access-Control-Allow-Origin Issue
Watch Multiple $Scope Attributes
Jqgrid - Maintain Check Box Selection State - Page Refresh/Redirect/Reload
How to Make API Call with Hooks in React
Capture Value Out of Query String with Regex
Sort an Array of Objects Based on Another Array of Ids
How to Remove Spaces from a String Using JavaScript
How to Implement Prepend and Append with Regular JavaScript
How to Display Binary Data as Image in React
Var Name Produces Strange Result in JavaScript
How to Upload a File Using Jquery.Ajax and Formdata
How to Round a Number in JavaScript? .Tofixed() Returns a String
How to Calculate 3D Rotation on X and Y Axis from a 4X4 Matrix
How to Stop a Page from Unloading (Navigating Away) in Js
How to Highlight the Text of the Dom Range Object