MongoDB design pattern Strategy of reading notes

Source: Internet
Author: User
Tags node server

MongoDB's design pattern strategy
Reading notes

1. Query command Classification mode
The responsibilities in the replica set are detached to different nodes. The most basic first-class node may also occupy the top position, and it only needs to store the data that is needed to write and update. The query is then executed by the second type of node. This mode increases the write throughput of the primary node server because the amount of data that needs to be updated and inserted is reduced when writing to a set of objects, in addition to the fact that the two types of nodes benefit from less data to be updated and a memory working set that is optimized for its workload.
That is, read and write separation, master node write, the node to do the query.

2. Application-style Transaction mode
Using the queue
Sample Example
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}, {})?

It's not understood.

3.Bucketing mode
When the text contains an ever-growing array, use the bucketing mode, such as directives. The instruction line may expand to a reasonable value that exceeds the document size. This mode is handled programmatically and triggered by tolerance calculations.

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})?

It still doesn't understand.

4. Relationship Mode
Sometimes there are situations where the entire document cannot be inserted, such as when the human body is modeled, and we can use that pattern to build relationships.

Determines whether the data belongs to the document, that is, whether there is a relationship between the two.

If possible, insert the document, especially when confronted with useful, unique (proprietary) data.

The ID value is not referenced as much as possible.

Reverse-normalize the useful parts of the relationship. A good candidate does not often even never change values and is quite useful.

Focus on the update of anti-canonical data and relationship repair.

{
_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 had composed documents in MongoDB ',
Best_friend: {id:1, Name: ' Sam Smith '},
Hobbies: [{id:101, N: ' Music '}]
}

5. Materialized path mode
In the tree mode of a data model, the same object type is a sub-object of the object, in which case the materialized path model can be used for more efficient retrieval and querying. Examples are as follows:

{_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 tree mode by field path:

Db.collection.find (). Sort ({path:1})

Use the general expression of the path field to find the descendant set of programming:

Db.collection.find ({path:/,programming,/})

Query the descendant set of books in case books is top parent:

Db.collection.find ({path:/^,books,/})

Fuzzy matching is used here, and there is no understanding of why a more efficient search query can be counted.

The article reads from
www.iteye.com/news/31667

MongoDB design pattern Strategy of reading notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.