# Week01-Main ![](https://img.shields.io/badge/Created_Date-08_Apr_2022-orange) --- :::info - 【主線任務-W1】https://rpg.hexschool.com/task/273/show - 【題庫】https://hackmd.io/@hexschool/HJOX15NZ9 - 【課堂講義-W1】https://hackmd.io/@hexschool/HkHgw2cmq ::: --- --- ## 題庫-Part1 ### 1 :::info 搜尋 name 欄位為 “Ray Xu” 的 document 列表 ::: ```=sh db.posts.find( { "name": "Ray Xu" } ).pretty() ``` --- ### 2 :::info 新增 1 筆 document,請全部欄位皆填寫 ::: ```=sh 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 :::info 新增多筆 document,請全部欄位皆填寫 ::: ```=sh 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 :::info - 修改 1 筆 document - filter 條件請用 _id 指定 - content 欄位調整為 `測試資料` ::: ```=sh db.posts.updateOne( { "_id": ObjectId("62554608889a4ed3a69f26cf") }, { "$set": { "content": "測試資料" } } ) ``` --- ### 5 :::info - 修改多筆 name 欄位為 "Ray Xu" 的 document 列表 - content 欄位都調整為 `哈哈你看看你` ::: ```=sh db.posts.updateMany( { "name": "Ray Xu" }, { "$set": { "content": "哈哈你看看你" } } ) ``` - `#TODO` - [ ] 如果寫入這種顏文字,好像會漏掉某些特殊符號的字元 - `哈哈你看看你 (σ′▽‵)′▽‵)σ` --- ### 6 :::info - 刪除 1 筆 document - filter 條件請用 _id 任意指定其中 1 筆資料 ::: ```=sh db.posts.deleteOne( { "_id": ObjectId("62554608889a4ed3a69f26fd") } ) ``` --- ### 7 :::info - 刪除多筆 document - filter 條件請用 type 為 group 的值,刪除所有社團貼文 ::: ```=sh db.posts.deleteMany( { "type": "group" } ) ``` --- ### 8 :::info - 刪除多筆 document,filter 條件為以下 - a. name: "Ray Xu" - b. likes: 500(含)個讚以下 ::: ```=sh db.posts.deleteMany( { "$and": [ { "likes": { "$lte": 500 } }, { "name": "Ray Xu" } ] } ) ``` - 判斷可以也寫一起 ```=sh db.posts.deleteMany( { "name": "Ray Xu", "likes": { "$lte": 500 } } ) ``` --- ### 9 :::info 查詢全部 posts 的 document 列表 ::: ```=sh db.posts.find().pretty() ``` - `{}` 也可以 ```=sh db.posts.find({}).pretty() ``` --- ### 10 :::info 關鍵字搜尋 name 裡面含有 o 的 document 列表 ::: ```=sh db.posts.find( { "name": /o/ } ).pretty() ``` - 【語法】 - 注意 `value` 不用字串型別的引號 `''` --- ### 11 :::info - 查詢 name 欄位為 "Ray Xu" - filter 篩選出介於 500~1000(含)個讚 ::: ```=sh db.posts.find( { "$and": [ { "name": "Ray Xu" }, { "likes": { "$lte": 1000 } }, { "likes": { "$gt": 500 } } ] } ).pretty() ``` --- ### 12 :::info - 查詢 comments - 有超過 500(含)以上的 document 列表 ::: ```=sh db.posts.find( { "comments": { "$gte": 500 } } ).pretty() ``` --- ### 13 :::info - 查詢 tags 欄位 - 有 `謎因` 或(or) `幹話` 的 document 列表 ::: ```=sh db.posts.find( { "tags": { "$in": [ "謎因", "幹話" ] } } ).pretty() ``` - 也可以用 `$or` --- ### 14 :::info - 查詢 tags 欄位 - 有 `幹話` 的 document 列表,需隱藏 _id 欄位 ::: ```=sh db.posts.find( { "tags": { "$in": [ "幹話" ] } }, { "_id": 0 } ).pretty() ``` --- ### 15 :::info 請嘗試用 Mongo Shell 指令刪除全部 Documents ::: ```=sh db.posts.deleteMany({}) ``` - 【語法】 - 要放 `{}` - 沒放會跑錯誤 --- --- ## 題庫-Part2 ### 1 :::info posts 所有 document 數量為? ::: ```=sh db.posts.find().count() ``` --- ### 2 :::info - 請查詢 name 為 Ray Xu 的 document 列表 - 排序為由新到舊 ::: ```=sh db.posts.find( { "name": "Ray Xu" } ).sort( { "createdAt": -1 } ).pretty() ``` --- ### 3 :::info - 請查詢 name 為 Ray Xu 的 document 列表 - 顯示前 30 筆資料 ::: ```=sh db.posts.find( { "name": "Ray Xu" } ).limit(30).pretty() ``` --- ### 4 :::info - 請查詢 name 為 Ray Xu - 顯示 100(含)個讚以上 - 的前 30 筆 document 列表 - 時間排序由新到舊 ::: ```=sh db.posts.find( { "name": "Ray Xu", "likes": { "$gte": 100 } } ).sort( { "createdAt": -1 } ).limit(30).pretty() ``` --- ### 5 :::info - 請查詢 comments 超過 100 的 document 列表 - 跳過前 30 筆 - 再顯示 30 筆資料 ::: ```=sh db.posts.find( { "comments": { "$gt": 100 } } ).limit(30).skip(30).pretty() ``` --- ### 6 :::info - 尋找超夯熱門貼文, - 請查詢 likes 且(and) comments - 都超過 1,500(含) 的 document 列表 ::: ```=sh db.posts.find( { "$and": [ { "likes": { "$gte": 1500 } }, { "comments": { "$gte": 1500 } } ] } ).pretty() ``` --- ### 7 :::info - 尋找普通熱門貼文, - 請查詢 likes 或(or) comments - 超過 1,000(含) 的 document 列表 ::: ```=sh db.posts.find( { "$or": [ { "likes": { "$gte": 1000 } }, { "comments": { "$gte": 1000 } } ] } ).pretty() ``` --- ### 8 :::info 查詢 image 欄位為 null 的 document 列表 ::: ```=sh db.posts.find( { "image": { "$eq": null, "$exists": true } } ).pretty() ``` --- - 【會用到的語法項目】 - 因為只過濾 `null` 會把不存在該欄位的文件也撈出來 1. `$eq` - 判斷資料與 `null` 相等 2. `$exists"` - 確定該欄位有存在 > > - https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields/#equality-filter > - https://stackoverflow.com/questions/4057196/how-do-you-query-for-is-not-null-in-mongo > - `#TODO` > - [ ] 尋找反向的筆記 > --- ### 9 :::info - 隨意找 1 筆 document 資料 - 將 `tags` 欄位裡的陣列,新增 1 個新 tags 為 `遊記` ::: ```=sh db.posts.updateOne( { "_id": ObjectId("62556b8d889a4ed3a69f2aae") }, { "$push": { "tags": "遊記" } } ) ``` - 【延伸狀況】 - 假如陣列已經有 "遊記",陣列就會存成 2 筆同樣的標籤 - => `$addToSet` --- ### 10 :::info - 將所有 `tags` 陣列裡的 `感情` 都移除 ::: ```=sh db.posts.updateMany( {}, { "$pull": { "tags": "感情" } } ) ``` --- - 【會使用到的語法項目】 ```shell= db.collection.updateMany( <filter>, { arrayFilters: [ <filterdocument1>, ... ], } ) ``` 1. `updateMany()` 2. `<filter>` => `{}` - 直接更新全部文件 3. `arrayFilters` - 可以用各種陣列方法操作陣列型態的欄位資料 > - https://www.mongodb.com/docs/manual/reference/method/db.collection.updateMany/#syntax > - https://www.mongodb.com/docs/manual/reference/operator/update/positional-filtered/#std-label-positional-update-arrayFilters --- --- ## TODO ### PRE - [x] new db - [x] new collection => name `posts` - [x] import `.json` to its documents ### GO - [x] write - [x] check shell with compass - [x] part1 - [x] part2 ### NOTE - [ ] point - [ ] doc - [ ] re' - [ ] `#TODO` --- <!-- ### 8 :::info ::: ```=sh ``` --> {%hackmd RAtn1iEBRO2tCbZeA-NylQ %} ---