11. Cursors
$gt, $lt, $gte, $lte, $ne, $all, $in, $nin, $or,
$not, $mod, $size, $exists, $type, $elemMatch
> var c = db.test.find({x: 20}).skip(20).limit(10)> c.next()
> c.next()
...
query
first N results + cursor id
getMore w/ cursor id
next N results + cursor id or 0
...
Wednesday, 22 February 12
12. Creating Indexes
An index on _id is automatic.
For more use ensureIndex:
db.blogs.ensureIndex({author: 1})
1 = ascending
-1 = descending
Wednesday, 22 February 12
13. Compound Indexes
db.blogs.save({
author: "James",
ts: new Date()
...
});
db.blogs.ensureIndex({author: 1, ts: -1})
Wednesday, 22 February 12
14. Unique Indexes
db.blogs.save({
author: "James",
title: "My first blog"
...
});
db.blogs.ensureIndex({title: 1}, {unique: true})
Wednesday, 22 February 12
15. Indexing Embedded Documents
db.blogs.save({
title: "My First blog",
stats : { views: 0,
followers: 0 }
});
db.blogs.ensureIndex({"stats.followers": -1})
db.blogs.find({"stats.followers": {$gt: 500}})
Wednesday, 22 February 12
16. Indexing Embedded Arrays
db.blogs.save({
title: "My First blog",
comments: [
{author: "James", ts : new Date()} ]
});
db.blogs.ensureIndex({"comments.author": 1})
db.blogs.find({"comments.author": "James"})
Wednesday, 22 February 12
17. Multikeys
db.blogs.save({
title: "My Second blog",
tags: ["mongodb", "NoSQL"]
});
db.blogs.ensureIndex({tags: 1})
db.blogs.find({tags: "NoSQL"})
Wednesday, 22 February 12
18. Covered Indexes
• From 1.8.0
• Query resolved in index only
• Need to exclude _id from items projected
db.blogs.save({
author: "James",
title: "My first blog"
});
db.blogs.ensureIndex({author: 1})
db.blogs.find({author: "James"},
{author: 1, _id:0}))
Wednesday, 22 February 12
19. Sparse Indexes
• From 1.8.0
• Key value included if and only if the value is present
• Reduces size of index
• Limited to a single field
db.blogs.ensureIndex({loc: 1}, {sparse: true})
// loc key stored for Ben & Sarah only
db.blogs.save({author: "Jim"})
db.blogs.save({author: "Ben", loc: null})
db.blogs.save({author: "Sarah", loc: "CA"})
Wednesday, 22 February 12
20. Geospatial
• Geo hash stored in B-Tree
• First two values indexed
db.blogs.save({
loc: { long: 40.739037, lat: 40.739037 }
});
db.blogs.save({
loc: [40.739037, 40.739037]
});
db.blogs.ensureIndex({"loc": "2d"})
Wednesday, 22 February 12
22. Types of outage
• Planned
• Hardware upgrade
• O/S or file-system tuning
• Relocation of data to new file-system / storage
• Software upgrade
• Unplanned
• Hardware failure
• Data center failure
• Region outage
• Human error
• Application corruption
Wednesday, 22 February 12
23. Replica Set features
• A cluster of N servers
• Any (one) node can be primary
• Consensus election of primary
• Automatic failover
• Automatic recovery
• All writes to primary
• Reads can be to primary (default) or a secondary
Wednesday, 22 February 12
24. How MongoDB Replication works
Member
1 Member
3
Member
2
•Set is made up of 2 or more nodes
Wednesday, 22 February 12
25. How MongoDB Replication works
Member
1 Member
3
Member
2
PRIMARY
•Election establishes the PRIMARY
•Data replication from PRIMARY to SECONDARY
Wednesday, 22 February 12
26. How MongoDB Replication works
negotiate
new master
Member
1 Member
3
Member
2
DOWN
•PRIMARY may fail
•Automatic election of new PRIMARY if majority
exists
Wednesday, 22 February 12
27. How MongoDB Replication works
Member
1 Member
3
PRIMARY
Member
2
DOWN
•New PRIMARY elected
•Replication Set re-established
Wednesday, 22 February 12
28. How MongoDB Replication works
Member
1 Member
3
PRIMARY
Member 2
RECOVERING
•Automatic recovery
Wednesday, 22 February 12
29. How MongoDB Replication works
Member
1 Member
3
PRIMARY
Member
2
•Replication Set re-established
Wednesday, 22 February 12
36. MongoDB Sharding
• Automatic partitioning and management
• Range based
• Convert to sharded system with no downtime
• Fully consistent
Wednesday, 22 February 12
37. How MongoDB Sharding works
> db.posts.save( {age:40} )
-∞ +∞
-∞ 40 41 +∞
•Data in inserted
•Ranges are split into more “chunks”
Wednesday, 22 February 12
38. How MongoDB Sharding works
> db.posts.save( {age:40} )
> db.posts.save( {age:50} )
-∞ +∞
-∞ 40 41 +∞
41 50 51 +∞
•More Data in inserted
•Ranges are split into more“chunks”
Wednesday, 22 February 12