---
# System prepended metadata

title: '<center><i class="fa fa-edit"></i> Navigating MongoDB: Importing, Exporting, and Querying Data </center>'
tags: [Internship]

---

# <center><i class="fa fa-edit"></i> Navigating MongoDB: Importing, Exporting, and Querying Data </center>
###### tags: `Internship`
:::info
**Goal:**
To gain a basic understanding of MongoDB  fundamentals. Focus on importing, exporting, and querying Data
- [x] Importing
- [x] Exporting
- [x] Querying Data

**Resources:**
[MongoDB Basics Online Course](https://university.mongodb.com/courses/M001/about)
[MongoDB 常用Query指令](/b2Y7lgR1RaeQyMHp9RGHdQ)
[Database Commands](https://www.mongodb.com/docs/manual/reference/command/)
:::
## Overview
- JSON: Javascript Standard Object Notation
    - Pros
        - User friendly
        - Readable 
        - Familiar
    - Cons
        - Text-based -> slow
        - Space consuming
        - Supports limited number of basic data types
- JSON fomart in MongoDB
    - Start and end with curly braces
    - Separate key/field and values with a semicolon
    - Separate each key:value pair with a comma
    - Keys/fields must be surrounded by " " or ' '
- BSON: Bridges the gap between binary representation and JSON format
- Optimized for 
    - Speed
    - Space
    - Flexibility
    - High performance
    - General purpose focus
    
![](https://i.imgur.com/9u20Euq.jpg)

### Importing and Exporting Data
- URI String: Uniform Resource Identifier
- srv: Establishes a secure connection
- JSON
    - mongoimport
        >  `mongoimport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --drop sales.json`
    - mongoexport
        > `mongoexport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --collection=sales --out=sales.json`
- BSON
    - mongorestore
        > `mongorestore --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies"  --drop dump`
    - mongodump
        >  `mongodump --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies"`

### Querying
- Data Explorer: Querying method from MongoDB directly
- Go to MongoDB cloud -> Clusters -> Collections
    - Namespace: The concatenation of the database name and collection name is called a namespace.

:::warning
Looked at the `sample_training.zips` collection and issued the following queries by filtering:

```
{"state": "NY"}
{"state": "NY", "city": "ALBANY"}
```
:::
- Find Command: Querying method from the shell

**Procedures**
- Connect to Atlas Cluster
```
mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin"
```
- Show list of databases in the cluster
```
show dbs
```
- Select database of choice
```
use sample_training
```
- View collections in database
```
show collectioins
```
- To query the zips collection (issue `find()`)
```
db.zips.find({"state": "NY"})
```
- Iterate through the cursor
```
it
```
- `count()`
```
db.zips.find({"state": "NY"}).count()
```
- `pretty()`
    - Places each field:value pair on a separate line
```
db.zips.find({"state": "NY", "city": "ALBANY"}).pretty()
```
- Find commant without a query presents first 20 documents in databse
    - Not in any particular order
```
db.zips.find{}
```
