MongoDB 集合的插入、更新、刪除操作

feelpurple發表於2016-06-28
--建立集合

> db.restaurants.insert({})
2016-06-03T16:05:08.428-0700 I COMMAND  [conn4] command test.restaurants command: insert { insert: "restaurants", documents: [ { _id: ObjectId('57520d24f435fd6c606d67a0') } ], ordered: true } 

ninserted:1 keyUpdates:0 writeConflicts:0 numYields:0 reslen:25 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 1, W: 1 } }, Collection: { acquireCount: { w: 1, W: 

1 } } } protocol:op_command 140ms
WriteResult({ "nInserted" : 1 })
> show collections;
restaurants
> db.restaurants.find()
{ "_id" : ObjectId("57520d24f435fd6c606d67a0") }

--查詢集合裡面的文件

> db.restaurants.find()
{ "_id" : ObjectId("574466fc4ecd57851a197423"), "address" : { "street" : "2 Avenue", "zipcode" : "10075", "building" : "1480", "coord" : [ -73.9557413, 40.7720266 ] }, "borough" : "Manhattan", 
"cuisine" : 
"Italian", "grades" : [ { "date" : ISODate("2014-10-01T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-01-16T00:00:00Z"), "grade" : "B", "score" : 17 } ], "name" : "Vella", 
"restaurant_id" : "41704620" }

> db.restaurants.findOne()
{
"_id" : ObjectId("574466fc4ecd57851a197423"),
"address" : {
"street" : "2 Avenue",
"zipcode" : "10075",
"building" : "1480",
"coord" : [
-73.9557413,
40.7720266
]
},
"borough" : "Manhattan",
"cuisine" : "Italian",
"grades" : [
{
"date" : ISODate("2014-10-01T00:00:00Z"),
"grade" : "A",
"score" : 11
},
{
"date" : ISODate("2014-01-16T00:00:00Z"),
"grade" : "B",
"score" : 17
}
],
"name" : "Vella",
"restaurant_id" : "41704620"
}

--刪除集合

> db.restaurants.drop()
2016-06-03T16:02:51.910-0700 I COMMAND  [conn4] CMD: drop test.restaurants
2016-06-03T16:02:52.073-0700 I COMMAND  [conn4] command test.restaurants command: drop { drop: "restaurants" } keyUpdates:0 writeConflicts:0 numYields:0 reslen:64 locks:{ Global: { acquireCount: { 
r: 1, w: 1 } }, Database: { acquireCount: { W: 1 } } } protocol:op_command 152ms
true
> db.restaurants.find()

--插入單條文件

> show collections;
restaurants
> db.restaurants.find()
2016-06-03T08:26:09.624-0700 I COMMAND  [conn4] command test.restaurants command: find { find: "restaurants", filter: {} } planSummary: COLLSCAN keysExamined:0 docsExamined:0 cursorExhausted:1 
keyUpdates:0 writeConflicts:0 numYields:1 nreturned:0 reslen:107 locks:{ Global: { acquireCount: { r: 4 } }, Database: { acquireCount: { r: 2 } }, Collection: { acquireCount: { r: 2 } } } 
protocol:op_command 186ms
> db.restaurants.find()
> db.restaurants.insert({"bar" : "baz"})
2016-06-03T08:27:32.007-0700 I COMMAND  [conn4] command test.restaurants command: insert { insert: "restaurants", documents: [ { _id: ObjectId('5751a1e3f435fd6c606d679f'), bar: "baz" } ], ordered: 
true } ninserted:1 keyUpdates:0 writeConflicts:0 numYields:0 reslen:25 locks:{ Global: { acquireCount: { r: 1, w: 1 } }, Database: { acquireCount: { w: 1 } }, Collection: { acquireCount: { w: 1 } } 
} protocol:op_command 210ms
WriteResult({ "nInserted" : 1 })
> db.restaurants.find()
{ "_id" : ObjectId("5751a1e3f435fd6c606d679f"), "bar" : "baz" }

--批次插入文件

