# mongo
## [mongoDB](https://docs.mongodb.com/manual/tutorial/query-documents/)
## [MongoDB tutorial point](https://www.tutorialspoint.com/mongodb/mongodb_update_document.htm)
### 筆記
- 下 find 的時候會因為 資料型態的不同而撈不到
```
db.collection.find({num:123})
和
db.collection.find({num:'123'})
找出來的結果是不一樣的
```
### Insert
```sql=
db.inventory.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
db.products.insert( { item: "card", qty: 15 } )
db.products.insert(
[
{ _id: 11, item: "pencil", qty: 50, type: "no.2" },
{ item: "pen", qty: 20 },
{ item: "eraser", qty: 25 }
]
)
```
### Query
約 15 分
#### question
(翻成mongo)
- SELECT * FROM inventory
- SELECT * FROM inventory WHERE status = "D"
- SELECT * FROM inventory WHERE status in ("A", "D")
- SELECT * FROM inventory WHERE status = "A" AND qty < 30
- SELECT * FROM inventory WHERE status = "A" OR qty < 30
- SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
(找出)
- 找出 size => h = 14, w = 21, uom = "cm"
- 找出 size.h => 小於5, size.uom = in, status = D
- 找出 size.uom = in
- 找出 tags = ["red", "blank"]
(operator)
- qty > 20
- qty = 20
- qty in 5, 15
- qty < 20
- qty <= 20
- quantity = 20 or price = 10
```sql=
mongo: db.inventory.find( {} )
sql: SELECT * FROM inventory
// Specify Equality
mongo:db.inventory.find( { status: "D" } )
sql: SELECT * FROM inventory WHERE status = "D"
// Specify Conditions
mongo:db.inventory.find( { status: { $in: [ "A", "D" ] } } )
sql:SELECT * FROM inventory WHERE status in ("A", "D")
// AND
mongo : db.inventory.find( { status: "A", qty: { $lt: 30 } } )
sql : SELECT * FROM inventory WHERE status = "A" AND qty < 30
// OR
mongo : db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
sql: SELECT * FROM inventory WHERE status = "A" OR qty < 30
// Specify AND as well as OR Conditions¶
mongo: db.inventory.find( {
status: "A",
$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )
sql : SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
// Match an Embedded/Nested Document
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
// Specify Equality Match on a Nested Field¶
db.inventory.find( { "size.uom": "in" } )
// Specify AND Condition¶
db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )
// Match an Array¶ - 會有順序與值是否一樣
db.inventory.find( { tags: ["red", "blank"] } )
```
[operator](https://docs.mongodb.com/manual/reference/operator/query/)
$gt - 大於
```
db.inventory.find( { qty: { $gt: 20 } } )
```
$gte - 大於等於
```
db.inventory.find( { qty: { $gte: 20 } } )
```
$in - 任何一個在裡面的值
```
db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
```
$lt - 小於
```
db.inventory.find( { qty: { $lt: 20 } } )
```
$lte - 小於等於
```
db.inventory.find( { qty: { $lte: 20 } } )
```
$or - 或
```
db.inventory.find( { $or: [ { quantity: { $lt: 20 } }, { price: 10 } ] } )
```
### Update Documents
```sql=
// Update a Single Document
db.inventory.updateOne(
{ item: "paper" },
{
$set: { "size.uom": "cm", status: "P" },
$currentDate: { lastModified: true }
}
)
// Update Multiple Documents
db.inventory.updateMany(
{ "qty": { $lt: 50 } },
{
$set: { "size.uom": "in", status: "P" },
$currentDate: { lastModified: true }
}
)
// Replace a Document
db.inventory.replaceOne(
{ item: "paper" },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)
```
### Delete Documents
```sql=
// Delete All Documents
db.inventory.deleteMany({})
// Delete All Documents that Match a Condition
db.inventory.deleteMany({ status : "A" })
// Delete Only One Document that Matches a Condition
db.inventory.deleteOne( { status: "D" } )
```
## Text Search¶
```sql=
```
## 其它
找出獨特值
```
db.bank.distinct('type')
```
## [jenssegers/laravel-mongodb](https://github.com/jenssegers/laravel-mongodb)
```php=
// Model
use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
class Book extends Model
{
use SoftDeletes; // 使用軟刪除
protected $collection = 'my_books_collection'; // table_name
protected $primaryKey = 'id'; // 主鍵
protected $connection = 'mongodb'; // 定義連線方式
protected $dates = ['birthday']; // 變成 carbon 去操作
}
// Basic Usage
$users = User::all(); // 撈全部
$user = User::find('517c43667db388101e00000f'); // 撈特定的 key
// query
$posts = Post::where('author.name', 'John')
->take(10)
->get();
$users =
User::where('age', '>', 18)
->where('name', '!=', 'John')
->get();
$users = User::whereIn('age', [16, 18, 20])->get();
$posts = Post::whereBetween('votes', [1, 100])->get();
$users = User::whereNull('age')->get();
```
## [mongoDB documentation](https://docs.mongodb.com/manual/tutorial/query-documents/)
{%hackmd BJrTq20hE %}
###### tags: `mongo`