DEVELOPMENT/DB

[MongoDB] Introduction to MongoDB

heeble 2023. 9. 13. 23:27
본 문서는 MongoDB university의 "Introduction to MongoDB"를 정리한 포스팅입니다.
강의 : https://learn.mongodb.com/learning-paths/introduction-to-mongodb\

Unit 01: Getting Started with MongoDB Atlas, the Developer Data Platform

Replica Set

- A group of servers that hold your data

- Ensures data redundancy and high availability


Unit 02: Overview of MongoDB and the Document Model

Document Model

- Easier to plan how application data will correspond to data in the database

- Model data of any shape or structure

 

Document Structure

{
    "_id": 1,
    "name": "AC3 Phone",
    "colors" : ["black", "silver"],
    "price" : 200,
    "available" : true
}

- string, object, array, boolean, null, date, objectId, ... any data type


Unit 03: Connecting to a MongoDB Database

 

MongoDB Connection String


Unit 04: MongoDB CRUD Operations: Insert and Find Documents

INSERT DOCUMENT(S)

db.grades.insertOne({
  student_id: 654321,
  products: [
    {
      type: "exam",
      score: 90,
    },
    {
      type: "homework",
      score: 59,
    },
    {
      type: "quiz",
      score: 75,
    },
    {
      type: "homework",
      score: 88,
    },
  ],
  class_id: 550,
})
db.grades.insertMany([
  {
    student_id: 546789,
    products: [
      {
        type: "quiz",
        score: 50,
      },
      {
        type: "homework",
        score: 70,
      },
      {
        type: "quiz",
        score: 66,
      },
      {
        type: "exam",
        score: 70,
      },
    ],
    class_id: 551,
  },
  ....,
])

FIND DOCUMENT(S)

db.zips.find({ _id: ObjectId("5c8eccc1caa187d17ca6ed16") })
db.zips.find({ city: { $in: ["PHOENIX", "CHICAGO"] } })

FIND DOCUMENT(S) by USING Comparison Operators

// $gt
db.sales.find({ "items.price": { $gt: 50}})

// $lt
db.sales.find({ "items.price": { $lt: 50}})

// $lte
db.sales.find({ "customer.age": { $lte: 65}})

// $gte
db.sales.find({ "customer.age": { $gte: 65}})

Querying on Array Elements in MongoDB

db.accounts.find({ products: "InvestmentFund"})

db.sales.find({
  items: {
    $elemMatch: { name: "laptop", price: { $gt: 800 }, quantity: { $gte: 1 } },
  },
})

/////

db.routes.find({
  $and: [
    { $or: [{ dst_airport: "SEA" }, { src_airport: "SEA" }] },
    { $or: [{ "airline.name": "American Airlines" }, { airplane: 320 }] },
  ]
})

Unit 05: MongoDB CRUD: Replace and Delete

Replacing a Document in MongoDB

  • filter : a query that matches the document to replace
  • replacement : new document to replace
  • options
db.books.replaceOne(
  {
    _id: ObjectId("6282afeb441a74a98dbbec4e"),
  },
  {
    title: "Data Science Fundamentals for Python and MongoDB",
    isbn: "1484235967",
    publishedDate: new Date("2018-5-10"),
    thumbnailUrl:
      "https://m.media-amazon.com/images/I/71opmUBc2wL._AC_UY218_.jpg",
    authors: ["David Paper"],
    categories: ["Data Science"],
  }
)

Updating a Document

db.podcasts.updateOne(
  {
    _id: ObjectId("5e8f8f8f8f8f8f8f8f8f8f8"),
  },

  {
    $set: {
      subscribers: 98562,
    },
  }
)
// upsert : creates a new document if no documents match the filtered criteria
db.podcasts.updateOne(
  { title: "The Developer Hub" },
  { $set: { topics: ["databases", "MongoDB"] } },
  { upsert: true }
)

// push : adds a new value to the array
db.podcasts.updateOne(
  { _id: ObjectId("5e8f8f8f8f8f8f8f8f8f8f8") },
  { $push: { hosts: "Nic Raboy" } }
)
// findAndModify - find and replace a single documents
db.podcasts.findAndModify({
  query: { _id: ObjectId("6261a92dfee1ff300dc80bf1") },
  update: { $inc: { subscribers: 1 } },
  new: true,
})

