標籤:mongodb 運算元組元素
persons文檔的資料如下:
> db.persons.find()
{ "_id" : 2, "name" : 2 }
{ "_id" : 3, "name" : 3 }
> db.persons.update({_id:4},{_id:4,name:4})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.persons.find()
{ "_id" : 2, "name" : 2 }
{ "_id" : 3, "name" : 3 }
做完update操作,依然看不到_id:4的記錄,因為update方法需要一個true指標,才會對查詢不到的記錄進行insert操作:
> db.persons.update({_id:4},{_id:4,name:4},true)
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 4 })
> db.persons.find()
{ "_id" : 2, "name" : 2 }
{ "_id" : 3, "name" : 3 }
{ "_id" : 4, "name" : 4 }
現有需求,將persons文檔中的name為"3"的改成"33"
> db.persons.update({name:"3"},{$set:{name:"33"}},false,true)
false含義:若查不到name:"33"的鍵值對,則不執行插入操作,true含義:表示是批次更新
為persons增加age:"88"屬性
> db.persons.update({name:"4"},{$set:{age:"88"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4", "age" : "88" }
將age加2
> db.persons.update({name:"4"},{$inc:{age:2}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4", "age" : 90 }
將age屬性拿走:
> db.persons.update({age:90},{$unset:{age:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
給persons文檔增加一條記錄,_id為5
> db.persons.insert({_id:5,name:5,books:[]})
WriteResult({ "nInserted" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "name" : 5, "books" : [ ] }
給books數組增加一個元素:"js"和"extjs4.0"
> db.persons.update({_id:5},{$push:{books:"js"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "name" : 5, "books" : [ "js" ] }
> db.persons.update({_id:5},{$push:{books:"extjs4.0"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "name" : 5, "books" : [ "js", "extjs4.0" ] }
建立一個新的classes數組:
> db.persons.update({_id:5},{$push:{classes:"01class"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "name" : 5, "books" : [ "js", "extjs4.0" ], "classes" : [ "01class"
] }
為calsses數組一次增加幾個元素:
> db.persons.update({_id:5},{$pushAll:{classes:["02class","03class","04class"]}}
)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "name" : 5, "books" : [ "js", "extjs4.0" ], "classes" : [ "01class"
, "02class", "03class", "04class" ] }
刪除_id是5的記錄,並建立一個新的_id是5的記錄,使用$addToSet,此命令會檢查要添加的元素在數組裡面是不是存在,存在就不會再添加,否則會添加:
> db.persons.remove({_id:5})
WriteResult({ "nRemoved" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
> db.persons.insert({_id:5,books:["js"]})
WriteResult({ "nInserted" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "js" ] }
> db.persons.update({_id:5},{$addToSet:{books:"js"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "js" ] }
> db.persons.update({_id:5},{$addToSet:{books:"java"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "js", "java" ] }
> db.persons.update({_id:5},{$addToSet:{books:"mongo"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "js", "java", "mongo" ] }
刪除books數組的第一個元素:"js",使用$pop命令:
> db.persons.update({_id:5},{$pop:{books:-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "java", "mongo" ] }
> db.persons.update({_id:5},{$pop:{books:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "java" ] }
-1代表第一個元素,1代表最後一個元素
也可以使用pull命令一次刪除一個指定的元素:
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "java", "mongo", "js" ] }
> db.persons.update({_id:5},{$pull:{books:"js"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ "java", "mongo" ] }
$pullAll命令可以一次指定多個要刪除的元素:
> db.persons.update({_id:5},{$pullAll:{books:["java","mongo"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ ] }
建立一條新的記錄_id為6:
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ ] }
{ "_id" : 6, "books" : [ { "type" : "js", "name" : "extjs4.0" }, { "type" : "db"
, "name" : "mongodb" }, { "type" : "js", "name" : "jquery" } ] }
為type是js的books元素添加pens:"too long"屬性,使用.符號一定使用雙引號引用
> db.persons.update({"books.type":"js"},{$set:{"books.$.author":"tom"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4, "name" : "4" }
{ "_id" : 5, "books" : [ ] }
{ "_id" : 6, "books" : [ { "type" : "js", "name" : "extjs4.0", "author" : "tom"
}, { "type" : "db", "name" : "mongodb" }]
}
db.persons.update({"books.type":"js"},{$set:{"books.$.pens":"too long"}})
判斷數組元素執行插入操作,重複的元素不會被插入第二次:
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4 }
{ "_id" : 5, "books" : [ "js" ] }
> db.persons.update({_id:5},{$addToSet:{books:{$each:["js","db","java"]}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : 2, "name" : "33" }
{ "_id" : 3, "name" : "33" }
{ "_id" : 4 }
{ "_id" : 5, "books" : [ "js", "db", "java" ] }
mongodb 批次更新 操作文檔的數組鍵