mongod --dbpath "data 資料夾"
mongod --dbpath "DB資料夾路徑" --logpath "LOG資料夾路徑/mongo.log"
mongo 與 mongod 指令差異
1. mongod:server 端開啟伺服器應用
2. mongo: 客戶端,直連造訪您開啟的 server 應用
資料庫名稱
db.rooms.insertOne(
{name:"單人房",price:1000,rating:4.5}
)
db.rooms.insertMany(
[
{
"rating": 4.3,
"price": 1500,
"name": "豪華單人房"
},
{
"rating": 4.8,
"price": 2000,
"name": "雙人房"
}
]
)
## 一般搜尋
- db.集合名稱(collections name).find()
- 尋找對應屬性:db.collections.find({屬性名稱:屬性值})
- 模糊搜尋:db.rooms.find({"name":/雙/})
- 數值區間:db.collections.find({屬性名稱:{$lte:400}})
- 複合條件:db.collections.find({屬性名稱:{$lte:400},rating:{$gte:4.8}})
## 常用語法
- project 保護欄位:db.rooms.find({"name":/雙/},{"_id":0})
- 搜尋陣列裡面的值:db.rooms.find({"payment":{$in:["信用卡"]}})
$eq | 等於 |
---|---|
$ne | 不等於 |
$gt | 大於 |
$lt | 小於 |
$gte | 大於等於 |
$lte | 小於等於 |
$in | 存在某個值 |
$nin | 不存在某個值 |
邏輯運算子:or 或 and
$or:find({$or:[{},{}]})
db.rooms.find(
{ $and:
[
{price:{$lte:1500}},
{rating:{$gte:4.5}}
]
}
)
replaceOne()
,只能替換整個文件,updateOne()
則允許更新欄位。
db.rooms.updateOne(
{"_id":ObjectId("621edf99a20aa7506a116f9a")}
,{$set:{name:"修改雙人房"}})
db.rooms.updateMany(
{rating:{$gt:4.5}},
{$set:{price:300}}
)
db.collection.replaceOne(filter, replacement, options)
db.rooms.replaceOne(
{"_id":ObjectId("621edf99a20aa7506a116f9a")},
{name:"yes~"})
db.rooms.deleteMany(
{
rating:{$gte:4.3}
})
[
{
"rating": 4.5,
"price": 1000,
"name": "標準單人房",
"payment":["信用卡","ATM"]
},
{
"rating": 4.3,
"price": 1500,
"name": "豪華單人房",
"payment":["信用卡","ATM"]
},
{
"rating": 4.8,
"price": 2000,
"name": "標準雙人房",
"payment":["信用卡","ATM"]
},
{
"rating": 4.7,
"price": 2500,
"name": "豪華雙人房",
"payment":["ATM"]
},
{
"rating": 4.0,
"price": 3000,
"name": "標準四人房",
"payment":["現金"]
},
{
"rating": 3.5,
"price": 10000,
"name": "總統套房",
"payment":["現金"]
}
]
find 語法有兩個參數(parameter),分別是 Query 和 Projection,兩個非選填,如果都不設定,
1. 顯示全部:db.集合名稱(collections name).find()
2. 顯示第一個符合條件:db.collections.findOne()
`db.collection.find({price:{$gt: 3000}, {title:1})`
posts
collectionposts
collection貼文集合欄位介紹
- name:貼文姓名
- imageUrl:貼文圖片
- content:貼文內容
- likes:按讚數
- comment:留言數
- createdAt:發文時間
- type:貼文種類[fan(粉絲)、group(社團)]
- tags:貼文標籤
db.posts.find({"name":"Ray Xu"})
新增一筆 document,欄位皆必填
新增多筆 document,欄位皆必填
修改一筆 document,filter 條件請用 _id
指定其中一筆資料,content
欄位調整為測試資料
修改多筆 name
欄位為 "Ray Xu"
的 document 列表,content
欄位都調整為哈哈你看看你
刪除一筆 document,filter 條件請用 _id
任意指定其中一筆資料
刪除多筆 document,filter 條件請用 type
為 group
的值,刪除所有社團貼文
刪除多筆 document,filter 條件為以下條件
a. name
:Ray Xu
b. likes
: 500(含) 個讚以下
查詢全部 post
的 document 列表
關鍵字搜尋 name
裡面含有 o
的 docuemnt 列表
查詢name
欄位為 "Ray Xu"
,filter 篩選出介於 500~1000(含) 個讚
查詢 comment
有超過 500(含)以上的 document 列表
查詢 tag
欄位,有 謎因
或(or) 幹話
的 docuemnt 列表
查詢 tag
欄位,有 幹話
的 docuemnt 列表,需隱藏 _id
欄位
請嘗試用 Mongo Shell 指令刪除全部 Documents
db.posts.find().count()
請查詢 name
為 Ray Xu
的 document 列表,排序為由新到舊
請查詢 name
為 Ray Xu
的 document 列表,顯示前 30 筆資料
請查詢 name
為 Ray Xu
,顯示100(含) 個讚以上的前 30 筆 document 列表,時間排序由新到舊
請查詢 comment
超過 100
的 document 列表,跳過前 30 筆資料,再顯示 30 筆資料
尋找超夯熱門貼文,請查詢 likes
且(and) comments
都超過 1,500(含)
的 document 列表
尋找普通熱門貼文,請查詢 likes
或(or) comments
超過 1,000(含)
的 document 列表
查詢 image
欄位為 null
的 document 列表
隨意找一筆 document 資料,將 tag
欄位裡的陣列,新增一個新 tag 為 遊記
將所有 tag
陣列裡的 感情
都移除