// updateMany - to update multiple documents
db.books.updateMany(
  { publishedDate: { $lt: new Date("2019-01-01") } },
  { $set: { status: "LEGACY" } }
)

Deleting Documents

// delete one document
db.podcasts.deleteOne({ _id: Objectid("6282c9862acb966e76bbf20a") })

// delete many documents
db.podcasts.deleteMany({category: “crime”})

Unit 06: MongoDB CRUD Operations: Modifying Query Results

Sorting and Limiting Query Results

// sorting
db.collection.find(<query>).sort(<sort>)

// limiting
db.companies.find(<query>).limit(<number>)
// Return data on all music companies, sorted alphabetically from A to Z.
db.companies.find({ category_code: "music" }).sort({ name: 1 });

// Return data on all music companies, sorted alphabetically from A to Z.
// Ensure consistent sort order
db.companies.find({ category_code: "music" }).sort({ name: 1, _id: 1 });
// Return the three music companies with the highest number of employees. 
// Ensure consistent sort order.
db.companies
  .find({ category_code: "music" })
  .sort({ number_of_employees: -1, _id: 1 })
  .limit(3);

Returning Specific Data from a query

// Syntax:
db.collection.find( <query>, <projection> )

// Include a Field
db.collection.find( <query>, { <field> : 1 })

// Exclude a Field
db.collection.find(query, { <field> : 0, <field>: 0 })
// Return all restaurant inspections - business name, result, and _id fields only
db.inspections.find(
  { sector: "Restaurant - 818" },
  { business_name: 1, result: 1 }
)

// Return all inspections with result of "Pass" or "Warning" - exclude date and zip code
db.inspections.find(
  { result: { $in: ["Pass", "Warning"] } },
  { date: 0, "address.zip": 0 }
)

// *** _id Field is included by default, and can be suppressed by setting 0
// Return all restaurant inspections - business name and result fields only
db.inspections.find(
  { sector: "Restaurant - 818" },
  { business_name: 1, result: 1, _id: 0 }
)

Counting Documents

// Syntax:
db.collection.countDocuments( <query>, <options> )
// Count number of docs in trip collection
db.trips.countDocuments({})

// Count number of trips over 120 minutes by subscribers
db.trips.countDocuments({ tripduration: { $gt: 120 }, usertype: "Subscriber" })

Unit 07: MongoDB Aggregation

  • Aggregation : Collection and summary of data
  • Stage : One of built-in methods that can be completed on the data, but does not permanently alter it
  • Aggregation pipeline: A series of stages completed on the data in order
db.collection.aggregate([
    {
        $stage1: {
            { expression1 },
            { expression2 }...
        },
        $stage2: {
            { expression1 }...
        }
    }
])

$match and $group STAGE

db.zips.aggregate([
{   
   $match: { 
      state: "CA"
    }
},
{
   $group: {
      _id: "$city",
      totalZips: { $count : { } }
   }
}
])

$sort and $limit STAGE

db.zips.aggregate([
{
  $sort: {
    pop: -1
  }
},
{
  $limit:  5
}
])

$project, $count and $set STAGE

// $project
{
    $project: {
        state:1, 
        zip:1,
        population:"$pop",
        _id:0
    }
}

// $set - creates new fields or changes the value of existing fields
{
    $set: {
        place: {
            $concat:["$city",",","$state"]
        },
        pop:10000
     }
}
  
// $count
{
    $count: "total_zips"
}

$out

- writes the documents that are returned by an aggregation pipeline into a collection

- must be the last stage

- creates a new collection if it does not already exist

- if collection exists, $out replaces the existing collection with new data

db.collection.aggregate([
    { $match: {...}},
    { $group: {...}},
    ...,
    { $out: {...}},
])
db.collection.aggregate([
    { 
      $group: {
          _id: "$state",
          total_pop: { $sum: "$pop" }
       }
    },
    { 
      $match: {
          total_pop: { $lt: 1000000 }
       }
    },
    { 
      $out: "small_states"
    }
])


Unit 08: MongoDB Indexing

