Cheatography
https://cheatography.com
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.collection.update({}, {$rename:{"oldname":"newname"}}, false, true);
|
create index |
db.col.createIndex( { "attr1":1, "attr2":1}, { name: "indexName" } )
|
Conversion
convert Long to Float in place |
db.col.updateMany( {},[ { $set: { "attr1" : { $trunc: [ "$attr1", 4 ] } }} ] );
|
convert Long to Float into new collection |
db.col.aggregate([ { $set: { "attr1" : { $trunc: [ "$attr1", 4 ] } }}, { $out: "outCollection"} ]);
|
Data xfer
output json from SQL server |
/opt/mssql-tools/bin/bcp "select * from dbname.dbo.Table FOR JSON PATH" queryout ./output.json -c -S "sql.server.net" -U "sa" -P "passwd"
|
remove linefeeds from output |
pv output.json |tr -d "\n" > output_nolf.json
|
import to mongo |
mongoimport --host mongo.server.com --db db_name --collection col_name --file "output_nolf.json" --jsonArray --numInsertionWorkers=16
|
import csv to sql with bcp |
/opt/mssql-tools/bin/bcp table in csvfile.csv -d database -S "192.168.0.1" -U "sa" -P "password" -q -c -t ,
|
mongodump / mongorestore
export database |
dump mongodump --db db_name --out /path_of_your_backup/\date +"%m-%d-%y"
|
import database |
mongorestore --db db_name --drop /path_of_your_backup/01-01-19/db_name/
|
|
|
BSON Types
Type |
BSON |
Alias |
Double |
1 |
|
String |
2 |
|
Object |
3 |
|
Array |
4 |
|
ObjectID |
7 |
|
Boolean |
8 |
|
Date |
9 |
|
Null |
10 |
|
JavaScript |
13 |
|
32-Bit Int |
16 |
|
Timestamp |
17 |
|
64-bit Int |
18 |
|
Decimal 128 |
19 |
|
System
Check Index Creation Progress |
db.currentOp(true).inprog.forEach(function(op){ if(op.msg!==undefined) print(op.msg) })
|
Set Runtime Wired Tiger Cache Size |
db.getSiblingDB("admin").runCommand({ setParameter: 1, wiredTigerEngineRuntimeConfig: "cache_size=90G" });
|
Linux Commands
time process has been running |
ps -p {PID-HERE} -o etime
|
find pid of process |
|
|