Skip to content

Commit

Permalink
Added info to mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres Hermosilla committed Jul 18, 2017
1 parent b5da25e commit 6389f20
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions mongodb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Mongodb

**Get stats**
`mongo Health --eval "db.stats()" &>/dev/null`

**Query inline**
```shell
second_ago=$(date --date="2 minutes ago" +%s)
query="{class: 0, ts: {\$gt: ${second_ago} }}"
mongo Health --eval "db.Stats.findOne($query).ts" &>/dev/null
```

## Export
**Snapshot**
`mongodump --quiet -d User -o /data/snapshot/user-$(date +%Y%m%d)`

**CSV**
`mongoexport --db Health --collection Stats --csv -q '{class: 1, type:"Customer"}' --fields sn,model_full,domain,cluster_serials,ts --out /tmp/data.csv`

## Restore
**Import from json**
`mongoimport -db HealthTS --collection CountTS --jsonArray --file count-ts.json`

**Import from snapshots**
```shell
restore_data()
{
local reindex='db.getCollectionNames().forEach(function(col){db[col].reIndex()});'
mongorestore --db Health ./snapshot/Health/
mongorestore --db User ./snapshot/User/
mongo User --eval "$reindex" &>/dev/null
mongo Health --eval "$reindex" &>/dev/null
}
```

0 comments on commit 6389f20

Please sign in to comment.