Professional Documents
Culture Documents
M DB S: The Mongodb Solution For Big Data
M DB S: The Mongodb Solution For Big Data
VS MONGODB AT SCALE
20x
90%
70%
solution or to ™
,
u
s
www.Tokutek.com or email us:
contact@tokutek.co
B r o u g H t t o Yo u B Y:
17
1
» Configuration Options
CONTENTS
MongoDB
» Using the Shell
» Diagnosing What’s Happening
» Quick Rules
» Query Operators
By Vlad Mihalcea
» And more...
Get More Refcardz! Visit
easy to use from almost any language. As of August If you started mongod with a bunch of options six
2014, MongoDB is by far the m o st p opu lar NoSQL months ago, how can you see which options you
d a tabas e a cco rd i n g to http://db-engines.co m/e n / used? The shell has a helper:
ranking. This R efcard is inte n d e d to h elp yo u g e t t h e
m o st o u t of MongoDB a n d a ss u m e s th at yo u alre a d y > db.serverCmdL ineOpts()
k n ow the basics If you’re just starting out, try t h e s e { “ a r g v ” : [ “. I mo ng o d”, “ - - p or t ”, “30 0 0 0” ] , “ pa r s e d” :
{ }, “ o k” : 1 }
re so u rces:
• For installation notes, see http://docs.mongodb. The parsed field is a list of arguments read from a
org/manual/installation/ config file.
t y p e s of options: a d mi ni s t r a t i ve h e l p
hel p c onne c t c o n ne c t i ng t o a db
hel p
Co mma n d - Li n E Co n f ig f iLE ...
--d bpat h /path/to/db dbpath=/path/to/db Note that there are separate help functions for
databases, collections, replica sets, sharding,
--auth auth=true
administration, and more. Although not listed
-vvv vvv=true explicitly, there is also help for cursors:
Run mongod --help for a full list of options. Here are > II list common cursor functions
> db.foo.find().help()
some of the most useful:
--logappend
t runc at e) t h e e xisting log file.
Always use this when using the
--logpath option.
--fork
Forks t h e m o n g o d a s a d a e m o n
process.
--auth
E na bl e s authenti ca tion o n a single
server.
mo n g o d B
--bind_ip address
Onl y allow s connections fro m t h e
specified network interface.
© DZo n e, I n c . | DZo n e . c o m
2 Mongodb
can run it without the parentheses in the shell to see its • DBCollection.prototype.reIndex
source code:
• DBCollection.prototype.dropIndexes
> II run the function
> db.serverCmdLineOpts() CHanging tHE prompt
{ “argv” : [ “.Imongod” ], “parsed” : { }, “ok” : 1 }
> II see its source The shell prompt can be customized by setting the
> db.serverCmdLineOpts
prompt variable to a function that returns a string:
This can be helpful for seeing what arguments it expects prompt = function() {
or what errors it can throw, as well as how to run it try {
db.getLastError();
from another language. }
catch (e) {
print(e);
Using edit }
The shell has limited multi-line support, so it can be return (new Date())+”$ “;
}
difficult to program in. The shell helper edit makes this
easier, opening up a text editor and allowing you to edit
variables from there. For example: If you set prompt, it will be executed each time the
prompt is drawn (thus, the example above would give
> x = f u nc t i o n( ) { / * s o m e f u n c t i on w e ’ r e g o i n g t o f i l l i n * / } you the time the last operation completed).
> edit x
<opens emacs with the contents of x>
Try to include the d b.getLa stError() function call in your
p ro m p t . This is included in the d e fault p ro m p t a n d take s
Modify the variable in your editor, then save and exit. The c are of server reconnect ion an d returning errors fro m
variable will be set in the shell. writes.
Either the EDITOR environment variable or a MongoDB Also, always put any code that could throw an exception
shell variable EDITOR must be set to use edit. You can in a try/catch block. It’s annoying to have your prompt
set it in the MongoDB shell as follows: turn into an exception!
> EDITOR=”IusrIbinIemacs”
© DZo n e, I n c . | DZo n e . c o m
3 Mongodb
This command will show you what it thinks the state and
There are several important fields in the output of
status of t he ot her members are.
explain():
•n: the number of results returned. Running rs.status() on a secondary will show you who
the secondary is syncing from in the (poorly named)
•nscanned: the number of index entries read. syncingTo field.
© DZo n e, I n c . | DZo n e . c o m
4 Mongodb
For example:
{age : {$gte : 18}}
updatE format
Updates are always of the form:
See http://mms.10gen.com/help for more {key : {$mod : value}}
d ocumentatio n.
For example:
dataBasEs
Database names cannot contain “.”, “$”, or “\0” (the QuErY opErators :
null character). N a m e s can only contain characters t h a t :Matches
can be used on your filesystem as filenames. Admin, x: Does not mat ch
config, and local a re reser ved d atabase n a m e s (yo u c a n o p Er a t o r E x a m pLE Q u Er Y Ex a mp LE d o Cs
sto re yo u r o w n d ata in th em, b u t yo u should n ever d ro p
t hem). {numSold: 1}
$gt, $gte, $lt, $lte,
CoLLECtions $ne
{numSold : {$lt:3}} x {numSold: “hello”}
x {x : 1}
Collection names cannot contain “$” or “\0”. N am e s
prefixed with “system.” are reserved by MongoDB a n d {age: 21}
c a n n o t b e d ro p p e d (eve n if yo u c re ate d th e collection). $in, $nin {age : {$in : [10, 14, 21]}} {age:
D o t s are ofte n used fo r o rganization in collection names, [9 , 10, 11]}
© DZo n e, I n c . | DZo n e . c o m
5 Mongodb
$set {x:”foo”} {$set:{x:[1,2,3]}} {x:[1,2,3]} $divide : [“$sum”, “$count”] Divides the sum field by count.
$mod : [ “ $ s u m ” , “ $ c o u n t ” ]
T he remainde r of dividing s u m by
$unset {x:”foo”} {$unset:{x:true}} {}
count.
{$inc:{co u n td o wn:-
$inc {c o u n td o wn:5}
{countdown:4} $multiply : [“$mph”, 24, 365] Multiplies mp h by 24*365
1}}
$subtract : [“$price”,
$push, “$discount”] S u b t racts d isco u n t fro m price .
{votes:[-1,-1,1]} {$push:{votes:-1}} {votes:[-1,-1,1,-1}}
$pushAll
1 if n a m e is less t h a n ZZ, 0 if n a m e is
$strcasecm p : [“ZZ”, “$name”]
$pull, {blacklist::[“ip1”, {$pull:{blacklist: {blacklist:”ip1”, ZZ, -1 if name is greater than ZZ.
$pullAll ”ip2”, “ip3”]} “ip2”}} ”ip3”} G e t s th e a re a co d e (first t hre e
$ s u b s t r : [“$phone”, 0, 3]
{queue:[“1pm” ,” {queue:[“3pm”, characters) of phone.
$p o p
{$pop:{queue:-1}}
3pm”,”8pm”]} “8pm”]} $t oLow e r : “ $ s t r ” C o n verts str to all lowe rc a s e .
$ l e t : { v a r s , in }
Bi nd s variables to be used in
subexpressions.
aggrEgation pipELinE opErators $map : {
input : [ 1 , 2 ] ,
Map calls the p rov i d e d s u b -
a s : “v”
T he aggregation framework can be used to in : { $multiply: [
ex p re ssion o n ce fo r e ac h e l e m e n t in
a n array, a n d co n struct s a n ew ar ray
“$$v”, 2 ] }
perform everything from simple queries to complex from the results (e.g. [2, 4]).
aggregations. }
E x p l o d e s an e m b e d d e d ar ray into To restore from this backup, copy the files to the correct
{$ un win d : f ie ld }
its own top-level documents. server ’s dbpat h and st art the mongod.
{$ redact : expression}
R e m oves sensitive inform ation fro m
t he current aggregation result. If you have a filesystem that does filesystem snapshots
and your journal is on the same volume and you haven’t
© DZo n e, I n c . | DZo n e . c o m
5 Mongodb
done anything stripy with RAID, you can take a snapshot usEr managEmEnt
without locking. In this case, when you restore, the
journal will replay operations to make the data files CHECK CurrEnt usEr priviLEgEs
consistent.
> db.runCommand(
Mongodump is only for backup in special cases. If you ... {
... us e r s I nf o: ” ma n a g e r ”,
decide to use it anyway, don’t fsync+lock first. ... s h o wPr i v i l e ge s : t r u e
... }
... )
> d b . rger va on kt Re R
o loelseTo
s FU
r osm
e rU( s e r (
... “ s e n s or s Us e r Ad mi n”,
... [
... { r ol e: “ ru es ae d”,r Ad midb:n”, “ addb: “ s e }n s or s ” }
mi n”
... ]
... )
© DZo n e, I n c . | DZo n e . c o m
8 Mongodb
3. Each namespace file must be no larger than 2047 Field names cannot contain: $, null or . (dot)
meg ab y tes.
4. The index entry total size must be less than 1024 additionaL rEsourCEs
bytes.
•Download MongoDB at http://www.mongodb.org/
5. A collection can have up to 64 indexes. downl oa ds
6. The index name (namespace included) cannot be •Documentation is available at
longer than 125 chars. ht t p ://d ocs. mon go db.org
7. A replica set can have at most 12 members. •Download TokuMX distribution at http://www.to
8. A shard key can have at most 512 bytes. kutek.com//tokumx_downloads/
9. A shard key is always immutable. •See the roadmap and fil e bugs and request features
at http://jira.mongodb.org
10. MongoDB non-indexed field sort will return results
only if this o p e ration doesn’ t us e m o re than 32 •Ask questions on the mailing list: http://groups.
m e g a b y te s of m e m o r y. google.com/group/mongodb-user