MongoDB data Model (ii)

Source: Internet
Author: User
Tags createindex findone

Original address

Pick up an article

Model tree structure of parent Reference Model tree structure

This data model describes a tree structure that stores references to parent nodes in child nodes.

Mode

The parent reference mode stores each tree node in the document, and in addition to the tree node, the document stores the ID of the parent node.

Consider the hierarchical relationships of the following directories.

The following are examples of applications

Db.categories.insert ({_id: "MongoDB", Parent: "Databases""dbm", Parent: "Databases""Databases", Parent: "Programming""Languages", Parent: "Programming""Programming", Parent: "Books"  NULL } )

Querying the parent node of a node becomes fast and intuitive

Db.categories.findOne ({_id: "MongoDB"}). Parent

You can also create indexes on the parent field to improve query speed

Db.categories.createIndex ({parent:1})

Querying its immediate child nodes through the parent field

Db.categories.find ({parent: "Databases"})

This parent reference pattern is a simple way to implement tree storage, with the disadvantage that multiple queries are required to get a subtree.

Model tree structure for child references

This data model describes the tree structure, storing references to the child nodes in the parent node.

Mode

The sub-reference mode stores each tree node in a document, except for the tree node, which also stores an array containing its child node IDs.

Consider the same directory-level relationship as in the previous diagram, with its sub-reference pattern implemented as follows

Db.categories.insert ({_id: "MongoDB""dbm""Databases", Children: ["MongoDB", "dbm"" Languages "" Programming ", Children: [" Databases "," Languages "" Books ", Children: [" Programming "]})

Query gets the immediate child node of a node becomes fast and intuitive

Db.categories.findOne ({_id: "Databases"}). Children

Create an index on the Children field for quick query

Db.categories.createIndex ({children:1})

Querying its parent node and sibling node information through children information

Db.categories.find ({children: "MongoDB"})

Sub-reference mode provides a suitable scheme for tree storage as long as the subtree operation is not required (a single query cannot guarantee the acquisition of all child nodes of a node). For storing images, one of the nodes may have multiple parent nodes, which is also a good scheme.

Model tree structure of ancestor arrays

This data model describes the tree structure, uses a reference to the parent node, and an array to store all the ancestor nodes.

Mode

In addition to storing each tree node in the document, an array is stored, and the other IDs or paths that expose all ancestor nodes (the ID of the ancestor nodes are sorted in order to make up the path).

Consider the same directory-level relationship as in the diagram above

In the following actual instance, the document has the parent field in addition to the Ancestors field, and the parent field stores a reference to the immediate parent node.

Db.categories.insert ({_id: "MongoDB", Ancestors: ["Books", "Programming", "Databases"], Parent: "Databases"" DBM ", Ancestors: [" Books "," Programming "," Databases "], Parent:" Databases "" Databases ", Ancestors: [" Books "," Programming "], Parent:" Programming "" Languages ", Ancestors: [" Books "," programming "], Parent:" Programming " c3>"Programming", Ancestors: ["Books"], Parent: "Books"null })

Query gets the ancestor node or path of a node becomes fast and intuitive

Db.categories.findOne ({_id: "MongoDB"}). Ancestors

Create an index on the Ancestors field to increase query speed

Db.categories.createIndex ({ancestors:1})

Query the Ancestors field to find all its descendant nodes

Db.categories.find ({ancestors: "Programming"})

The ancestor array pattern provides an efficient way to query the descendant nodes of a node and the ancestors of a node, so this pattern is a good choice when it is necessary to operate on a subtree node.

The ancestor array pattern is slightly slower than the materialized path (materialized Paths) pattern, but is more intuitive to use.

Model tree structure for materialized paths (materialized Paths)

This data model describes the tree structure and stores the whole relationship path between documents.

Mode

In the materialized path, the document stores the ID or path of the ancestor of the node, in addition to the storage tree node. Although the materialized path pattern requires additional steps to handle strings and regular expressions, this pattern also provides the flexibility to handle paths, such as finding nodes through partial paths.

Consider the same directory-level relationship as in the diagram above

Materialized path pattern, store path to Path field, path using comma as delimiter

Null"Programming", Path: ", Books,""Databases", Path: ", books,programming,"" Languages ", Path:", books,programming, "" MongoDB ", Path:", Books,programming,databases, "" dbm ", Path: ", Books,programming,databases,"})

Querying all nodes through the path field

Db.categories.find (). Sort ({path:1})  //  Ascending by Path

Use regular expressions on the path field to find descendants of programming

// path contains ", Programming,"

Find descendants of Books because Books is the highest node, so the regular expression begins with ", Books,"

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

To create an index on the path field

Db.categories.createIndex ({path:1})

