Beruflich Dokumente
Kultur Dokumente
Topics
Introduction Basic Data Modeling Evolving a schema Common patterns Single table inheritance One-to-Many & Many-to-Many Trees Queues
http://www.ickr.com/photos/42304632@N00/493639870/
* source : wikipedia
Data and Logic combined Separation of concerns Data modeled independent of logic Logic freed from concerns of data design
Terminology
RDBMS Table Row(s) Index Join Partition Partition
Key MongoDB Collection JSON
Document Index Embedding
&
Linking Shard Shard
Key
Design Session
Design documents that simply map to your application
>
post
=
{author:
"Herg",
date:
ISODate("2011-09-18T09:56:06.298Z"),
text:
"Destination
Moon",
tags:
["comic",
"adventure"]} >
db.posts.save(post)
Query operators
Conditional operators: $ne, $in, $nin, $mod, $all, $size, $exists, $type, .. $lt, $lte, $gt, $gte, $ne,
Query operators
Conditional operators: $ne, $in, $nin, $mod, $all, $size, $exists, $type, .. $lt, $lte, $gt, $gte, $ne,
Query operators
Conditional operators: $ne, $in, $nin, $mod, $all, $size, $exists, $type, .. $lt, $lte, $gt, $gte, $ne,
> db.posts.update( {text: Destination Moon }, { $push: {comments: new_comment}, $inc: {comments_count: 1}})
Group
Equivalent to a Group By in SQL Specic the attributes to group the data Process the results in a Reduce function
{ }, { }
Common Patterns
Inheritance
circle 3.14
square 4
rect
10
// find shapes where radius > 0 > db.shapes.find({radius: {$gt: 0}}) // create index > db.shapes.ensureIndex({radius: 1})
One to Many
One to Many relationships can specify degree of association between objects containment life-cycle
One to Many
- Embedded Array / Array Keys - slice operator to return subset of array - some queries harder e.g nd latest comments across all documents
blogs:
{
author
:
"Herg",
date
:
ISODate("2011-09-18T09:56:06.298Z"),
comments
:
[
{
author
:
"Kyle",
date
:
ISODate("2011-09-19T09:56:06.298Z"),
text
:
"great
book"
}
]}
One to Many
- Embedded tree - Single document - Natural - Hard to query
blogs:
{
author
:
"Herg",
date
:
ISODate("2011-09-18T09:56:06.298Z"),
comments
:
[
{
author
:
"Kyle",
date
:
ISODate("2011-09-19T09:56:06.298Z"),
text
:
"great
book",
replies:
[
{
author
:
James,
...}
]
}
]}
One to Many
- Normalized (2 collections) - most exible - more queries
blogs:
{
author
:
"Herg",
date
:
ISODate("2011-09-18T09:56:06.298Z"),
comments
:
[
{comment
:
ObjectId(1)}
]} comments
:
{
_id
:
1,
author
:
"James",
date
:
ISODate("2011-09-19T09:56:06.298Z")}
Many - Many
Example:
- Product can be in many categories - Category can have many products
Many - Many
products:
{
_id:
ObjectId("10"),
name:
"Destination
Moon",
category_ids:
[
ObjectId("20"),
ObjectId("30]}
Many - Many
products:
{
_id:
ObjectId("10"),
name:
"Destination
Moon",
category_ids:
[
ObjectId("20"),
ObjectId("30]}
categories:
{
_id:
ObjectId("20"),
name:
"adventure",
product_ids:
[
ObjectId("10"),
ObjectId("11"),
ObjectId("12"]}
Many - Many
products:
{
_id:
ObjectId("10"),
name:
"Destination
Moon",
category_ids:
[
ObjectId("20"),
ObjectId("30]}
categories:
{
_id:
ObjectId("20"),
name:
"adventure",
product_ids:
[
ObjectId("10"),
ObjectId("11"),
ObjectId("12"]}
Alternative
products:
{
_id:
ObjectId("10"),
name:
"Destination
Moon",
category_ids:
[
ObjectId("20"),
ObjectId("30]}
categories:
{
_id:
ObjectId("20"),
name:
"adventure"}
Alternative
products:
{
_id:
ObjectId("10"),
name:
"Destination
Moon",
category_ids:
[
ObjectId("20"),
ObjectId("30]}
categories:
{
_id:
ObjectId("20"),
name:
"adventure"} //
All
products
for
a
given
category >
db.products.find({category_ids:
ObjectId("20")})
Alternative
products:
{
_id:
ObjectId("10"),
name:
"Destination
Moon",
category_ids:
[
ObjectId("20"),
ObjectId("30]}
categories:
{
_id:
ObjectId("20"),
name:
"adventure"} //
All
products
for
a
given
category >
db.products.find({category_ids:
ObjectId("20")})
//
All
categories
for
a
given
product product
=
db.products.find(_id
:
some_id) >
db.categories.find({_id
:
{$in
:
product.category_ids}})
Trees
Full Tree in Document
{
comments:
[
{
author:
Kyle,
text:
...,
replies:
[
{author:
James,
text:
...,
replies:
[]}
]}
] }
Pros: Single Document, Performance, Intuitive Cons: Hard to search, Partial Results, 16MB limit
Trees
Parent Links - Each node is stored as a document - Contains the id of the parent Child Links - Each node contains the ids of the children - Can support graphs (multiple parents / child)
Array of Ancestors
- Store all Ancestors of a node
{
_id:
"a"
}
{
_id:
"b",
ancestors:
[
"a"
],
parent:
"a"
}
{
_id:
"c",
ancestors:
[
"a",
"b"
],
parent:
"b"
}
{
_id:
"d",
ancestors:
[
"a",
"b"
],
parent:
"b"
}
{
_id:
"e",
ancestors:
[
"a"
],
parent:
"a"
}
{
_id:
"f",
ancestors:
[
"a",
"e"
],
parent:
"e"
}
Array of Ancestors
- Store all Ancestors of a node
{
_id:
"a"
}
{
_id:
"b",
ancestors:
[
"a"
],
parent:
"a"
}
{
_id:
"c",
ancestors:
[
"a",
"b"
],
parent:
"b"
}
{
_id:
"d",
ancestors:
[
"a",
"b"
],
parent:
"b"
}
{
_id:
"e",
ancestors:
[
"a"
],
parent:
"a"
}
{
_id:
"f",
ancestors:
[
"a",
"e"
],
parent:
"e"
} //find
all
descendants
of
b: >
db.tree2.find({ancestors:
b}) //find
all
direct
descendants
of
b: >
db.tree2.find({parent:
b})
Array of Ancestors
- Store all Ancestors of a node
{
_id:
"a"
}
{
_id:
"b",
ancestors:
[
"a"
],
parent:
"a"
}
{
_id:
"c",
ancestors:
[
"a",
"b"
],
parent:
"b"
}
{
_id:
"d",
ancestors:
[
"a",
"b"
],
parent:
"b"
}
{
_id:
"e",
ancestors:
[
"a"
],
parent:
"a"
}
{
_id:
"f",
ancestors:
[
"a",
"e"
],
parent:
"e"
} //find
all
descendants
of
b: >
db.tree2.find({ancestors:
b}) //find
all
direct
descendants
of
b: >
db.tree2.find({parent:
b}) //find
all
ancestors
of
f: >
ancestors
=
db.tree2.findOne({_id:f}).ancestors >
db.tree2.find({_id:
{
$in
:
ancestors})
Trees as Paths
Store hierarchy as a path expression - Separate each node by a delimiter, e.g. / - Use text search for nd parts of a tree
{
comments:
[
{
author:
Kyle,
text:
initial
post,
path:
/
},
{
author:
Jim,
text:
jims
comment,
path:
/jim
},
{
author:
Kyle,
text:
Kyles
reply
to
Jim,
path
:
/jim/kyle}
]
} //
Find
the
conversations
Jim
was
part
of
>
db.posts.find({path:
/^jim/i})
Queue
Need to maintain order and state Ensure that updates to the queue are atomic
{
inprogress:
false,
priority:
1,
...
}
Queue
Need to maintain order and state Ensure that updates to the queue are atomic
{
inprogress:
false,
priority:
1,
...
} //
find
highest
priority
job
and
mark
as
in-progress job
=
db.jobs.findAndModify({
query:
{inprogress:
false},
sort:
{priority:
-1},
update:
{$set:
{inprogress:
true,
started:
new
Date()}},
new:
true})
Summary
Schema design is different in MongoDB Basic data design principals stay the same Focus on how the apps manipulates data Rapidly evolve schema to meet your requirements Enjoy your new freedom, use it wisely :-)
download at mongodb.org
alvin@10gen.com
http://bit.ly/mongo1
http://linkd.in/joinmongo