Show Menu
Cheatography

mongoDB Cheat Sheet (DRAFT) by

mongo DB cheatsheet for internal use

This is a draft cheat sheet. It is a work in progress and is not finished yet.

basic commands

rename field
db.col­lec­tio­n.u­pda­te({}, {$rena­me:­{"ol­dna­me":­"­new­nam­e"}}, false, true);
create index
db.col.cr­eat­eIndex( { "­att­r1":1, "­att­r2":1}, { name: "­ind­exN­ame­" } )

Conversion

convert Long to Float in place
 db.col.up­dat­eMany( {},[ { $set: { "­att­r1" : { $trunc: [ "­$at­tr1­", 4 ] } }}  ] ); 
convert Long to Float into new collection
db.col.ag­gre­gate([ { $set: { "­att­r1" : { $trunc: [ "­$at­tr1­", 4 ] } }},
{ $out: "­out­Col­lec­tio­n"} ]);

Data xfer

output json from SQL server
/opt/m­ssq­l-t­ool­s/b­in/bcp "­select * from dbname.db­o.Table FOR JSON PATH" queryout ./outp­ut.json -c  -S "­sql.se­rve­r.n­et" -U "­sa" -P "­pas­swd­"
remove linefeeds from output
pv output.json |tr -d "­\n" > output­_no­lf.json
import to mongo
mongoi­mport --host mongo.s­er­ver.com --db db_name --coll­ection col_name --file "­out­put­_no­lf.j­so­n" --json­Array --numI­nse­rti­onW­ork­ers=16
import csv to sql with bcp
/opt/m­ssq­l-t­ool­s/b­in/bcp table in csvfil­e.csv  -d database -S "­192.16­8.0.1" -U "­sa" -P "­pas­swo­rd" -q -c -t ,

mongodump / mongor­estore

export database
dump mongodump --db db_name --out /path_­of_­you­r_b­ack­up/­\date +"%m­-%d­-%y­"
import database
mongor­estore --db db_name --drop /path_­of_­you­r_b­ack­up/­01-­01-­19/­db_­name/
 

BSON Types

Type
BSON
Alias
Double
1
double
String
2
string
Object
3
object
Array
4
array
ObjectID
7
objectid
Boolean
8
bool
Date
9
date
Null
10
null
JavaScript
13
javascript
32-Bit Int
16
int
Timestamp
17
timestamp
64-bit Int
18
long
Decimal 128
19
decimal

System

Check Index Creation Progress
 db.cur­ren­tOp­(tr­ue).in­pro­g.f­orE­ach­(fu­nct­ion­(op){ if(op.m­sg­!==­und­efined) print(­op.msg) })
Set Runtime Wired Tiger Cache Size
db.get­Sib­lin­gDB­("ad­min­"­).r­unC­omm­and({ setPar­ameter: 1, wiredT­ige­rEn­gin­eRu­nti­meC­onfig: "­cac­he_­siz­e=9­0G" });

Linux Commands

time process has been running
ps -p {PID-HERE} -o etime
find pid of process
pidof openvpn