Three

## The design pattern of MongoDB

readFour thousand two hundred and eighty-one

Original text:Design Patterns MongoDB
translator：Zhong Peiyi, concerned about the field of database, error correction, to seek reports or contributions please mail: zhongpy@PROG3.COM.

MongoDB is a universal tool, but it is not perfect. In view of certain MongoDB does not apply the occasion, sometimes may choose the design pattern to deal with.

Mongodb is a NoSQL document database, and in most cases is a relatively ideal choice, even in its do not apply to the case can still rely on the following list of these design patterns to overcome its limitations.

This article will be for my another article"MongoDB's good and bad" (MongoDB: Good The, Bad and, the Ugly The)Some limitations are mentioned in providing a corresponding solution.

1 query command separation mode

Responsibilities are separated into different nodes in the replica set. The most basic first class of nodes may also occupy the first place, it only needs to store the data required to write and update. While the query is submitted by the second types of nodes to perform. This model will improve the primary server nodes write throughput, because when writing to a group of objects, it is necessary to update and insert data quantity is decreased, in addition, two kinds of nodes also benefits to less to be updated data and its for the workload and optimize the working set of memory.

2 application level transaction mode

MongoDB does not support transaction and file internal lock. However, according to the application logic, should keep the queue usage.

Db.queue.insert ({_id:One hundred and twenty-three,
Message: {},
Locked:False,
Tlocked: ISODate (),
Try:Zero});
VarTimerange = date.Now (TIMECONSTANT) -
VarDoc = db.queue.findAndModify ({$or{locked:False{locked} {:TrueTlocked: {$ltTimerange}] {$set{locked:True, tlocked: date.Now (),$inc: {Try:One}}}
)
Some processing //do
Db.queue.update ({_id:One hundred and twenty-three,Try: doc.Try}, {})

3 Bucketing model

When the text contains an ever growing array, the Bucketing mode is used, such as the instruction. The command line may be extended to a reasonable value of the document size. The model is processed by programming and triggered by tolerance calculation.

TOLERANCE var =One hundred;
For(recipientInMsg.To{)
Db.inbox.update ({
Msg. owner:To[recipient], count: {$lt: TOLERANCE},Time{$lt:Date.Now()},
{$setOnInsert: {msg. owner:To[recipient],Time:Date.Now(}), {$push: {"Messages"MSG}, \$inc: {count} {:One}},
{upsert:True})

4 Relationship Model

Sometimes, there are situations in which the entire document can not be inserted, such as when the human body is modeled, and we can use this model to build relationships.

1. Determines whether the data is the document, that is, whether there is a relationship between the two.
2. If possible, especially in the face of useful and unique (exclusive) data, insert the document.
3. As far as possible not to refer to the ID value.
4. The useful part of the relationship in the anti standardization process. Good candidates do not often even change the value, and quite useful.
5. Concerned about the update and relationship repair of the anti - Specification data.
{
_id:One,
Name'Smith Sam',
Bio: 'Smith SamIsNice guy a',
Best_friend: {ID:Two,Name'Reynolds Mary'},
Hobbies: [{ID:One hundred, Computers: 'n'}, {ID:One hundred and oneN: 'Music'}
}
{
_id:Two,
Name'Reynolds Mary'
Bio: 'has composed documents MaryInMongoDB',
Best_friend: {ID:One,Name'Smith Sam'},
Hobbies: [{ID:One hundred and oneN: 'Music'}
}

5 physical and chemical path mode

In a data model, the same object type is the child object of the object. In this case, we can use the materialized path model to obtain more efficient search and query. Examples are as follows:

{_id: "Books", null path:}
{_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 tree pattern by field path:

DB.collection.find().sort({Path: One })

Use the regular expression of the path field to find out the set of Programming offspring:

DB.collection.find({Path:/, Programming, /})

In the case of Books is parent Books query top offspring set:

DB.collection.find({Path:Books / / ^.})