# 第一週主線任務
## 前置作業
1. 請建立一個 database,並建立一個 posts collection
2. 將此 JSON 資料,透過 Compass 倒入到 posts collection
```
貼文集合欄位介紹
- name:貼文姓名
- image:貼文圖片
- content:貼文內容
- likes:按讚數
- comments:留言數
- createdAt:發文時間
- type:貼文種類[friend(摯友)、group(社團)]
- tags:貼文標籤
```
1. 搜尋 name 欄位為 "Ray Xu" 的 document 列表
```shell=
db.posts.find({"name":"Ray Xu"})
```
2. 新增一筆 document,請全部欄位皆填寫
```shell=
db.posts.insertOne( {
"name": 'shuantt',
"tags": [ '程式', '學習' ],
"type": 'group',
"image": 'http://dummyimage.com/126x100.png/ff4444/ffffff',
"createdAt": '2022-03-20 14:53:44 UTC',
"content": '留言內容234920523095',
"likes": 10000,
"comments": 100
})
```
3. 新增多筆 document,請全部欄位皆填寫
```shell=
db.posts.insertMany([{
"name": 'shuantt',
"tags": [ '程式', '學習' ],
"type": 'group',
"image": 'http://dummyimage.com/126x100.png/ff4444/ffffff',
"createdAt": '2022-03-20 14:53:44 UTC',
"content": '留言內容234920523095',
"likes": 10000,
"comments": 100
},
{
"name": 'shuantt',
"tags": [ '遊戲', '娛樂' ],
"type": 'friend',
"image": 'http://dummyimage.com/126x100.png/ff4444/ffffff',
"createdAt": '2024-04-20 10:00:00 UTC',
"content": '哈囉,你好嗎?我很好。',
"likes": 50,
"comments": 13
},
])
```
4. 修改一筆 document,filter 條件請用 _id 指定其中一筆資料,content 欄位調整為測試資料
```shell=
db.posts.updateOne(
{ _id: ObjectId('6616a9f6bded8cbb9d2f4c59') },
{ $set: { content: '測試資料' } }
);
```
5. 修改多筆 name 欄位為 "Ray Xu" 的 document 列表,content 欄位都調整為哈哈你看看你
```shell=
db.posts.updateMany(
{ name: 'Ray Xu' },
{ $set: { content: '哈哈哈你看看你' } }
);
```
6. 刪除一筆 document,filter 條件請用 _id 任意指定其中一筆資料
```shell=
db.posts.deleteOne({ "_id": ObjectId('6616a7aa763be6a78d04e4bf')})
```
7. 刪除多筆 document,filter 條件請用 type 為 group 的值,刪除所有社團貼文
```shell=
db.posts.deleteMany({"type":"group"})
```
8. 刪除多筆 document,filter 條件為以下條件
a. name:"Ray Xu"
b. likes: 500(含) 個讚以下
```shell=
db.posts.deleteMany({
"name":"Ray Xu",
"likes":{ $lte: 500 }
})
```
9. 查詢全部 posts 的 document 列表
```shell=
db.posts.find()
```
10. 關鍵字搜尋 name 裡面含有 o 的 document 列表
```shell=
db.posts.find({"name":/o/})
```
11. 查詢name 欄位為 "Ray Xu" ,filter 篩選出介於 500~1000(含) 個讚(大於等於 500、小於等於 1000)
```shell=
db.posts.find({
"name":"Ray Xu",
"likes":{
$gte:500,
$lte:1000
}
})
```
12. 查詢 comments 有大於等於 500 以上的 document 列表
```shell=
db.posts.find({
comments:{
$gte:500
}
})
```
13. 查詢 tags 欄位,有 謎因 或(or) 幹話 的 document 列表
```shell=
db.posts.find({
tags: {$in: ['謎因', '幹話']}
})
```
14. 查詢 tags 欄位,有 幹話 的 document 列表,需隱藏 _id 欄位
```shell=
db.posts.find(
{
tags: {
$in: ['幹話']
}
},
{ _id: 0 }
);
```
15. 請嘗試用 Mongo Shell 指令刪除全部 Documents
```shell=
db.posts.deleteMany({})
```
## 自主研究題
1. posts 所有 document 數量為?(回傳數字)
```shell=
db.posts.find().count()
```
2. 請查詢 name 為 Ray Xu 的 document 列表,排序為由新到舊
```shell=
db.posts.find({"name":"Ray Xu"}).sort({"createdAt": -1})
```
```shell=
db.posts.aggregate([
{
$match: {"name": 'Ray Xu'}
},
{
$sort: {"createdAt": -1}
}
])
```
3. 請查詢 name 為 Ray Xu 的 document 列表,顯示前 30 筆資料
```shell=
db.posts.find({ name: 'Ray Xu' }).limit(30);
```
```shell=
db.posts.aggregate([
{
$match: { name: 'Ray Xu' },
},
{
$limit: 30,
},
])
```
4. 請查詢 name 為 Ray Xu ,顯示100(含) 個讚以上的前 30 筆 document 列表,時間排序由新到舊
```shell=
db.posts.find({name:'Ray Xu',likes:{$gte: 100}}).limit(30).sort({createdAt:-1,});
```
```shell=
db.posts.aggregate([
{
$match: {
name: 'Ray Xu',
likes: {$gte:100},
},
},
{
$limit: 30,
},
{
$sort:{createdAt:-1}
}
])
```
5. 請查詢 comments 超過 100 的 document 列表,跳過前 30 筆資料,再顯示 30 筆資料
```shell=
db.posts.find({comments:{$gt:100}}).skip(30).limit(30)
```
```shell=
db.posts.aggregate([
{
$match: {comments: { $gt: 100 }},
},
{
$skip: 30
},
{
$limit: 30
}
])
```
6. 尋找超夯熱門貼文,請查詢 likes 且(and) comments 都 1,500(含)以上的 document 列表
```shell=
db.posts.find({
$and: [
{likes: { $gte: 1500 }},
{comments: { $gte: 1500 }}
]
})
```
```shell=
## 使用 $match 已預設使用 $and
db.posts.aggregate([
{
$match: {
comments: { $gte: 1500 },
likes: { $gte: 1500 }
}
}
])
```
7. 尋找普通熱門貼文,請查詢 likes 或(or) comments , 1,000(含)以上 的 document 列表
```shell=
db.posts.find({
$or: [
{likes: { $gte: 1000 }},
{comments: { $gte: 1000 }}
]
});
```
8. 查詢 image 欄位為 null 的 document 列表
```shell=
db.posts.find({image:null})
```
9. 隨意找一筆 document 資料,將 tags 欄位裡的陣列,新增一個新 tags 為 遊記
```shell=
db.posts.updateOne(
{
_id: ObjectId('66174868763be6a78d04e797')
},
{
$push: {tags: '遊記'}
}
)
```
10. 將所有 tags 陣列裡的 感情 都移除
```shell=
db.posts.updateMany(
{ },
{
$pull: {tags: '遊記'}
}
);
```