Depending on the query, this index may improve performance:

    • For queries on the subtree of the root node Books (for example,/^,books,/or/^,books,programming,/), the index of the path field can significantly improve query performance.
    • For queries on subtrees that do not provide a path to the root node (for example,/,databases,/), or similar subtree queries, the node is in the middle of the indexed string, at which point the query must scan all indexes. For these queries, indexes can improve query performance if the index is much smaller than the entire collection.
Model tree structure of embedded set (Nested sets)

This data model describes the tree structure and optimizes the performance of the lookup subtree, but it also leads to an increase in the variability of the tree.

Mode

The inline set mode makes a round trip to the tree and identifies each node in the tree as a stop point. The application accesses each node two times, first for the Traverse, and the second for the return traversal. In addition to the storage tree node, the document in the inline set mode stores the ID of its parent node, stores it to go to the dwell point to the left field, and stores the return dwell point to the right field.

Consider the hierarchical relationship of the following directory

The following code shows the implementation of the inline set

Db.categories.insert ({_id: "Books", parent:0, Left:1, Right:12"Programming", Parent: "Books", Left:2, right : "Languages", Parent: "Programming", Left:3, Right:4"Databases", Parent: "Programming", Left:5, RI Ght:10"MongoDB", Parent: "Databases", Left:6, Right:7"dbm", Parent: "Databases", Left:8, Right:9} )

Query gets the descendants of a node

var databasecategory = Db.categories.findOne ({_id: "Databases" });d B.categories.find ({left: {$gt: Datab Asecategory.left}, right: {$lt: Databasecategory.right}});

Inline set mode provides a fast and efficient way to find subtrees, but it is cumbersome to modify the tree structure. Therefore, this model is suitable for static trees that do not change structure.

Model data for model-specific atomic manipulation of program contexts

Write operations in MongoDB, such as Db.collection.update (), Db.collection.findAndModify (), Db.collection.remove () are atomic at a single document level. For those fields that need to be updated together, embedding the fields in the document ensures that updates to these fields are atomic.

Consider, for example, how to determine the atomic nature of the two operations when a maintenance book's information is required, including the number of books that can be checked out and the current checkout information.

The available books and checked out information must be synchronized. In this way, the available field and the checkout field are embedded in the same document to ensure that the updated document is atomic.

{    123456789,    "mongodb:the Definitive Guide",    "Kristina chodorow", "Mike Dirolf " ],    published_date:isodate (" 2010-09-24 ")    , 216,    " 中文版 ",     "oreilly",    available3,    checkout"Joe", Date: Isodate ("2012-10-15")}]}

Then, depending on the new checkout information, you can update with Db.collection.update (), and the update to the available field and checkout field is atomic.

db.books.update (   123456789, available: {$gt: 0 }},  //Query condition   {     -1 }, 
    
     //available field minus 1     
    new  Date ()}} //Checkout field array add an element   })

The above operation returns a Writeresult () object that contains information about the operation

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

The Nmatched field indicates that 1 documents match this update condition, and nmodified indicates that the operation updated a document.

Model data that supports keyword queries

Unlike text search or full-text search, keyword search does not provide stemming or other text processing features.

This pattern describes the methods that support keyword search to support the program search function, which uses the keywords stored in an array in the same document, and this array acts as a text field for the document. When combined with a multi-key index, this mode can support the program's keyword search operations.

Mode

To add structures to the document to support keyword-based queries, first create an array field in the document and add the keyword as a string to the array. You can then create a multi-key index on this array and create a query to select values from this array.

Instance:

Given a collection of book volumes and want to provide a search for an opportunity topic. For each volume, we increase the topics array and add the keyword to the array as much as possible for the volume. The Moby-dick volume can be represented by the following document.

{title: "Moby-dick" ,  "Herman Melville" ,  1851 ,  0451526996 ,  " Whaling "," Allegory "," Revenge "," American " ,    " novel "," Nautical "," Voyage "," Cape Cod " ]}

Then create a multi-key index on the topics array

Db.volumes.createIndex ({topics:1})

A multi-key index creates an index entry for each keyword in the topics array. For example, there is an index entry for "whaling" in the index and an index entry for "allegory".

The keyword-based query is then shown for example below

Db.volumes.findOne ({topics: "Voyage"}, {title:1})

Tip: When the number of elements in an array is large, if there are hundreds of or thousands of keywords, the insert operation can cause the index to be expensive.

Restrictions on keyword indexing

With specific data models and multi-key indexes, MongoDB can support keyword searches. However, these keyword indexes are less than or impossible to compare with full-text search in the following ways:

    • Stem extraction. Keyword queries cannot parse keywords as root or related words.
    • Synonymous. Keyword search features must provide synonymous support or related queries at the application level.
    • Sort. The keyword query does not provide a way to determine the weight of the results.
    • An asynchronous index. MongoDB synchronously creates an index, which means that the index used for the keyword is always current and can be manipulated in real time. However, an index created asynchronously is more efficient under a certain content and workload.

MongoDB data Model (ii)

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.