The document discusses MongoDB database architecture and examples of data structures. It provides examples of adding fields to objects and storing comment replies. It also summarizes MongoDB monitoring tools and concepts like sharding.
5. It’s very easy to add a new nodes to a replica set
Start the process on the new server:
mkdir /data/db
mongod --port xxx --dbpath /data/db --replSet setname
Add the node on the Primary DB
rs.add(“[hostname]:port”);
6.
7. Most of the operations we perform on the analytics db are atomic in-place updates :
analytics.heatmap.update(
{
homepage_id:"4ecfb4eefcda248b89000003",
uri : "/ron-paul-slams-trump-joins-huntsman-in-sitting-out-circus-like-debate-2011-12"
},
{
$inc : {views : 1},
$set : {updated_at : {"sec":1329587057,"usec":175000}} }},
true // upsert
)
12. Example: adding a new field to an object
In the console
> var myObj = {name: 'julie', role: "developer"}
//creates the foo collection and inserts an object
> db.foo.save(myObj)
> db.foo.find()
{ "_id" : ObjectId("4f418251f4dd8d3d88a776ea"), "name" : "julie", "role" : "developer" }
// Add a new field
var julie = db.foo.findOne()
{
"_id" : ObjectId("4f418251f4dd8d3d88a776ea"),
"name" : "julie",
"role" : "developer"
}
> julie.last_name = "sommerville'
> db.foo.save(julie)
> db.foo.find().pretty()
{
"_id" : ObjectId("4f418251f4dd8d3d88a776ea"),
"name" : "julie",
"role" : "developer",
"last_name" : "sommerville"
}
(pretty just makes it pretty)
13.
14. Some Examples from our DB
Post Object: contains comments, many posts will have way more than a couple of comments
db.blog.posts.findOne({comments : {$exists:true}, comment_count : {$gte: 1}, ts : {$gte: start}})
{
"_id" : ObjectId("4d4798c949e2aef0100d0000"),
"attached_images" : [ ],
"author" : "Ed Dolan",
"author_id" : ObjectId("4d121ca0ccd1d5d337000000"),
"autotweet" : false,
"badges" : [ ],
"browser_title" : null,
"categories" : [ ],
"cls" : "entry",
"comment_count" : 1,
"comment_thread_count" : 1,
"comments" : [
{
"cid" : ObjectId("4d482923cadcbb310a080000"),
"ts" : ISODate("2011-02-01T15:39:15.541Z"),
"ip" : "113.53.150.3",
"author" : "kenhow",
"email" : "info@howincorporated.com",
"url" : "http://howincorporated.com",
"user_id" : ObjectId("4d32fceaccd1d53d7b010000"),
"text" : "In other words: nothing to worry about"
}
],
15. We also store the Replies to comments :
{
"author" : "LS",
"cid" : ObjectId("4eda72376bb3f72279000009"),
"email" : "lawrenceshapiro@gmail.com",
"flags" : [ ],
"ip" : "74.68.112.217",
"rate_down" : 0,
"rate_up" : 6,
"raters" : {
"$ref" : "blog.comments.raters",
"$id" : ObjectId("4eda72376bb3f72279000008")
},
"replies" : [
{
"cid" : ObjectId("4eda800b69beddf55900004f"),
"author" : "Skepticus Maximus",
"email" : "henry1807@yahoo.com",
"flags" : [ ],
"ip" : "66.159.194.20",
"rate_down" : 0,
"rate_up" : 1,
"raters" : {
"$ref" : "blog.comments.raters",
"$id" : ObjectId("4eda800b69beddf55900004e")
},
"reply" : ObjectId("4eda72376bb3f72279000009"),
"reply_author" : "LS",
"text" : "No, it's perfectly natural - nature's way to make sure the species continues",
"ts" : ISODate("2011-12-03T20:01:15.930Z"),
"url" : "",
"session_id" : ObjectId("4eda76e3eab8ea9f4d00003d")
},
* Fast retrieval - no joins
16.
17.
18.
19. MMS (MongoDB Monitoring Service)
* Really nice stats with the ability to “zoom” in (minute, 5min, hour etc.).
* If the data base is acting up you can go in and see where the problem may lie
-- Num Collections
-- Data Size in the DB and in storage
-- Avg. doc size
-- Index Sizes
-- Replica Set Stats
-- page faults
-- the lock queue (reads and writes)
20. total number of contiguously allocated chunks of
datafile space for the database