插入文档操作
|
|
db.foo.insert({"bar": "baz"})
|
删除文档操作
|
删除所有的数据:
|
db.foo.remove()
|
|
删除指定的数据
|
db.foo.remove(
{"bar": "baz"})
|
更新文档操作
|
{
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"name" : "joe",
"friends" : 32,
"enemies" : 2
}
更新为
{
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"username" : "joe",
"relationships" :
{
"friends" : 32,
"enemies" : 2
}
}
|
操作步骤:
var joe = db.users.findOne({"name" : "joe"});
joe.relationships = {"friends" : joe.friends, "enemies" : joe.enemies};
joe.username = joe.name;
delete joe.friends;
delete joe.enemies;
delete joe.name;
db.users.update({"name" : "joe"}, joe);
|
修改器
|
|
|
$set修改器
|
"$set"修改器用来指定一个键值,如果这个键不存在,则创建它
|
> db.users.find()
{ "_id" : ObjectId("55153d95b6aecf8244e6f263"),
"name" : "joe",
"age" : 30,
"sex" : "male",
"location" : "Wisconsin" }
//不存在,创建一个
> db.users.update({"_id" : ObjectId("55153d95b6aecf8244e6f263")}, {"$set":{"favorite book":"war and peace"}})
> db.users.find()
{ "_id" : ObjectId("55153d95b6aecf8244e6f263"), "age" : 30, "favorite book" : "war and peace", "location" : "Wisconsin", "name" : "joe", "sex" : "male" }
//存在更新一个
> db.users.update({"name":"joe"}, {"$set":{"favorite book":"green eggs and ham"}})
> db.users.find()
{ "_id" : ObjectId("55153d95b6aecf8244e6f263"), "age" : 30, "favorite book" : "green eggs and ham", "location" : "Wisconsin", "name" : "joe", "sex" : "male" }
//更新为数组
> db.users.update({"name":"joe"}, {"$set":{"favorite book":["cat's candle", "foundation trilogy", "ender's game"]}})
> db.users.find()
{ "_id" : ObjectId("55153d95b6aecf8244e6f263"), "age" : 30, "favorite book" : [ "cat's candle", "foundation trilogy", "ender's game" ], "location" : "Wisconsin", "name" : "joe", "sex" : "male" }
> db.address.findOne()
{
"_id" : ObjectId("55150a19b6aecf8244e6f25a"),
"name" : "John Doe",
"address" : {
"street" : "123 Park Street",
"city" : "Anytown",
"state" : "NY"
}
}
> db.address.update({"address.city":"Anytown"}, {$set :{"address.state":"KY"}})
> db.address.findOne()
{
"_id" : ObjectId("55150a19b6aecf8244e6f25a"),
"address" : {
"city" : "Anytown",
"state" : "KY",
"street" : "123 Park Street"
},
"name" : "John Doe"
}
|
$unset修改器, 删除一个键值
|
|
> db.users.update({"name" : "joe"}, {"$unset" : {"favorite book" : 1}})
> db.users.find()
{ "_id" : ObjectId("55153d95b6aecf8244e6f263"), "age" : 30, "location" : "Wisconsin", "name" : "joe", "sex" : "male" }
|
$inc增加和减少器
|
$inc修改器用来增加已有的键值,或者在键值不在的时候创建一个键值;值为负数的时候即为减
|
> db.games.findOne()
{
"_id" : ObjectId("55151a11b6aecf8244e6f25c"),
"game" : "pineball",
"score" : 0,
"user" : "Joe"
}
> db.games.update({"game" : "pineball","user" : "Joe"}, {"$inc": {"score":50}})
> db.games.findOne()
{
"_id" : ObjectId("55151a11b6aecf8244e6f25c"),
"game" : "pineball",
"score" : 50,
"user" : "Joe"
}
|
数组修改器
|
|
|
$push:
|
如果已有的键值已经存在,"$push"会向已有的数组尾部加入一个元素,如果不存在,则创建一个新的的数组
|
>db.blog.find()
{ "_id" : ObjectId("5518a6ba1d802f6252d90e79"), "title" : "A blog post", "content" : "..." }
>db.blog.update({"title":"A blog post"}, {"$push":{"comments":... {"name":"joe","email":"joe@gmail.com", "content":"nice post."}}})
> db.blog.find()
> db.blog.update({"title":"A blog post"}, {"$push":{"comments":{ "_id" : ObjectId("5518a6ba1d802f6252d90e79"), "comments" : [ { "name" : "joe", "email" : "joe@gmail.com", "content" : "nice post." } ], "content" : "...", "title" : "A blog post" } ... {"name":"bob","email":"bob@gmail.com", "content":"good post."}}})
> db.blog.findOne()
{ "_id" : ObjectId("5518a6ba1d802f6252d90e79"), "comments" : [ { "name" : "joe", "email" : "joe@gmail.com", "content" : "nice post." }, { "name" : "bob", "email" : "bob@gmail.com", "content" : "good post." } ], "content" : "...", "title" : "A blog post" } |
$addToSet
|
可以避免重复插入
|
> db.users.findOne() { "_id" : ObjectId("5518ae1b1d802f6252d90e7b"), "username" : "joe", "emails" : [ "joe@example.com", "joe@gmail.com", "joe@yahoo.com" ] } > db.users.findOne() { "_id" : ObjectId("5518ae1b1d802f6252d90e7b"), "username" : "joe", "emails" : [ "joe@example.com", "joe@gmail.com", "joe@yahoo.com" ] }
> db.users.findOne()
{ "_id" : ObjectId("5518ae1b1d802f6252d90e7b"), "emails" : [ "joe@example.com", "joe@gmail.com", "joe@yahoo.com", "joe@hotmail.com" ], "username" : "joe" } |
$addToSet和$each组合
|
$addToSet和$each组合,可以添加多个不同的值
|
> db.users.update({"username" : "joe"}, {"$addToSet":{"emails":{"$each":["joe@php.net", "joe@python.org"]}}})
> db.users.findOne() { "_id" : ObjectId("5518ae1b1d802f6252d90e7b"), "emails" : [ " joe@example.com ", " joe@gmail.com ", " joe@yahoo.com ", " joe@hotmail.com ", "joe@php.net", "joe@python.org" ], "username" : "joe" } |
$pop修改器
|
从数组中删除元素,{"$pop":{key:1}}表示从数组尾部删除数据,{"$pop":{key:-1}}从头部删除数据
|
|
$pull修改器
|
$pull修改器
|
> db.lists.find()
{ "_id" : ObjectId("5518b3de1d802f6252d90e7c"), "todo" : [ "dishes", "laundry", "dry cleaning" ] }
> db.lists.update({}, {"$pull":{"todo":"laundry"}})
> db.lists.find()
{ "_id" : ObjectId("5518b4b71d802f6252d90e7d"), "todo" : [ "dishes", "dry cleaning" ] }
|
|
多个条件符合时,会删除多个
|
> db.lists.find()
{ "_id" : ObjectId("5518b5201d802f6252d90e7e"), "todo" : [ "dishes", "laundry", "dry cleaning", "laundry" ] }
> db.lists.update({}, {"$pull":{"todo":"laundry"}})
> db.lists.find()
{ "_id" : ObjectId("5518b5201d802f6252d90e7e"), "todo" : [ "dishes", "dry cleaning" ] }
|
数组的定位修改器
|
若数组有多个值,我们只想对其中一部分进行操作,需要一定的技巧;数组是已0开始的,可以直接以下标作为键值来选择元素
|
> db.blog.posts.findOne()
{
"_id" : ObjectId("5518b70d1d802f6252d90e7f"),
"content" : "...",
"comments" : [
{
"comment" : "good post",
"votes" : 0
},
{
"comment" : "i thought it was too short",
"author" : "Claire",
"votes" : 3
},
{
"comment" : "free watches",
"author" : "Alice",
"votes" : -1
}
]
}
db.blog.posts.update({"_id" : ObjectId("5518b70d1d802f6252d90e7f")}, {"$inc":{"comments.0.votes":1}})
> db.blog.posts.findOne()
{
"_id" : ObjectId("5518b70d1d802f6252d90e7f"),
"content" : "...",
"comments" : [
{
"comment" : "good post",
"votes" : 1
},
{
"comment" : "i thought it was too short",
"author" : "Claire",
"votes" : 3
},
{
"comment" : "free watches",
"author" : "Alice",
"votes" : -1
}
]
}
|
|
MongoDB提供了定位操作符"$",用来定位查询文档已经匹配的元素,并进行更新,当多个符合时,只是修改第一个
|
> db.blog.posts.findOne()
{
"_id" : ObjectId("5518b70d1d802f6252d90e7f"),
"content" : "...",
"comments" : [
{
"comment" : "good post",
"votes" : 1
},
{
"comment" : "i thought it was too short",
"author" : "Claire",
"votes" : 3
},
{
"comment" : "free watches",
"author" : "Alice",
"votes" : -1
}
]
}
> db.blog.posts.update({"comments.author":"Alice"}, {"$set":{"comments.$.author":"Jim"}})
> db.blog.posts.findOne()
{
"_id" : ObjectId("5518b70d1d802f6252d90e7f"),
"comments" : [
{
"comment" : "good post",
"votes" : 1
},
{
"comment" : "i thought it was too short",
"author" : "Claire",
"votes" : 3
},
{
"author" : "Jim",
"comment" : "free watches",
"votes" : -1
}
],
"content" : "..."
}
|
upsert (update的第三个参数为true)
|
upsert是一种特殊的更新,要是没有符合更新条件,就会以这个条件和更新文档为基础创建一个新的文档;如果找到了匹配的文档,则正常更新
|
> db.math.remove()
> db.math.update({"count":25}, {"$inc":{"count":3}}, true)
> db.math.find()
{ "_id" : ObjectId("5518be218d926d78f40e93ea"), "count" : 28 }
|
更新多个文档 (update的第四个参数为true) | 在默认的情况下,更新只能对符合匹配的第一个文档进行更新,当需要更新多个文档时,将update的第四个参数设置为true |
> db.users.update({birthday : "10/13/1978"},{$set : {gift : "Happy Birthday!"}}, false, true)
|
|
|
|
参考资料: 《MongoDB权威指南》