---
# System prepended metadata

title: 旅館訂房網(hotel) - database

---

# 旅館訂房網(hotel) - database

##  房型(rooms) -  collections

``` =javascript
[
    {
        "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":["現金"]
    }
]
```

### 尋找集合裡面的資料 db.collections.find()

```
## 一般搜尋
- db.集合名稱(collections name).find()
- 尋找對應屬性：db.collections.find({屬性名稱:屬性值})
- 數值區間：db.collections.find({屬性名稱:{$lte:400}})
- 複合條件：db.collections.find({屬性名稱:{$lte:400},rating:{$gte:4.8}})
- 關鍵字搜尋：db.rooms.find({"name":/雙/})

## 常用語法
- 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:[{},{}]})`

```=javascript
db.rooms.find( 
    { $and:
        [
            {price:{$lte:1500}},
            {rating:{$gte:4.5}}
        ] 
    } 
)
```