MongoDB更新数据
插入一条数据
db.goods.insert({"productId":"100001","productName":"admin","salePrice":"200","checked":"1","productNum":"1","productImage":"1.jpg"})
发现productname输成了admin了
应该是小米蓝牙音箱
怎么办? 更新一下就行
db.goods.update({"productId":"100001"},{productName:"小米蓝牙音箱"})
成功返回: 查看一下写入成功了没
db.goods.findOne({"productId":"100001");
结果是
db.goods.findOne({"productId":"100001")
...
...
这是什么鬼? 这尼玛不是更新,是覆盖啊!!!!
先删除这条数据
db.goods.remove({"productName" : "小米蓝牙音箱" })
WriteResult({ "nRemoved" : 1 })
重新增加
db.goods.insert({"productId":"100001","productName":"admin","salePrice":"200","checked":"1","productNum":"1","productImage":"1.jpg"})
重新更新这条数据的productname
db.goods.update({"productId":"100001"},{$set:{productName:"小米蓝牙音箱"}})
成功了
db.goods.findOne()
{
"_id" : ObjectId("5a07cfa2d418356ec346b3d0"),
"productId" : "100001",
"productName" : "小米蓝牙音箱",
"salePrice" : "200",
"checked" : "1",
"productNum" : "1",
"productImage" : "1.jpg"
}