--- title: Node 第一週主線任務 tags: Node直播班 - 2023 春季班 --- ## 第一週主線任務 ### 前置作業 1. 請建立一個 database,並建立一個 posts collection 2. 將此 [JSON](https://drive.google.com/file/d/1VCuWX2M6K-Du8pWlrcGImO_ux4Zwsa6v/view) 資料,透過 Compass 倒入到 posts collection ``` 貼文集合欄位介紹 - name:貼文姓名 - image:貼文圖片 - content:貼文內容 - likes:按讚數 - comments:留言數 - createdAt:發文時間 - type:貼文種類[friend(摯友)、group(社團)] - tags:貼文標籤 ``` ## 題庫 ### 課程範圍 1. 搜尋 name 欄位為 “Ray Xu” 的 document 列表 ```bash= db.posts.find({"name":"Ray Xu"}) ``` 2. 新增一筆 document,請全部欄位皆填寫 ```bash= db.posts.insertOne({ name:"albee", image:'http://dummyimage.com/126x100.png/ff4444/ffffff', content:'lobortis sapien sapien non mi integer ac neque duis bibendum morbi non', likes:666, comments:66, createdAt:'2022-04-10 22:53:44 UTC', type:'friend', tags: [ '感情', '幹話', '電影' ] }) ``` 3. 新增多筆 document,請全部欄位皆填寫 ```bash= { name: 'Rorke Andres', tags: [ '心情', '感情' ], type: 'group', image: 'http://dummyimage.com/126x100.png/ff4444/ffffff', createdAt: '2022-03-20 14:53:44 UTC', content: 'lobortis sapien sapien non mi integer ac neque duis bibendum morbi non', likes: 899, comments: 24 }, { name: 'Andres', tags: [ '感情' ], type: 'group', image: 'http://dummyimage.com/126x100.png/ff4444/ffffff', createdAt: '2022-03-20 14:53:44 UTC', content: 'sapien sapien non mi integer ac neque duis bibendum morbi non', likes: 1099, comments: 34 } ``` 4. 修改一筆 document,filter 條件請用 _id 指定其中一筆資料,content 欄位調整為測試資料 ```bash= db.posts.updateOne({ _id: ObjectId("643421f575a9de7e7c536c0b")}, {$set:{content:"測試資料"}}) ``` 5. 修改多筆 name 欄位為 "Ray Xu" 的 document 列表,content 欄位都調整為哈哈你看看你 ```bash= db.posts.updateMany( {name:"Ray Xu"}, {$set:{content:"哈哈你看看你"}} ) ``` 6. 刪除一筆 document,filter 條件請用 _id 任意指定其中一筆資料 ```bash= db.posts.deleteOne({"_id":ObjectId("625046bd478503fc8c62420d")}) ``` 7. 刪除多筆 document,filter 條件請用 type 為 group 的值,刪除所有社團貼文 ```bash= db.posts.deleteMany({type:"group"}) ``` 8. 刪除多筆 document,filter 條件為以下條件 - a. name:"Ray Xu" - b. likes: 500(含) 個讚以下 ```bash= db.posts.deleteMany({ name:"Ray Xu", likes:{ "$lte":500 } }) ``` 9. 查詢全部 posts 的 document 列表 ```bash= db.posts.find() ``` 10. 關鍵字搜尋 name 裡面含有 o 的 document 列表 ```bash= db.posts.find({name:/o/}) ``` 11. 查詢name 欄位為 "Ray Xu" ,filter 篩選出介於 500~1000(含) 個讚(大於等於 500、小於等於 1000) ```bash= db.posts.find({ name:"Ray Xu", likes:{ $gte:500, $lte:1000 } }) ``` 12. 查詢 comments 有大於等於 500 以上的 document 列表 ```bash= db.posts.find({ comments:{ $gte:500 } }) ``` 13. 查詢 tags 欄位,有 謎因 或(or) 幹話 的 document 列表 ```bash= db.posts.find({ $or: [ { tags:{ $in: [ "謎因", "幹話" ] } }] }) ``` 14. 查詢 tags 欄位,有 幹話 的 document 列表,需隱藏 _id 欄位 ```bash= db.posts.find( tags:{ $in: [ "幹話" ] },{ _id:0 } ) ``` 15. 請嘗試用 Mongo Shell 指令刪除全部 Documents ```bash= db.posts.remove({}) ``` ### 自主研究題 1. posts 所有 document 數量為?(回傳數字) ``` db.posts.find().count() > 1000 ``` 2. 請查詢 `name` 為 `Ray Xu` 的 document 列表,排序為由新到舊 ```bash= db.posts.find({name:"Ray Xu"}).sort({createdAt:-1}) ``` 3. 請查詢 `name` 為 `Ray Xu` 的 document 列表,顯示前 30 筆資料 ```bash= db.posts.find({name:"Ray Xu"}).limit(30) ``` 4. 請查詢 `name` 為 `Ray Xu` ,顯示100(含) 個讚以上的前 30 筆 document 列表,時間排序由新到舊 ```bash= db.posts.find({ name:"Ray Xu", likes:{$gte:100} }) .limit(30) .sort({createdAt:-1}) ``` 5. 請查詢 `comments` 超過 `100` 的 document 列表,跳過前 30 筆資料,再顯示 30 筆資料 ```bash= db.posts.find( comments:{ $gt:100 }) .skip(30) .limit(30) ``` 6. 尋找超夯熱門貼文,請查詢 `likes` 且(and) `comments` 都 `1,500(含)`以上的 document 列表 ```bash= db.posts.find({ $and:[ { likes:{ $gte:1500 }}, { comments:{ $gte:1500 }} ] }) ``` 7. 尋找普通熱門貼文,請查詢 `likes` 或(or) `comments` , `1,000(含)`以上 的 document 列表 ```bash= db.posts.find({ $or:[ { likes:{ $gte:1000 } }, { comments:{ $gte:1000 } } ] }) ``` 8. 查詢 `image` 欄位為 `null` 的 document 列表 ```bash= db.posts.find({ image:{ $type:10 } }) ``` 9. 隨意找一筆 document 資料,將 tags 欄位裡的陣列,新增一個新 `tags` 為 `遊記` ```bash= db.posts.updateOne({ _id:ObjectId("643421f575a9de7e7c536bfc") }, { $push: { tags:"遊記" } } ) ``` 10. 將所有 `tags` 陣列裡的 感情 都移除 ```bash= db.posts.updateMany({ tags:"感情" }, { $pull:{ tags:"感情" } } ) ```