Showing posts with label data model. Show all posts
Showing posts with label data model. Show all posts

Friday, July 4, 2014

Study MongoDB data models

Today we are going to learn on MongoDB Data Models.

It is important to study data modal is because as a developer, you would want to leverage what MongoDB is excel at and aware what it is not suitable for.

You can basically store a few document and reference then using and id field.
But remember this need two round trip back and forth from the application servers
to the mongo database.

As such, in this scenario if it better to embed the document into a document.
user document
{
_id: <ObjectId1>, <-------------+
username : "jasonwee" |
} |
|
contact document |
{ |
_id: <ObjectId2>, |
user_id: <ObjectId1> <-------------+
phone: "012-3456789"
}

into
user document
{
_id: <ObjectId1>,
contact : {
phone: "012-3456789"
}
}

This modelling guarantee you atomicity of a document as mongodb write operations
are atomic at document level.

Indexes

Use indexes to improve performance for common queries. Build indexes on fields that appear often in queries and for all operations that return sorted results. MongoDB automatically creates a unique index on the _id field.

Each index requires at least 8KB of data space.

GridFS

GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16MB.

Model Relationships Between Documents

  • Model One-to-One Relationships with Embedded Documents
    Presents a data model that uses embedded documents to describe one-to-one relationships between connected data.

  • Model One-to-Many Relationships with Embedded Documents
    Presents a data model that uses embedded documents to describe one-to-many relationships between connected data.

  • Model One-to-Many Relationships with Document References
    Presents a data model that uses references to describe one-to-many relationships between documents.


Model Tree Structures

MongoDB allows various ways to use tree data structures to model large hierarchical or nested data relationships.

  • Model Tree Structures with Parent References 
    Presents a data model that organizes documents in a tree-like structure by storing references to “parent” nodes in “child” nodes.

  • Model Tree Structures with Child References
    Presents a data model that organizes documents in a tree-like structure by storing references to “child” nodes in “parent” nodes.

  • Model Tree Structures with an Array of Ancestors
    Presents a data model that organizes documents in a tree-like structure by storing references to “parent” nodes and an array that stores all ancestors.

  • Model Tree Structures with Materialized Paths
    Presents a data model that organizes documents in a tree-like structure by storing full relationship paths between documents. In addition to the tree node, each document stores the _id of the nodes ancestors or path as a string.

  • Model Tree Structures with Nested Sets
    Presents a data model that organizes documents in a tree-like structure using the Nested Sets pattern. This optimizes discovering subtrees at the expense of tree mutability.