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

--创建集合

> 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" : "[email protected]", "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" : "[email protected]",
"content" : "nice post."
}
]
}
> db.blog.posts.update({"title" : "A blog post"}, {"$push" : {"comments" : {"name" : "bob", "email" : "[email protected]", "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" : "[email protected]",
"content" : "nice post."
},
{
"name" : "bob",
"email" : "[email protected]",
"content" : "good post."
}
]
}

--将数组作为数据集使用

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


--删除元素

> 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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26506993/viewspace-2121146/

你可能感兴趣的:(数据库,python)