2020-04-27

MongoDB 新增資料

MongoDB 新增資料

同樣使用 mongo shell 操作,新增單筆使用 insertOne():

> use test switched to db test > db.inventory.insertOne({ ... "item": "notebook1", ... "qty": 50, ... "size": { ... "h": 8.5, ... "w": 11, ... "uom": "in" ... }, ... "status": "A" ... }) { "acknowledged" : true, "insertedId" : ObjectId("5ea6f2ab40e9db6af06a231a") }

新增多筆使用 insertMany():

> db.inventory.insertMany([ ... { ... "item": "notebook2", ... "qty": 10 ... }, ... { ... "item": "notebook3", ... "qty": 13 ... } ... ]) { "acknowledged" : true, "insertedIds" : [ ObjectId("5ea6f53240e9db6af06a231b"), ObjectId("5ea6f53240e9db6af06a231c") ] }

尋找剛剛新增的兩筆:

> db.inventory.find({item:/^notebook[23]/}).pretty() { "_id" : ObjectId("5ea6f53240e9db6af06a231b"), "item" : "notebook2", "qty" : 10 } { "_id" : ObjectId("5ea6f53240e9db6af06a231c"), "item" : "notebook3", "qty" : 13 }

沒有留言:

FB 留言