首页 > 代码库 > MongoDB学习第无篇 --- Update操作
MongoDB学习第无篇 --- Update操作
数据如下:
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 } } )
# 更新多条document db.inventory.updateMany( { "qty": { $lt: 50 } }, { $set: { "size.uom": "in", status: "P" }, $currentDate: { lastModified: true } } )
# 替换一条 document db.inventory.replaceOne( { item: "paper" }, { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] } )
二、python操作update语句
# 更新一条document db.inventory.update_one( {"item": "paper"}, { "$set": {"size.uom": "cm", "status": "P"}, "$currentDate": {"lastModified": True} } ) # 更新多条document db.inventory.update_many( {"qty": {"$lt": 50}}, { "$set": {"size.uom": "in", "status": "P"}, "$currentDate": {"lastModified": True} } ) # 替换document db.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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。