> db.restaurants.insert([{"_id" : 0}, {"_id" : 1}, {"_id" : 2}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.restaurants.find()
{ "_id" : ObjectId("5751a1e3f435fd6c606d679f"), "bar" : "baz" }
{ "_id" : 0 }
{ "_id" : 1 }
{ "_id" : 2 }

--更新文件

> db.restaurants.find()
{ "_id" : ObjectId("57520d24f435fd6c606d67a0") }
> pos=({"phone":18210000000,"Adreess" : "Beijing Haidian Street"})
{ "phone" : 18210000000, "Adreess" : "Beijing Haidian Street" }

> db.restaurants.update({"_id" : ObjectId("57520d24f435fd6c606d67a0")}, pos)
2016-06-03T16:14:38.871-0700 I WRITE    [conn4] update test.restaurants query: { _id: ObjectId('57520d24f435fd6c606d67a0') } update: { phone: 18210000000.0, Adreess: "Beijing Haidian Street" } 

keysExamined:1 docsExamined:1 nMatched:1 nModified:1 keyUpdates:0 writeConflicts:0 numYields:1 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 2 } }, Collection: { 
acquireCount: { w: 2 } } } 142ms
2016-06-03T16:14:38.871-0700 I COMMAND  [conn4] command test.$cmd command: update { update: "restaurants", updates: [ { q: { _id: ObjectId('57520d24f435fd6c606d67a0') }, u: { phone: 18210000000.0, 
Adreess: "Beijing Haidian Street" }, multi: false, upsert: false } ], ordered: true } keyUpdates:0 writeConflicts:0 numYields:0 reslen:40 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: 

{ acquireCount: { w: 2 } }, Collection: { acquireCount: { w: 2 } } } protocol:op_command 142ms
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.restaurants.find()
{ "_id" : ObjectId("57520d24f435fd6c606d67a0"), "phone" : 18210000000, "Adreess" : "Beijing Haidian Street" }

> db.people.find()
{ "_id" : ObjectId("57522660f435fd6c606d67a1"), "name" : "joe", "age" : 65 }
{ "_id" : ObjectId("57522683f435fd6c606d67a2"), "name" : "joe", "age" : 20 }
{ "_id" : ObjectId("5752268bf435fd6c606d67a3"), "name" : "joe", "age" : 49 }
> joe = db.people.findOne({"name" : "joe", "age" : 20});
{ "_id" : ObjectId("57522683f435fd6c606d67a2"), "name" : "joe", "age" : 20 }
> joe.age++;
20
> db.people.update({"_id" : ObjectId("57522683f435fd6c606d67a2")}, joe)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("57522660f435fd6c606d67a1"), "name" : "joe", "age" : 65 }
{ "_id" : ObjectId("57522683f435fd6c606d67a2"), "name" : "joe", "age" : 21 }
{ "_id" : ObjectId("5752268bf435fd6c606d67a3"), "name" : "joe", "age" : 49 }

--刪除文件

> db.restaurants.remove({"_id" : 1})
WriteResult({ "nRemoved" : 1 })

--使用修改器
--"$set"用來指定一個欄位的值。如果這個欄位不存在,則建立它。這對更新模式或者增加使用者定義的鍵來說非常方便。

