Week01-Main




題庫-Part1

1

搜尋 name 欄位為 “Ray Xu” 的 document 列表

db.posts.find(
  {
    "name": "Ray Xu"
  }
).pretty()

2

新增 1 筆 document,請全部欄位皆填寫

db.posts.insertOne(
  {
    "name": "Aya",
    "tags": [
      "meme",
      "HD"
    ],
    "type": "group",
    "image": "https://imgur.com/NQinKJB",
    "createdAt": "2022-03-10 23:20:44 UTC",
    "content": "LOL",
    "likes": 0,
    "comments": 0
  }
)

3

新增多筆 document,請全部欄位皆填寫

db.posts.insertMany([
  {
    "name": "Aya",
    "tags": [
      "meme",
      "CSS"
    ],
    "type": "group",
    "image": "https://i.imgur.com/4srM5iY.png",
    "createdAt": new Date(),
    "content": "QQ",
    "likes": 0,
    "comments": 0
  },
  {
    "name": "Aya",
    "tags": [
      "meme",
      "CSS"
    ],
    "type": "group",
    "image": "https://i.imgur.com/4srM5iY.png",
    "createdAt": new Date(),
    "content": "QQ",
    "likes": 0,
    "comments": 0
  },
  {
    "name": "Aya",
    "tags": [
      "meme",
      "CSS"
    ],
    "type": "group",
    "image": "https://i.imgur.com/4srM5iY.png",
    "createdAt": new Date(),
    "content": "QQ",
    "likes": 0,
    "comments": 0
  }
])
  • #TODO
    • new Date() 轉成字串

4

  • 修改 1 筆 document
    • filter 條件請用 _id 指定
    • content 欄位調整為 測試資料
db.posts.updateOne(
  {
    "_id": ObjectId("62554608889a4ed3a69f26cf")
  },
  {
    "$set": {
      "content": "測試資料"
    }
  }
)

5

  • 修改多筆 name 欄位為 "Ray Xu" 的 document 列表
    • content 欄位都調整為 哈哈你看看你
db.posts.updateMany(
  {
    "name": "Ray Xu"
  },
  {
    "$set": {
      "content": "哈哈你看看你"
    }
  }
)
  • #TODO
    • 如果寫入這種顏文字,好像會漏掉某些特殊符號的字元
      • 哈哈你看看你 (σ′▽‵)′▽‵)σ

6

  • 刪除 1 筆 document
    • filter 條件請用 _id 任意指定其中 1 筆資料
db.posts.deleteOne(
  {
    "_id": ObjectId("62554608889a4ed3a69f26fd")
  }
)

7

  • 刪除多筆 document
    • filter 條件請用 type 為 group 的值,刪除所有社團貼文
db.posts.deleteMany(
  {
    "type": "group"
  }
)

8

  • 刪除多筆 document,filter 條件為以下
    • a. name: "Ray Xu"
    • b. likes: 500(含)個讚以下
db.posts.deleteMany(
  {
    "$and": [
      {
        "likes": {
          "$lte": 500
        }
      },
      {
        "name": "Ray Xu"
      }
    ]
  }
)
  • 判斷可以也寫一起
db.posts.deleteMany(
  {
    "name": "Ray Xu", 
	"likes": {
          "$lte": 500
    }
  }
)

9

查詢全部 posts 的 document 列表

db.posts.find().pretty()
  • {} 也可以
db.posts.find({}).pretty()

10

關鍵字搜尋 name 裡面含有 o 的 document 列表

db.posts.find(
  {
    "name": /o/
  }
).pretty()
  • 【語法】
    • 注意 value 不用字串型別的引號 ''

11

  • 查詢 name 欄位為 "Ray Xu"
    • filter 篩選出介於 500~1000(含)個讚
db.posts.find(
  {
    "$and": [
      {
        "name": "Ray Xu"
      },
      {
        "likes": {
          "$lte": 1000
        }
      },
      {
        "likes": {
          "$gt": 500
        }
      }      
    ]
  }
).pretty()

12

  • 查詢 comments
    • 有超過 500(含)以上的 document 列表
db.posts.find(
  {
    "comments": {
      "$gte": 500
    }
  }
).pretty()

