About using MongoDB with Meteor

I have two question using Meteor with MongoDB.

  1. Can I use Meteor with multiple mongo databases instead of just one? If true, how do I do?
  2. Do I need to use String IDs rather than Object IDs in MongoDB for Meteor? If true, when I use some scripts to add documents into a mongo database, how do I do to generate a String ID like what Meteor does rather than an Object ID generated by MongoDB?

This is how you connect to other dbs:

if(Meteor.isServer){
	var shared = new MongoInternals.RemoteCollectionDriver("mongodb://127.0.0.1:27017/shared");
}
Dictionary = new Mongo.Collection('dictionary', {_driver:shared});

I don’t know if it’s the “correct” way, but you can get a String ID by doing this: new Mongo.ObjectID()._str
Though you should rarely need to manually generate IDs.

1 Like

Or install the random package (meteor add random) and use:

var newStringId = Random.id();

Docs.

I use this occasionally in my apps.

The trouble comes when inserting documents to the database from outside a Meteor context. By default, a mongo object id is generated. The way around this is to explicitly provide a string _id value on the document when inserting it. Then you need to have something equivalent to the meteor random package that can generate (probably) globally unique identifier strings for you.

You can use Mongo.ObjectIds just fine. Meteor gives you the option of using Strings or ObjectIds. Have a look in the docs.

You can use Mongo.ObjectIds, yes. But I’d argue that, in a lot of cases, it makes app development harder than simply having a string to deal with.

I wonder how to generate a String ID like Meteor using other scripting languages like python or ruby when I need to update the MongoDB with those scripting languages.

Yes, I’m using meteortoys:allthings with mongol which requires String IDs to update the documents in the MongoDB.

Do you have any other example to recommend using String IDs rather than Object IDs?

It’s just a random String, so you could generate it however you want, right? Just don’t include any illegal characters :stuck_out_tongue:
For my app I need to retrieve documents from an API which doesn’t provide unique IDs. If I used random IDs, keeping my documents updated would be tricky. My solution was to combine a few of the fields (date and userId in this instance) into a String ID that I knew would always be unique, and easy to reconstruct for updates.

If I use a scripting language to generate the random ID, how do I ensure that the random String ID will not conflict to the existing String ID created by Meteor? Checking whether the generated ID exists before inserting to the collection works but is not efficient.

I think I need to use the same function (or algorithm) which Meteor uses to generate the String ID, doesn’t it?

You have a 1 in 280000000000000000000000000 chance of a conflict if you generate a random 17 character alpha-numeric string (same as Meteor does). This is not something you need to worry about.

This is like the birthday problem though, so the probability of a duplicate will grow exponentially with the size of your collection!

According to an approximating equation I found, if Facebook used random IDs for the entire population of Earth, the probability of a duplicate would be
1 - e^(-6000000000^2/(2*280000000000000000000000000))
Which equals… 1 in 15.625.000 :grinning: