Why does Meteor.js use it's own algorithms for IDs?
Why doesn't it use MongoDB's ObjectId()?
You should NOT convert the ObjectId in the database to a string, and then compare it to another string. If you'd do this, MongoDB cannot use the _id index and it'll have to scan all the documents, resulting in poor query performance. Show activity on this post. Don't.
An ObjectId is a 12-byte BSON type having the following structure − The first 4 bytes representing the seconds since the unix epoch. The next 3 bytes are the machine identifier. The next 2 bytes consists of process id. The last 3 bytes are a random counter value.
MongoDB is a NoSQL database that operates with collections and documents. Each document created on MongoDB has a unique object ID property.
MongoDB does not have out-of-the-box auto-increment functionality, like SQL databases. By default, it uses the 12-byte ObjectId for the _id field as the primary key to uniquely identify the documents.
Meteor uses the same method for object id's if you choose to use it:
Meteor.Collection.ObjectID()
is the same as MongoDB's ObjectID
Its just under the Meteor.Collection
name. It uses EJSON to hold object id's in ordinary JSON to the client end. Because basically there are 2 databases with meteor
Minimongo
This is a sort of cache of mongodb on the client end. The data is downloaded from the main mongodb on the server to this one when the browser loads up. When changes are made they are pushed up to the server.
Server MongoDB
This is the original mongodb from 10gen on the server
So because of these two databases Meteor needs to wrap mongodb functionality in Meteor.Collection
and let you use the same code on both the client and server.
By default meteor won't use Object IDs it'll use sort of random alphanumeric text. This is done so you can easily use ID's in your URL's and ID's in your html attributes.
If you do use new Meteor.Collection.ObjectID()
you will get an ObjectID
object that will use mongodb's specification of ObjectID on the server end. The timestamp value in the Object ID isn't held up but this shouldn't really do any harm.
Since 0.9.1 Meteor suggesting to use Mongo.ObjectID
instead of Meteor.Collection.ObjectID
. Basically both are same. Check history.md for more changes in naming conventions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With