DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Building Security into the Feature During the Design Phase
  • Build a Scalable E-commerce Platform: System Design Overview
  • A Step-by-Step Guide to Write a System Design Document

Trending

  • What Is Plagiarism? How to Avoid It and Cite Sources
  • Ensuring Configuration Consistency Across Global Data Centers
  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  • The Role of Retrieval Augmented Generation (RAG) in Development of AI-Infused Enterprise Applications
  1. DZone
  2. Data Engineering
  3. Databases
  4. MongoDB Design Patterns

MongoDB Design Patterns

MongoDB is a versatile tool, but it's not perfect for everything. For situations where it doesn't work, you can sometimes use design patterns to get around them.

By 
Darel Lasrado user avatar
Darel Lasrado
·
Jun. 15, 16 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
21.4K Views

Join the DZone community and get the full member experience.

Join For Free

MongoDB is a NoSQL document database. It's ideal for most use cases, and where it doesn't work, you can still overcome some of its limitations by using the following design patterns.

This article provides a solution for some of the limitations mentioned in my other article MongoDB : The Good, The Bad, and the Ugly.

1. Query Command Segregation Pattern

Segregate responsibility to different nodes in the replica set. The primary node may have priority 1 and may keep only indexes required for insert and update. The queries can be executed on secondaries. This pattern will increase write throughput on the “priority 1” servers because fewer indexes need to be updated and inserted on writing to a collection and secondaries benefit from having to update fewer indexes and having a working set of memory that is optimized for their workload

2. Application-Level Transactions Pattern

MongoDB does not support transactions and locking documents internally. However, with application logic, a queue may be maintained.

db.queue.insert( { _id : 123,
    message : { },
    locked : false,
    tlocked : ISODate(),
    try : 0 });
var timerange = date.Now() - TIMECONSTANT;
var doc = db.queue.findAndModify( { $or : [ { locked : false }, { locked : true, tlocked : {
$lt : timerange } } ], { $set : { locked : true, tlocked : date.Now(), $inc : { try : 1 } } }
);
//do some processing
db.queue.update( { _id : 123, try : doc.try }, { } );

3. Bucketing Pattern

When the document has an array that grows over the period of time, use the bucketing pattern. Example: Orders. The order lines can grow or may be larger than the desired size of the document. The pattern is handled programmatically and is triggered using a tolerance count.

var TOLERANCE = 100;
    for( recipient in msg.to) {
        db.inbox.update( {
            owner: msg.to[recipient], count: { $lt : TOLERANCE }, time : { $lt : Date.now() } },
{ $setOnInsert : { owner: msg.to[recipient], time : Date.now() },
{ $push: { "messages": msg }, $inc : { count : 1 } },
{ upsert: true } );


4. Relationship Pattern

Sometimes it's not feasible to embed entire document — for example, when we are modeling people. Use this pattern to build relationships.

  1. Determine if data "belongs to" a document — is there a relation?

  2. Embed when possible, especially if the data is useful and exclusive ("belongs in").

  3. Always reference _id values at minimum.

  4. Denormalize the useful parts of the relationship. Good candidates do not change value often, or ever, and are useful.

  5. Be mindful of updates to denormalized data and repair relationships

{
    _id : 1,
    name : ‘Sam Smith’,
    bio : ‘Sam Smith is a nice guy’,
    best_friend : { id : 2, name : ‘Mary Reynolds’ },
    hobbies : [ { id : 100, n :’Computers’ }, { id : 101, n : ‘Music’ } ]
}
{
    _id : 2,
    name : ‘Mary Reynolds’
    bio : ‘Mary has composed documents in MongoDB’,
    best_friend : { id : 1, name : ‘Sam Smith’ },
    hobbies : [ { id : 101, n : ‘Music’ } ]
}

5. Materialized Path Pattern


If you have a tree pattern of data model where the same object type is a child of an object, you can use the materialized path pattern for more efficient search/queries. A sample is given below.



{ _id: "Books", path: null }
{ _id: "Programming", path: ",Books," }
{ _id: "Databases", path: ",Books,Programming," }
{ _id: "Languages", path: ",Books,Programming," }
{ _id: "MongoDB", path: ",Books,Programming,Databases," }
{ _id: "dbm", path: ",Books,Programming,Databases," } 

Query to retrieve the whole tree, sorting by the field path:
    db.collection.find().sort( { path: 1 } )
Use regular expressions on the path field to find the descendants of Programming:
    db.collection.find( { path: /,Programming,/ } )
Retrieve the descendants of Books where the Books is the top parent:
    db.collection.find( { path: /^,Books,/ } )

Related Refcard:

MongoDB

MongoDB Design

Published at DZone with permission of Darel Lasrado. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Building Security into the Feature During the Design Phase
  • Build a Scalable E-commerce Platform: System Design Overview
  • A Step-by-Step Guide to Write a System Design Document

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!