Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor.Collection.ObjectID() vs MongoDB ObjectId()

Tags:

mongodb

meteor

Why does Meteor.js use it's own algorithms for IDs?

Why doesn't it use MongoDB's ObjectId()?

like image 490
hoodoy Avatar asked Mar 10 '13 02:03

hoodoy


People also ask

Should I use MongoDB 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.

What is ObjectId in MongoDB?

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.

Is MongoDB ObjectId unique across collections?

MongoDB is a NoSQL database that operates with collections and documents. Each document created on MongoDB has a unique object ID property.

Is MongoDB ObjectId incremental?

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.


2 Answers

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.

like image 132
Tarang Avatar answered Oct 06 '22 11:10

Tarang


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.

like image 43
Rajanand02 Avatar answered Oct 06 '22 11:10

Rajanand02