Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What are Mongoose ODM's ObjectId?

What are Mongoose ODM's ObjectId?

Problem

Looking at the Mongoose ODM docs, it doesn't really say much about what are ObjectId's and how they can be used. I think its something like foreign keys in MongoDB?

If so, Embedded Documents seem to achieve the same purpose, when do I use which?

Problem courtesy of: Jiew Meng

Solution

It would be very worthwhile to read the MongoDB documentation or a quick MongoDB intro such as The Little MongoDB Book (it's free) for some background on MongoDB concepts.

To answer your question:

  • An Objectid is a unique 12-byte identifier which can be generated by MongoDB as the primary key (_id) for a collection. There is a specification for the ObjectID.

  • A DBRef (database reference) is an ObjectID referencing an object in another collection. A DBRef does require require another query to fetch the related object, and is a convention supported by the client drivers rather than MongoDB server. The Mongoid equivalent is called referenced relations.

  • Embedded documents are nested arrays or subdocuments within a document. In Mongoid these are embedded relations.

The approach to data modelling and schema design in MongoDB is very different from relational databases. There are (intentionally) no joins or foreign keys, but the document-oriented approach allows large amounts of related data to be stored and fetched in a single document. Depending on how you plan to query and update your data, embedding or linking may be a more suitable choice. The schema design page on the MongoDB wiki has some helpful tips to get you started.

Solution courtesy of: Stennie

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

What are Mongoose ODM's ObjectId?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×