> db.people.find()
{ "_id" : ObjectId("57522660f435fd6c606d67a1"), "name" : "joe", "age" : 65 }
{ "_id" : ObjectId("57522683f435fd6c606d67a2"), "name" : "joe", "age" : 21 }
{ "_id" : ObjectId("5752268bf435fd6c606d67a3"), "name" : "joe", "age" : 49 }
> db.people.update({"_id" : ObjectId("57522660f435fd6c606d67a1")},{"$set" : {"favorite book" : "War and Peace"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("57522660f435fd6c606d67a1"), "name" : "joe", "age" : 65, "favorite book" : "War and Peace" }
{ "_id" : ObjectId("57522683f435fd6c606d67a2"), "name" : "joe", "age" : 21 }
{ "_id" : ObjectId("5752268bf435fd6c606d67a3"), "name" : "joe", "age" : 49 }
> db.people.update({"_id" : ObjectId("57522683f435fd6c606d67a2")},{"$set" : {"favorite book" : "Green Eggs and Ham"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("57522660f435fd6c606d67a1"), "name" : "joe", "age" : 65, "favorite book" : "War and Peace" }
{ "_id" : ObjectId("57522683f435fd6c606d67a2"), "name" : "joe", "age" : 21, "favorite book" : "Green Eggs and Ham" }
{ "_id" : ObjectId("5752268bf435fd6c606d67a3"), "name" : "joe", "age" : 49 }
> db.people.update({"_id" : ObjectId("5752268bf435fd6c606d67a3")},{"$set" : {"favorite book" : ["Cat's Cradle", "Foundation Trilogy", "Ender's Game"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("57522660f435fd6c606d67a1"), "name" : "joe", "age" : 65, "favorite book" : "War and Peace" }
{ "_id" : ObjectId("57522683f435fd6c606d67a2"), "name" : "joe", "age" : 21, "favorite book" : "Green Eggs and Ham" }
{ "_id" : ObjectId("5752268bf435fd6c606d67a3"), "name" : "joe", "age" : 49, "favorite book" : [ "Cat's Cradle", "Foundation Trilogy", "Ender's Game" ] }

> db.people.insert({"name" : "Neo", "age" : 35, "personal info" : {"Adrees" : "Beijing", "Phone" : 18500000005}})
WriteResult({ "nInserted" : 1 })
> db.people.find()
{ "_id" : ObjectId("57522660f435fd6c606d67a1"), "name" : "joe", "age" : 65, "favorite book" : "War and Peace" }
{ "_id" : ObjectId("57522683f435fd6c606d67a2"), "name" : "joe", "age" : 21 }
{ "_id" : ObjectId("5752268bf435fd6c606d67a3"), "name" : "joe", "age" : 49, "favorite book" : [ "Cat's Cradle", "Foundation Trilogy", "Ender's Game" ] }
{ "_id" : ObjectId("57540a73f435fd6c606d67a8"), "name" : "Neo", "age" : 35, "personal info" : { "Adrees" : "Beijing", "Phone" : 18500000005 } }
> db.people.update({"_id" : ObjectId("57540a73f435fd6c606d67a8")},{"$set" : {"Phone" : 18000000000}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("57522660f435fd6c606d67a1"), "name" : "joe", "age" : 65, "favorite book" : "War and Peace" }
{ "_id" : ObjectId("57522683f435fd6c606d67a2"), "name" : "joe", "age" : 21 }
{ "_id" : ObjectId("5752268bf435fd6c606d67a3"), "name" : "joe", "age" : 49, "favorite book" : [ "Cat's Cradle", "Foundation Trilogy", "Ender's Game" ] }
{ "_id" : ObjectId("57540a73f435fd6c606d67a8"), "name" : "Neo", "age" : 35, "personal info" : { "Adrees" : "Beijing", "Phone" : 18500000005 }, "Phone" : 18000000000 }

--"$unset"完全刪除某個鍵

> db.people.update({"_id" : ObjectId("57522683f435fd6c606d67a2")},{"$unset" : {"favorite book" : 1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.people.find()
{ "_id" : ObjectId("57522660f435fd6c606d67a1"), "name" : "joe", "age" : 65, "favorite book" : "War and Peace" }
{ "_id" : ObjectId("57522683f435fd6c606d67a2"), "name" : "joe", "age" : 21 }
{ "_id" : ObjectId("5752268bf435fd6c606d67a3"), "name" : "joe", "age" : 49, "favorite book" : [ "Cat's Cradle", "Foundation Trilogy", "Ender's Game" ] }

--"$inc"修改器用來增加已有鍵的值,如果該鍵不存在就建立一個,專門用於增加(和減少)數字的。對於更新分析資料、投票或者其他有變化資料的地方,使用這個都會非常方便。

> db.games.insert({"game" : "pinball", "user" : "joe"})
2016-06-11T02:31:48.943-0700 I COMMAND  [conn4] command test.games command: insert { insert: "games", documents: [ { _id: ObjectId('575bda83f435fd6c606d67a9'), game: "pinball", user: "joe" } ], 
ordered: true } ninserted:1 keyUpdates:0 writeConflicts:0 numYields:0 reslen:25 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 1, W: 1 } }, Collection: { 
acquireCount: { w: 1, W: 1 } } } protocol:op_command 733ms
WriteResult({ "nInserted" : 1 })
> db.games.findOne()
{
"_id" : ObjectId("575bda83f435fd6c606d67a9"),
"game" : "pinball",
"user" : "joe"
}
> db.games.update({"game" : "pinball", "user" : "joe"},{"$inc" : {"score" : 50}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.games.findOne()
{
"_id" : ObjectId("575bda83f435fd6c606d67a9"),
"game" : "pinball",
"user" : "joe",
"score" : 50
}
> db.games.update({"game" : "pinball", "user" : "joe"},{"$inc" : {"score" : 10000}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.games.findOne()
{
"_id" : ObjectId("575bda83f435fd6c606d67a9"),
"game" : "pinball",
"user" : "joe",
"score" : 10050
}

--"$push"會向已有的陣列末尾加入一個元素,要是沒有就建立一個新的陣列。

> db.blog.posts.insert({"title" : "A blog post", "contents" : "personal information"})
WriteResult({ "nInserted" : 1 })
> db.blog.posts.findOne()
{
"_id" : ObjectId("575bdd81f435fd6c606d67aa"),
"title" : "A blog post",
"contents" : "personal information"
}
> db.blog.findOne()
null
> db.blog.find()
> db.blog.posts.findOne()
{
"_id" : ObjectId("575bdd81f435fd6c606d67aa"),
"title" : "A blog post",
"contents" : "personal information"
}
> db.blog.posts.update({"title" : "A blog post"}, {"$push" : {"comments" : {"name" : "joe", "email" : "joe@example.com", "content" : "nice post."}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blog.posts.findOne()
{
"_id" : ObjectId("575bdd81f435fd6c606d67aa"),
"title" : "A blog post",
"contents" : "personal information",
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
}
]
}
> db.blog.posts.update({"title" : "A blog post"}, {"$push" : {"comments" : {"name" : "bob", "email" : "bob@example.com", "content" : "good post."}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blog.posts.findOne()
{
"_id" : ObjectId("575bdd81f435fd6c606d67aa"),
"title" : "A blog post",
"contents" : "personal information",
"comments" : [
{
"name" : "joe",
"email" : "joe@example.com",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "bob@example.com",
"content" : "good post."
}
]
}

--將陣列作為資料集使用

> db.users.insert({"username" : "joe", "emails" : ["jose@example.com", "joe@gmail.com", "joe@yahoo.com"]})
WriteResult({ "nInserted" : 1 })
> db.users.find();
{ "_id" : ObjectId("575bdf87f435fd6c606d67ab"), "username" : "joe", "emails" : [ "jose@example.com", "joe@gmail.com", "joe@yahoo.com" ] }
> db.users.update({"_id" : ObjectId("575bdf87f435fd6c606d67ab")}, {"$addToSet" : {"emails" : "jose@gmail.com"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find();
{ "_id" : ObjectId("575bdf87f435fd6c606d67ab"), "username" : "joe", "emails" : [ "jose@example.com", "joe@gmail.com", "joe@yahoo.com", "jose@gmail.com" ] }
> db.users.update({"_id" : ObjectId("575bdf87f435fd6c606d67ab")}, {"$addToSet" : {"emails" : "joe@gmail.com"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.users.find();
{ "_id" : ObjectId("575bdf87f435fd6c606d67ab"), "username" : "joe", "emails" : [ "jose@example.com", "joe@gmail.com", "joe@yahoo.com", "jose@gmail.com" ] }
> db.users.update({"_id" : ObjectId("575bdf87f435fd6c606d67ab")}, {"$addToSet" : {"emails" : {"$each" : ["joe@gmail.com" ,"jose@gmail.com"]}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.users.find();
{ "_id" : ObjectId("575bdf87f435fd6c606d67ab"), "username" : "joe", "emails" : [ "jose@example.com", "joe@gmail.com", "joe@yahoo.com", "jose@gmail.com" ] }


--刪除元素

> db.lists.insert({"todo" : ["dishes" , "laundry", "dry cleaning"]})
2016-06-11T03:26:01.394-0700 I COMMAND  [conn4] command test.lists command: insert { insert: "lists", documents: [ { _id: ObjectId('575be738f435fd6c606d67ac'), todo: [ "dishes", "laundry", "dry 
cleaning" ] } ], ordered: true } ninserted:1 keyUpdates:0 writeConflicts:0 numYields:0 reslen:25 locks:{ Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 1, W: 1 } }, 
Collection: { acquireCount: { w: 1, W: 1 } } } protocol:op_command 434ms
WriteResult({ "nInserted" : 1 })
> db.lists.findOne()
2016-06-11T03:26:21.355-0700 I COMMAND  [conn4] command test.lists command: find { find: "lists", filter: {}, limit: 1.0, singleBatch: true } planSummary: COLLSCAN keysExamined:0 docsExamined:1 
cursorExhausted:1 keyUpdates:0 writeConflicts:0 numYields:0 nreturned:1 reslen:186 locks:{ Global: { acquireCount: { r: 2 } }, Database: { acquireCount: { r: 1 } }, Collection: { acquireCount: { r: 
1 } } } protocol:op_command 102ms
{
"_id" : ObjectId("575be738f435fd6c606d67ac"),
"todo" : [
"dishes",
"laundry",
"dry cleaning"
]
}
> db.lists.find()
{ "_id" : ObjectId("575be738f435fd6c606d67ac"), "todo" : [ "dishes", "laundry", "dry cleaning" ] }
> db.lists.update({}, {"$pull" : {"todo" : "laundry"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.lists.findOne()
{
"_id" : ObjectId("575be738f435fd6c606d67ac"),
"todo" : [
"dishes",
"dry cleaning"
]
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2121146/,如需轉載,請註明出處,否則將追究法律責任。

相關文章