13

  • 查詢 tags 欄位
    • 謎因 或(or) 幹話 的 document 列表
db.posts.find(
  {
    "tags": {
      "$in": [
        "謎因", "幹話"
      ]
    }
  }
).pretty()
  • 也可以用 $or

14

  • 查詢 tags 欄位
    • 幹話 的 document 列表,需隱藏 _id 欄位
db.posts.find(
  {
    "tags": {
      "$in": [
        "幹話"
      ]
    }
  },
  {
    "_id": 0
  }
).pretty()

15

請嘗試用 Mongo Shell 指令刪除全部 Documents

db.posts.deleteMany({})
  • 【語法】
    • 要放 {}
      • 沒放會跑錯誤


題庫-Part2

1

posts 所有 document 數量為?

db.posts.find().count()

2

  • 請查詢 name 為 Ray Xu 的 document 列表
    • 排序為由新到舊
db.posts.find(
  { 
    "name": "Ray Xu" 
  }
).sort(
      { 
        "createdAt": -1 
      }
).pretty()

3

  • 請查詢 name 為 Ray Xu 的 document 列表
    • 顯示前 30 筆資料
db.posts.find(
  { 
    "name": "Ray Xu" 
  }
).limit(30).pretty()

4

  • 請查詢 name 為 Ray Xu
    • 顯示 100(含)個讚以上
    • 的前 30 筆 document 列表
    • 時間排序由新到舊
db.posts.find(
  { 
    "name": "Ray Xu",
    "likes": {
      "$gte": 100
    }
  }  
).sort(
    { 
      "createdAt": -1 
    }
).limit(30).pretty()

5

  • 請查詢 comments 超過 100 的 document 列表
    • 跳過前 30 筆
    • 再顯示 30 筆資料
db.posts.find(
  {
    "comments": {
      "$gt": 100
    }
  }
).limit(30).skip(30).pretty()

6

  • 尋找超夯熱門貼文,
    • 請查詢 likes 且(and) comments
    • 都超過 1,500(含) 的 document 列表
db.posts.find(
  {
    "$and": [
      {
        "likes": {
          "$gte": 1500
        }
      },
      {
        "comments": {
          "$gte": 1500
        }
      }    
    ]
  }
).pretty()

7

  • 尋找普通熱門貼文,
    • 請查詢 likes 或(or) comments
    • 超過 1,000(含) 的 document 列表
db.posts.find(
  {
    "$or": [
      {
        "likes": {
          "$gte": 1000
        }
      },
      {
        "comments": {
          "$gte": 1000
        }
      }    
    ]
  }
).pretty()

8

查詢 image 欄位為 null 的 document 列表

db.posts.find(
  {
    "image": {
      "$eq": null, 
      "$exists": true
    }
  }
).pretty()

  • 【會用到的語法項目】
    • 因為只過濾 null 會把不存在該欄位的文件也撈出來
  1. $eq

    • 判斷資料與 null 相等
  2. $exists"

    • 確定該欄位有存在

9

  • 隨意找 1 筆 document 資料
    • tags 欄位裡的陣列,新增 1 個新 tags 為 遊記
db.posts.updateOne(
  {
    "_id": ObjectId("62556b8d889a4ed3a69f2aae")
  },
  {
    "$push": {
      "tags": "遊記"
    }
  }
)
  • 【延伸狀況】
    • 假如陣列已經有 "遊記",陣列就會存成 2 筆同樣的標籤
      • => $addToSet

10

  • 將所有 tags 陣列裡的 感情 都移除
db.posts.updateMany(
  {},
  {
    "$pull": {
      "tags": "感情"
    }
  }
)

  • 【會使用到的語法項目】
db.collection.updateMany( <filter>, { arrayFilters: [ <filterdocument1>, ... ], } )
  1. updateMany()

  2. <filter> => {}

    • 直接更新全部文件
  3. arrayFilters

    • 可以用各種陣列方法操作陣列型態的欄位資料


TODO

PRE

  • new db
  • new collection => name posts
  • import .json to its documents

GO

  • write
  • check shell with compass
    • part1
    • part2

NOTE

  • point
  • doc
  • re'
    • #TODO