2

I am able to create new meteor collection using Test = new Meteor.Collection("testCollection")

But it creates testCollection inside admin database of my mongo installation.

Say I have two separate databases inside mongo like testing and another is admin. How to create above collection in testing db inside mongo installation?

Moreover Can I specify somewhere, that I want to cap/uncap the particular collection in order to define the size of collection.

Pratik K
  • 33
  • 4

1 Answers1

3

If you just want to use the testing database, you can overwrite the MONGO_URL environment variable before calling your app, for example (use the correct url to your database):

$ export MONGO_URL=mongodb://localhost:27017/testing
$ meteor

If you want to use different databases within your app, you should use the new _driver parameter. Just use the same mongo url as your default database, but replacing the database name!

  // this replace is just for explicit demonstration. Static string is advised
  var mongo_url = process.env.MONGO_URL.replace("/admin","/testing");
  var testing = new MongoInternals.RemoteCollectionDriver(mongo_url);
  Test = new Mongo.Collection("testCollection", { _driver: testing });

As for capped collections, it was answered properly in this meteor issue and fixed by this commit:

col1 = new Meteor.Collection("myCollection");
coll._createCappedCollection(numBytes, maxDocuments);

To my knowledge, you cannot uncap a previously capped collection.

Note that for these methods to work, you will have to dissociate collection creations between server and client, since clients cannot access your server's databases. In the client, just create your collections as usual, with the same name as the server version:

if (Meteor.isServer) {
  var testing = new MongoInternals.RemoteCollectionDriver("<mongo url testing>");
  Test = new Mongo.Collection("testCollection", { _driver: testing });
  Test._createCappedCollection(2000000, 500); // capped to 2,000,000 Bytes, 500 documents
}
else {
  Test = new Meteor.Collection("testCollection");
}
Community
  • 1
  • 1
SylvainB
  • 4,765
  • 2
  • 26
  • 39
  • The thing is I want to keep admin db in mongo untouched. And want meteor to USE only my defined db. So, in this case, still I need to use "_driver" ? – Pratik K Jun 04 '15 at 14:03
  • Do you want to use the `admin` collection at all in your app? If you don't, you just need overwrite the `MONGO_URL` environment variable to use "testing" instead of "admin", and then you can create collections normally and they will be put in the "testing" database. – SylvainB Jun 04 '15 at 16:06
  • Can you tell me that how to set ```MONGO_URL``` in meteor. I guess, it is going to be one time process. And Would you suggest anything else, as now I had already in-place ```meteor accounts``` package in system with it's own collection in ```admin``` db? Or everything will be done automatically once the ```MONGO_URL``` is changed. – Pratik K Jun 04 '15 at 17:43
  • Create a settings.json like in [this question](http://stackoverflow.com/questions/21971036/mongodb-meteor-export-mongo-url-to-deployed-applications) and start the server using `meteor --settings settings.json` – SylvainB Jun 04 '15 at 17:53