factbook.json/MONGO.md

121 lines
2.4 KiB
Markdown
Raw Normal View History

2017-03-28 15:37:26 +02:00
# Mongo
2017-03-28 16:45:17 +02:00
[Import](#import) •
[Query Examples](#query-examples)
2017-03-28 16:43:37 +02:00
## Import
2017-03-28 17:00:23 +02:00
Use the `mongoimport` command/tool to import the `factbook.json` documents.
2017-03-28 16:58:36 +02:00
For example to import the country profile of Austria (that is, `europe/au.json`) use:
2017-03-28 16:43:37 +02:00
```
$ mongoimport --db world --collection factbook --file europe/au.json
```
To import all documents use a shell script. Example `import.sh`:
``` bash
#!/bin/bash
2017-03-28 16:45:17 +02:00
MONGOIMPORT = mongoimport
SOURCE = . # assume working folder (as root)
2017-03-28 16:43:37 +02:00
2017-03-28 16:58:36 +02:00
function import_file {
2017-03-28 16:45:17 +02:00
echo " importing >${1}<..."
2017-03-28 16:43:37 +02:00
${MONGOIMPORT} --db world --collection factbook --file ${1}
}
function import_region {
for file in ${SOURCE}/$1/*.json
do
2017-03-28 16:58:36 +02:00
import_file ${file}
2017-03-28 16:43:37 +02:00
done
}
import_region africa
import_region antarctica
import_region australia-oceania
import_region central-america-n-caribbean
import_region central-asia
import_region east-n-southeast-asia
import_region europe
import_region middle-east
import_region north-america
import_region oceans
import_region south-america
import_region south-asia
import_region world
```
2017-03-28 16:58:36 +02:00
To check up if all country profiles got imported use a query in the mongo shell e.g.:
2017-03-28 16:43:37 +02:00
```
> use world
switched to db world
> db.factbook.count()
261
```
2017-03-28 15:37:26 +02:00
## Query Examples
2017-03-28 15:40:40 +02:00
### Find all countries speaking _X_?
2017-03-28 15:37:26 +02:00
German
2017-03-28 15:39:20 +02:00
```js
2017-03-28 15:42:57 +02:00
db.factbook.find( { "People and Society.Languages.text": /German/ }, { "Government.Country name": 1 } )
2017-03-28 15:37:26 +02:00
```
English
2017-03-28 15:39:20 +02:00
```js
2017-03-28 15:42:57 +02:00
db.factbook.find( { "People and Society.Languages.text": /English/ }, { "Government.Country name": 1 } )
2017-03-28 15:37:26 +02:00
```
2017-03-28 15:40:40 +02:00
### Find all countries with a land border with _X_?
2017-03-28 15:37:26 +02:00
Austria
2017-03-28 15:39:20 +02:00
```js
2017-03-28 15:42:57 +02:00
db.factbook.find( { "Geography.Land boundaries.border countries.text": /Austria/ },
2017-03-28 15:39:20 +02:00
{ "Government.Country name": 1 } )
2017-03-28 15:37:26 +02:00
```
Germany
2017-03-28 15:39:20 +02:00
```js
2017-03-28 15:42:57 +02:00
db.factbook.find( { "Geography.Land boundaries.border countries.text": /Germany/ },
2017-03-28 15:39:20 +02:00
{ "Government.Country name": 1 } )
2017-03-28 15:37:26 +02:00
```
2017-03-28 15:40:40 +02:00
### Find all import partner countries for _X_?
2017-03-28 15:37:26 +02:00
Austria
2017-03-28 15:39:20 +02:00
```js
2017-03-28 15:42:57 +02:00
db.factbook.find( { "Economy.Imports - partners.text": /Austria/ }, { "Government.Country name": 1 } )
2017-03-28 15:37:26 +02:00
```
### Find all countries with voting age 16 years
2017-03-28 15:39:20 +02:00
```js
2017-03-28 15:42:57 +02:00
db.factbook.find( { "Government.Suffrage.text": /16/ },
2017-03-28 15:39:20 +02:00
{ "Government.Country name": 1, "Government.Suffrage": 1 })
2017-03-28 15:37:26 +02:00
```
2017-03-28 15:40:40 +02:00
### Find all countries with _X_ membership?
2017-03-28 15:37:26 +02:00
NATO
2017-03-28 15:39:20 +02:00
```js
2017-03-28 15:42:57 +02:00
db.factbook.find( { "Government.International organization participation.text": /NATO/ },
2017-03-28 15:39:20 +02:00
{ "Government.Country name": 1 } )
2017-03-28 15:37:26 +02:00
```
And so on.