What are indexes?

  • Special data structures
  • Store small portion of the data
  • Ordered and easy to search efficiently
  • Point to the document identity

Single Field Index

db.customers.createIndex({
  birthdate: 1
})

// unique single field
db.customers.createIndex({
  email: 1
},
{
  unique:true
})
// view the indexes
db.customers.getIndexes()

// Check if an index is being used on a query
db.customers.explain().find({
  birthdate: {
    $gt:ISODate("1995-08-01")
  }
})

Multike Index

- If a single field or compound index includes an array field, then the index is a multikey index

 

Compound Index

db.customers.createIndex({
  active:1, 
  birthdate:-1,
  name:1
})
// order of fields in a compound index
db.customers.find({
  birthdate: { $gte:ISODate("1977-01-01") },
  active:true
}).sort({
   birthdate:-1, 
   name:1
})

// efficient index
db.customers.createIndex({
  active:1, 
  birthdate:-1,
  name:1
})

Deleting an Index

// delete index by name
db.customers.dropIndex(
  'active_1_birthdate_-1_name_1'
)

// delete index by key
db.customers.dropIndex({
  active:1,
  birthdate:-1, 
  name:1
})

// delete all indexes
db.customers.dropIndexes()
db.collection.dropIndexes([
  'index1name', 'index2name', 'index3name'
])

Unit 09: MongoDB Atlas Search

Search Index

 - defines how a search should be performed

Database Index

 - makes database queries more efficient

 

Static Indexing

- The fields being queried are always the same

$search {
  "compound": {
    "must": [{
      "text": {
        "query": "field",
        "path": "habitat"
      }
    }],
    "should": [{
      "range": {
        "gte": 45,
        "path": "wingspan_cm",
        "score": {"constant": {"value": 5}}
      }
    }]
  }
}

The compound operator within the $search aggregation stage allows us

- to give weight to different field

- filter our results without having to create additional aggregation stages. 

 

The four options for the compound operator

  • must - exclude records that do not meet the criteria
  • mustNot - exclude results that do meet the criteria
  • should - allow you to give weight to results that do meet the criteria so that they appear first
  • filter - remove results that do not meet the criteria

 

Grouping Search Results

$searchMeta: {
    "facet": {
        "operator": {
            "text": {
            "query": ["Northern Cardinal"],
            "path": "common_name"
            }
        },
        "facets": {
            "sightingWeekFacet": {
                "type": "date",
                "path": "sighting",
                "boundaries": [ISODate("2022-01-01"), 
                    ISODate("2022-01-08"),
                    ISODate("2022-01-15"),
                    ISODate("2022-01-22")],
                "default" : "other"
            }
        }
    }
}
  • facets - buckets that we group our search results into
  • $searchMeta - allows us to see the facets and how many results are in each bucket

Unit 10: Introduction to MongoDB Data Modeling


Unit 11: MongoDB Transactions

ACID Transactions

a group of database operations that will be a completed as a unit or not at all

 

  • Atomicity - all operations will either succeed or fail together
  • Consistency - all changes made by operations are consistent with database constraints
  • Isolation - multiple transactions can happen at the same time without affecting the outcome of the otehr transaction
  • Durability - all of the changes that are made by operations in transaction will persist, no matter what

Using a Transaction

const session = db.getMongo().startSession()

session.startTransaction()

const account = session.getDatabase('< add database name here>').getCollection('<add collection name here>')

//Add database operations like .updateOne() here

session.commitTransaction()

Aborting a Transaction

const session = db.getMongo().startSession()

session.startTransaction()

const account = session.getDatabase('< add database name here>').getCollection('<add collection name here>')

//Add database operations like .updateOne() here

session.abortTransaction()


9월 15일에 강의를 수강하면서 정리하던 글이었는데,, 중간에 일부가 날라가서 ㅠㅠㅠㅠ

이제야 복구했네요 😂

'DEVELOPMENT > DB' 카테고리의 다른 글

[MongoDB] Building with Patterns  (2) 2023.11.01
[MongoDB] Sharding  (0) 2023.10.28
[MongoDB] Configuration File Options  (0) 2023.10.28
[MongoDB] MongoDB Node.js Developer Path  (0) 2023.09.28