mongodb: tips, trick and hacks

17
Tips, Trick and Hacks Get’n it done!

Upload: scott-hernandez

Post on 14-Dec-2014

12.572 views

Category:

Technology


5 download

DESCRIPTION

 

TRANSCRIPT

  • 1. Tips, Trick and Hacks
    Getn it done!

2. Basic Expectations
No transactions
Fast-N-Loose by default (no safe/w/GLE)
Indexing order matters; query optimizer helps
One write thread
One JS thread (M/R, $where, group)
Many query threads (non-js)
Memory Mapped Data Files
BSON everywhere
3. Indexes
Index on A, B, C works for [A], [A,B], [A,B,C]
Query Optimizer figures out order
Hint when you know
Missing values are indexed as null value
Just like real null values
Unique indexes include missing/nulls
Sort works on last field
4. Shell: Functions
Leave off the () to see the function:
> db.coll.find
function (query, fields, limit, skip) {
return new DBQuery(this._mongo, this._db, this, this._fullName, this._massageObject(query), fields, limit, skip); }
5. Connections
Order of ops is only preserved in the same connection (socket/port)
Connection pools are good, but could be a problem (getLastError/ordered ops)
Server only executes one (concurrent) operation per connection
6. getLastError()
More like getLastOpStatus()
Returns useful data on update/findAndModify/insert/remove ops
> db.t.update({x:1}, {$inc:{y:1}}, true, true)
> db.getLastErrorObj()
{"err" : null,
"updatedExisting" : false,
"upserted" : ObjectId("4c49f54cab620000000071b7"),
"n" : 1,
"ok" : 1 }
7. getLastErrorParams
Params
w: number of replicas to write to
wtimeout: time to wait for acknowledgements
fsync: flush to disk
{getlasterror : 1, w : 40, wtimeout: 3000}
{ "err" : null, "n" : 0,
"wtimeout" : true, "waited" : 3006,
"errmsg" : "timed out waiting for slaves", "ok" : 0 }
8. Shell Command Line
--eval
it/cursor
Printing values; be careful
Pass in a script
9. Enable Profiling
setProfilingLevel(lvl, )
0: none
1: time-based
2: all
Reading from profile collection
>db.system.profile.find()
10. _id: ObjectId Generated on Client
Most drivers create _id field (if not set)
No way to get _id after insert (from server)
Nothingyou cant do yourself
class Foo {
ObjectId id = new ObjectId()
.
}
11. mongod.conf
Specify a logpath or it goes to /dev/null
Quiet = true/anything
Dont do Quiet = false
Flag options used no matter what values
V[v*] = true for verbose logging
12. Limit/Sort/Pagination
Sort fields should be the last in the index
Limit helps with in-memory sorts (diff alg.)
Skip/Offset still walks through index
Pagination is best using last _id/sort value
Set batchsize = pagesize (under 4MB)
13. Bulk Data Loading
Initial load
Create the index after load
Start with new DB if possible
Updates
Be careful of excessive indexes
Sometimes better to drop and recreate indexes
14. Printing Collection Stats
db.getCollectionNames().
forEach(function(x){
print(Collection: + x);
printjson(db[x].stats());
})
15. Backups
Only way to get a consistent state
Dont use mongoexport (lacks type fidelity)
Use mongodump (snap-shotted query)
Or fsync+copy
1.) fsync + lock
2.) backup dbpath files
3.) release lock
16. mongostat
Watch
% idx miss
faults/sec
flushes/sec
17. Questions
You dont have to go home, but you cant
[email protected]