MongoDB 創建,更新和刪除文檔
插入並保存文檔
可以使用insert方法想目標集合插入一個文檔: > db.foo.insert({"bar": "baz"}) ,這個操作會給文檔自動添加一個“_id”(如果原來沒有的話),然後將其保存到MongoDB中。
批量插入
如果要想集合中插入多個文檔,使用批量插入會更加快一點。使用批量插入,可以將一組文檔傳遞給數據庫。insert 也可以批量插入
> db.foo.Insert([{"_id": 0}, {"_id": 1}, {"_id": 2}])
刪除文檔
現在數據庫中有些數據,要刪除它:
> db.foo.remove({})
上述命令會刪除foo集合中所有的文檔。但是不會刪除集合本身,也不會刪除集合的元信息。 db.tester.drop() 會把整個集合都被刪除,所有數據都不見了。
更新文檔
文檔替換
最簡單的更新使用一個新的文檔完全替換匹配的文檔。這適用於進行大規模模式遷移的情況。
> var joe = db.test.findOne({"name": "joe"}) > joe.relationships = {"friends": joe.friends, "enemies": joe.enemies} { "friends" : 32, "enemies" : 2 }> joe.username = joe.name joe > delete joe.friends true > delete joe.enemies true > delete joe.name true > db.test.update({"name": "joe"}, joe) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
使用修改器
通常文檔只會有一部分要更新。可以使用原子性的更新修改器,指定對文檔中的某些字段進行更新。更新修改器是一種特殊的鍵,用來指定復雜的更新操作,比如修改,增加或者刪除鍵,還可能是操作數組或者內嵌文檔。
$inc
增加計數器,原子性的完成這個增加
> db.analytics.findOne() { "_id" : ObjectId("5ccd9315fa7a6f5af4645efa"), "url" : "www.example.com", "pageviews" : 20 }
> db.analytics.update({"url": "www.example.com"}, {"$inc": {"pageviews": 1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.analytics.findOne() { "_id" : ObjectId("5ccd9315fa7a6f5af4645efa"), "url" : "www.example.com", "pageviews" : 21 }
使用修改器時,“_id”的值不能改變。(整個文檔替換是可以改變)其他鍵值,包括其他唯一索引的鍵,都是可以更改的。
$set
用來制定一個字段的值。如果這個字段不存在就創建他。這對更新模式或者增加用戶定義的鍵來說非常方便。
> db.users.findOne() { "_id" : ObjectId("5ccd954b7dd247457f95a420"), "name" : "joe", "age" : 30, "sex" : "male", "location" : "shanghai", "favourite book" : "War and Peace" } > db.users.update({"_id": ObjectId("5ccd954b7dd247457f95a420")}, {"$set": {"favourite book": "Green Eggs and Ham"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.findOne() { "_id" : ObjectId("5ccd954b7dd247457f95a420"), "name" : "joe", "age" : 30, "sex" : "male", "location" : "shanghai", "favourite book" : "Green Eggs and Ham" } > db.users.update({"_id": ObjectId("5ccd954b7dd247457f95a420")}, {"$set": {"favourite book": ["Green Eggs and Ham", "War and Peace"]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.findOne() { "_id" : ObjectId("5ccd954b7dd247457f95a420"), "name" : "joe", "age" : 30, "sex" : "male", "location" : "shanghai", "favourite book" : [ "Green Eggs and Ham", "War and Peace" ] }
可以用來修改值,也可以用來修改值的類型。
如果想刪除一個鍵可以使用 $unset
> db.users.update({"_id": ObjectId("5ccd954b7dd247457f95a420")}, {"$unset": {"favourite book": 1}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.users.findOne() { "_id" : ObjectId("5ccd954b7dd247457f95a420"), "name" : "joe", "age" : 30, "sex" : "male", "location" : "shanghai" }
用$set修改內嵌文檔
{ "_id" : ObjectId("5ccd977c7dd247457f95a421"), "title" : "A Blog Post", "content" : "...", "author" : { "name" : "joe", "email" : "[email protected]" } } > db.blog.posts.update({"author.name": "joe"}, {"$set": {"author.name": "joe schmoe"}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.blog.posts.findOne() { "_id" : ObjectId("5ccd977c7dd247457f95a421"), "title" : "A Blog Post", "content" : "...", "author" : { "name" : "joe schmoe", "email" : "[email protected]" } }
如果不用$修改器那麽:
db.blog.posts.update({"title": "A Blog Post"}, {"content": "11111"}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.blog.posts.find() { "_id" : ObjectId("5ccd977c7dd247457f95a421"), "content" : "11111" } { "_id" : ObjectId("5ccd98487dd247457f95a422"), "title" : "A Blog Post 022222", "content" : "...", "author" : { "name" : "joe", "email" : "[email protected]" } }
整個文檔會一起被替換掉。
數組修改器
添加元素:$push
> db.blog.posts.findOne() { "_id" : ObjectId("5ccd977c7dd247457f95a421"), "title" : "A Blog Post", "content" : "...", "author" : { "name" : "joe", "email" : "[email protected]" } } > db.blog.posts.update({"_id" : ObjectId("5ccd977c7dd247457f95a421")}, {"$push": {"comment": {"name": "joe", "email": "[email protected]", "content": "very good"}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.blog.posts.findOne() { "_id" : ObjectId("5ccd977c7dd247457f95a421"), "title" : "A Blog Post", "content" : "...", "author" : { "name" : "joe", "email" : "[email protected]" }, "comment" : [ { "name" : "joe", "email" : "[email protected]", "content" : "very good" } ] }
如果還想要加一條評論,繼續使用$push
> db.blog.posts.update({"_id" : ObjectId("5ccd977c7dd247457f95a421")}, {"$push": {"comment": {"name": "bob", "email": "[email protected]", "content": "very bad"}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.blog.posts.findOne() { "_id" : ObjectId("5ccd977c7dd247457f95a421"), "title" : "A Blog Post", "content" : "...", "author" : { "name" : "joe", "email" : "[email protected]" }, "comment" : [ { "name" : "joe", "email" : "[email protected]", "content" : "very good" }, { "name" : "bob", "email" : "[email protected]", "content" : "very bad" } ] }
如果要多條插入可以配合使用$each
> db.stock.ticker.update({"_id": 1}, {"$push": {"hourly": {"$each": [555, 666, 777]}}})
這樣就可以將三個新元素添加到數組中。如果只有一個那麽相當於沒有使用$each,只是簡單的$push操作。
$slice
還可以在添加數組時限制長度,可以使用 $slice 這樣可以得到一個最多包含N個元素的數組
> db.blog.posts.update({"title": "A Blog Post"}, {"$push": {"comment": {"$each": [{"1": "a"},{"2": "b"},{"3": "c"},{"7": "d"},{"6": "f"}], "$slice": -2}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.blog.posts.find() { "_id" : ObjectId("5ccd977c7dd247457f95a421"), "title" : "A Blog Post", "content" : "...", "author" : { "name" : "joe", "email" : "[email protected]" }, "comment" : [ { "7" : "d" }, { "6" : "f" } ] }> db.blog.posts.update({"title": "A Blog Post"}, {"$push": {"comment": {"$each": [{"1": "a"},{"2": "b"},{"3": "c"},{"7": "d"},{"6": "f"}], "$slice": -10}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.blog.posts.find() { "_id" : ObjectId("5ccd977c7dd247457f95a421"), "title" : "A Blog Post", "content" : "...", "author" : { "name" : "joe", "email" : "[email protected]" }, "comment" : [ { "7" : "d" }, { "6" : "f" }, { "1" : "a" }, { "2" : "b" }, { "3" : "c" }, { "7" : "d" }, { "6" : "f" } ] }
可以看到當$slice為-2時會添加 最後兩個值並且覆蓋原來的值,$slice的值必須是負整數。
$sort
排序
db.blog.posts.update({"title": "A Blog Post"}, {"$push": {"comment": {
"$each": [{"1": "a", "rating": 6.6},{"2": "b", "rating": 7.9},{"3": "c", "rating": 9.0}],
"$slice": -10,
"$sort": {"rating": -1}}}})
這樣會根據"rating"字段的值對數組中所有的對象進行排序, "$slice","$sort"必須與"$push"配合使用
將數組作為數據集使用,避免重復
如果想將數組作為數據集使用,保證數組內的元素不會重復。可以在查詢文檔中使用"$ne"來實現,例如 如果作者不在文檔中那麽就添加進去可以這麽做
> db.papers.update({"authors cited": {"$ne": "Richie"}},
... {"$push": {"authors cited": "Richie"}})
也可以使用 $addToSet 因為有時候$ne行不通
用法和$push相當 也可以搭配 $each使用 但是 $addToSet 可以避免添加重復的值
刪除數組中的元素
$pop $pull
{"$pop": {"key": 1}} // 從末尾刪除 {"$pop": {"key": -1}} //從頭部刪除
有時候需要基於特定的條件來刪除元素,而不僅僅是一句元素的位置這時候可以使用 $pull 例如有一個無序的待完成事項列表:
> db.lists.insert({"todo": ["dishes", "laundry", "dry cleaning"]})
如果我們完成了洗衣服可以用下面方式完成它
> db.lists.update({}, {"$pull": {"todo": "laundry"}})
upsert
如果我們有一個需求,如果沒有文檔那麽就創建他,如果存在了我們就在特定字段上 +1 具體寫法如下
> db.users.update({"url": "/blog"}, {"$inc": {"pageviews": 1}}, true )
最後一個參數就是他的開關
更新多個文檔,那麽只要update的第四個參數設置為true就可以了
findAndModify
使用
ps = db.runCommand({"findAndModify": "processes", "query": {"status": "READY"}, "sort": {"priority": -1}, "update": {"$set": {"status": "RUNNING"}}}).value do_something(ps)
- findAndModify
字符串,集合名 - query
查詢文檔,用於檢索文檔的條件 - sort
排序結果的條件 - update
修改器文檔(update和remove必須選一個) - new
布爾類型,表示返回更新前的文檔還是更新後的文檔,默認更新前 - fields
文檔中需要返回的字段(可選) - upsert
布爾類型,值為true是表示這是一個upsert.默認為false
MongoDB 創建,更新和刪除文檔