时间:2021-07-01 10:21:17 帮助过:8人阅读
一、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操作
标签:sketch -- 5.5 ace set str cti sep last