80 likes | 211 Views
what you need to know to get cranking. MongoDB Essentials II. Querying Subset Arrays Indexing Aggregating – map-reduce. What you will learn. Retrieve only what you need Structure remains same (not full projection) Syntax { a:” hello ”, b:” hola ”, c :”bonjour ”, _ id:1}
E N D
what you need to know to get cranking MongoDB Essentials II
Querying • Subset • Arrays • Indexing • Aggregating – map-reduce What you will learn
Retrieve only what you need • Structure remains same (not full projection) • Syntax {a:”hello”, b:”hola”, c:”bonjour”, _id:1} • db.people.find({},{b:0}) • db.people.find({},{a:1,c:1}) • db.people.find({},{a:1,_id:0}) Selecting
Look like an JS array (BSON actually) • Are stores as a document with index as key • Syntax • db.people.find({“pets.name”:”Felix”}) • db.people.find({},{pets[0]:1}) • db.people.find({},{pets:{$slice:1}}) • db.people.find({},{pets:{$slice:[-2,1]}}) Arrays
Indexes run deep. • Arrays can be indexed – (try that with RDBMS!) • Every collection has _id indexed. (“Clustered”) • Index structure is Btree • Covered index support (except array) • Syntax • db.people.ensureIndex({“pets.type”:1}) • db.people.ensureIndex({birthdate:-1}) • db.system.indexes.find() Indexed access
The only way to aggregate* • Challenge: lots of IO to read all records • Answer: distribute aggregation to nodes, then cull the results • Implementation: Map Reduce Map/Reduce
Map • function() • At some point: emit(key, value) • Value can be scalar or any object • Reduce • function(key, values) • return some computation over the values (typically). Key is implied. • Finalize • function(key, value) • Optional. Re-shape/filter - final step in computation. • Output • Screen, into new or existing collection (incremental) • Query • Limit work, support incremental m/r. Map/Reduce
http://openmymind.net/mongodb.pdf • http://mongodb.org/ • http://10gen.com/ • http://plusnconsulting.com/ • nuri@plusnconsulting.com • (818) 446.NUHA References