MongoDB Common Data Modeling method: 1) Document relationship modeling, 2) tree structure modeling
Model:
|--1, document relationship modeling
| |--1) Embedded document model
| |--2) Embedded document model
|--2, tree structure modeling
|--1) Parent document reference
|--2) Sub-document reference
|--3) Ancestor array
|--4) Materialized path
|--5) nested sets
The following records the common modeling and the specific relevant sample code for each model: First, document relationship modeling:
1) Embedded document model
{
_id: "Joe",
Name: "Joe Bookreader",
address: {
Street: "123 Fake Street", City
: "Faketon",
State: "MA",
Zip: "12345"
}
}
2) document Reference Model
{
_id: "Joe",
Name: "Joe Bookreader"
}
{
patron_id: "Joe",
Street: "123 Fake Street", City
: "Faketon", State
: "MA",
Zip: "12345"
}
second, tree structure modeling
Tree-shaped business structure diagram:
(note: Quoted from MongoDB Chinese official website map)
1) Parent Document reference
Db.categories.insert ({_id: "MongoDB", Parent: "Databases"})
Db.categories.insert ({_id: "dbm", Parent: "Databases })
Db.categories.insert ({_id: "Databases", Parent: "Programming"})
Db.categories.insert ({_id: "Languages" , Parent: "Programming"})
Db.categories.insert ({_id: "Programming", "Parent:" Books "}
) Db.categories.insert ( {_id: "Books", Parent:null})
2) Sub-document reference
Db.categories.insert ({_id: "MongoDB", Children: []})
Db.categories.insert ({_id: "dbm", Children: []})
DB.C Ategories.insert ({_id: "Databases", Children: ["MongoDB", "dbm"]})
Db.categories.insert ({_id: "Languages", CHI Ldren: []})
Db.categories.insert ({_id: "Programming", Children: ["Databases", "Languages"]})
Db.categorie S.insert ({_id: "Books", Children: ["Programming"]})
3) Ancestor Array
Db.categories.insert ({_id: "MongoDB", Ancestors: ["Books", "Programming", "Databases"], Parent: "Databases"})
db . Categories.insert ({_id: "dbm", Ancestors: ["Books", "Programming", "Databases"], Parent: "Databases"})
Db.categ Ories.insert ({_id: "Databases", Ancestors: ["Books", "programming"], Parent: "Programming"})
Db.categories.inser T ({_id: "Languages", Ancestors: ["Books", "programming"], Parent: "Programming"})
Db.categories.insert ({_id: " Programming ", Ancestors: [" Books "], Parent:" Books "})
Db.categories.insert ({_id:" Books ", Ancestors: [], Parent : null})
4) Materialized path
Db.categories.insert ({_id: "Books", Path:null})
Db.categories.insert ({_id: "Programming", Path: ", Books,"})
db.categories.insert ({_id: "Databases", Path: ", Books,programming,"})
Db.categories.insert ({_id: "Languages ", Path:", Books,programming, "})
Db.categories.insert ({_id:" MongoDB ", Path:", Books,programming,databases, "})" C4/>db.categories.insert ({_id: "dbm", Path: ", Books,programming,databases,"})
5) Nested Sets
Db.categories.insert ({_id: "Books", parent:0, Left:1, right:12})
Db.categories.insert ({_id: "Programming", PA Rent: "Books", Left:2, right:11})
Db.categories.insert ({_id: "Languages", Parent: "Programming", Left:3, right: 4})
Db.categories.insert ({_id: "Databases", "Parent:" Programming ", Left:5, right:10})
Db.categories.inser T ({_id: "MongoDB", Parent: "Databases", Left:6, Right:7})
Db.categories.insert ({_id: "dbm", Parent: "Databases ", Left:8, Right:9})