Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit 18bc755

Browse files
committedDec 3, 2018
Add information to the README how to query data
from staging or production database
1 parent a64fff7 commit 18bc755

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed
 

‎scripts/README.md

+41-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
# How to use the backup script
1+
# MongoDB scripts
2+
3+
This README explains how to directly access the production or staging database
4+
for backup or query purposes.
5+
6+
## Backup script
27

38
The backup script is intended to be used as a cron job or as a single command from your laptop.
49
It uses SSH tunneling to a remote host and dumps the mongo database on your machine.
510
Therefore, a public SSH key needs to be copied to the remote machine.
611

7-
# Usage
12+
### Usage
813

914
All parameters must be supplied as environment variables:
1015

@@ -28,7 +33,7 @@ After exporting these environment variables to your bash, run:
2833
```
2934

3035

31-
# Import into your local mongo db (optional)
36+
### Import into your local mongo db (optional)
3237

3338
Run (but change the file name accordingly):
3439
```bash
@@ -40,3 +45,36 @@ If you previously encrypted your dump, run:
4045
gpg --decrypt human-connection-dump_2018-11-21.archive.gpg | mongorestore --gzip --archive
4146
```
4247

48+
49+
## Query remote MongoDB
50+
51+
In contrast to the backup script, querying the database is expected to be done
52+
interactively and on demand by the user. Therefore our suggestion is to use a
53+
tool like [MongoDB compass](https://www.mongodb.com/products/compass) to query
54+
the mongo db through an SSH tunnel. This tool can export a collection as .csv
55+
file and you can further do custom processing with a csv tool like
56+
[q](https://github.com/harelba/q).
57+
58+
### Suggested workflow
59+
60+
Read on the mongodb compass documentation how to connect to the remote mongo
61+
database [through SSH](https://docs.mongodb.com/compass/master/connect/). You
62+
will need all the credentials and a public SSH key on the server as for the
63+
backup script above.
64+
65+
Once you have a connection, use the MongoDB Compass
66+
[query bar](https://docs.mongodb.com/compass/master/query-bar/) to query for the
67+
desired data. You can
68+
[export the result](https://docs.mongodb.com/compass/master/import-export/) as
69+
.json or .csv.
70+
71+
Once you have the .csv file on your machine, use standard SQL queries through
72+
the command line tool q to further process the data.
73+
74+
For example
75+
```sh
76+
q "SELECT email FROM ./invites.csv INTERSECT SELECT email FROM ./emails.csv" -H --delimiter=,
77+
```
78+
79+
[Q's website](http://harelba.github.io/q/usage.html) explains the usage fairly
80+
well.

0 commit comments

Comments
 (0)
This repository has been archived.