MongoDB學習第無篇 --- Update操作

來源:互聯網
上載者:User

標籤:sketch   --   5.5   ace   set   str   cti   sep   last   

資料如下:

db.inventory.insertMany( [   { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },   { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },   { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },   { item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },   { item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }]);

 

一、shell執行update語句

# 更新一條document
db.inventory.updateOne( { item: "paper" }, { $set: { "size.uom": "cm", status: "P" }, $currentDate: { lastModified: true } })
# 更新多條documentdb.inventory.updateMany( { "qty": { $lt: 50 } }, { $set: { "size.uom": "in", status: "P" }, $currentDate: { lastModified: true } })
# 替換一條 documentdb.inventory.replaceOne( { item: "paper" }, { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] })

 

二、python操作update語句

# 更新一條documentdb.inventory.update_one(    {"item": "paper"},    {    "$set": {"size.uom": "cm", "status": "P"},      "$currentDate": {"lastModified": True}   })# 更新多條documentdb.inventory.update_many(    {"qty": {"$lt": 50}},    {     "$set": {"size.uom": "in", "status": "P"},        "$currentDate": {"lastModified": True}    })# 替換documentdb.inventory.replace_one(    {"item": "paper"},    {       "item": "paper",       "instock": [         {"warehouse": "A", "qty": 60},         {"warehouse": "B", "qty": 40}    ]    })

 

 

三、Java操縱update語句

#  更新一條document
collection.updateOne(eq("item", "paper"),combine(set("size.uom", "cm"), set("status", "P"), currentDate("lastModified")));
#  更新多條document
collection.updateMany(lt("qty", 50),combine(set("size.uom", "in"), set("status", "P"), currentDate("lastModified"))); 
#  替換document
collection.replaceOne(eq("item", "paper"),Document.parse("{ item: ‘paper‘, instock: [ { warehouse: ‘A‘, qty: 60 }, { warehouse: ‘B‘, qty: 40 } ] }"));

 

MongoDB學習第無篇 --- Update操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.