---
# System prepended metadata

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

---

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

**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
- Structured way to store and access data 
- No SQL database
    - Does not use legacy approach of related tables of data (now rows and cols)
- Collection of documents

**Documents**
- A way to organize and store data as a set of field-value pairs
     `<field> : <value> `
- Field: unique identifier for a datapoint
- Value: data related to a given identifier
- Collection: organized store of documents in MongoDB, usually with common fields between documents. There can be many collections per database and many documents per collection
    
**MongoDB Atlas**
- Replica Set: a few connected machines that store the same data to ensure that if something happens to one of the machines the data will remain intact
- Instance: a single machine locally or in the cloud, running a certain software, in our case it is the MongoDB database
- Cluster: group of servers that store your data

### Commands
:::warning
name of new collection = example
:::
Creating a new collection 
```
db.createCollection("example");
```
Adding data to a collection
```
db.example.insert({
    "field1": "value1"
    'field2': 'value2'
    field3: 'value3'
    field4  "value4"
})
```
To find data
```
db.example.find()
```
To find and format data simultaneously
```
db.example.find().pretty()
```
Updating the document - existing parameters 
```
db.example.update(
    {field1: 'value1'},
    {$set:{field1: 'newvalue1'}})
```
Updating the document - adding new parameters
```
db.example.update(
    {field1: 'newvalue1'},
    {$set:{field5: 'value5'}},
    {$upsert: true})
```
Removing paramenters
```
db.example.update(
    {field1: ''},
    {$upsert: true})
```
Deleting the document 
```
db.example.delete()
```
Couting the number of documents
```
db.example.count()
```
**In Robo 3T**
- Sorting data in chronological order (oldest to newest)
  >`db.getCollection('raw_data').find({}).sort({"Timming":1})`
- Modifying one field
  > `(db.raw_data.update({'Dispenser':'EE_06_01'},{$set:{'Dispenser':'EE0601'}},)`
- Modifying multiple fields simultaneously
  > `db.raw_data.update({'Dispenser':'EE_06_01'},{$set:{'Dispenser':'EE0601'}},{multi:true})`