修改器 $set 用来 设置一个指定键的值 , 如果键不存在 创建它。
给用户 添加喜欢看的书 这个键值进去:
> db.user.find()
{ "_id" : ObjectId("4fdc893d89834bfb0bd267a6"), "relationships" : { "friends" :
32, "enemies" : 2 }, "username" : "joe" }
> db.user.update({"_id" : ObjectId("4fdc893d89834bfb0bd267a6")},
... {"$set":{"favorite book" : "MongoDb,The Definitive Guide"}})
> db.user.find()
{ "_id" : ObjectId("4fdc893d89834bfb0bd267a6"), "favorite book" : "MongoDb,The D
efinitive Guide", "relationships" : { "friends" : 32, "enemies" : 2 }, "username
" : "joe" }
{ "_id" : ObjectId("4fdc893d89834bfb0bd267a6"), "relationships" : { "friends" :
32, "enemies" : 2 }, "username" : "joe" }
> db.user.update({"_id" : ObjectId("4fdc893d89834bfb0bd267a6")},
... {"$set":{"favorite book" : "MongoDb,The Definitive Guide"}})
> db.user.find()
{ "_id" : ObjectId("4fdc893d89834bfb0bd267a6"), "favorite book" : "MongoDb,The D
efinitive Guide", "relationships" : { "friends" : 32, "enemies" : 2 }, "username
" : "joe" }
如果用户觉得现在更喜欢另一本书
> db.user.update({"_id" : ObjectId("4fdc893d89834bfb0bd267a6")},
... {"$set":{"favorite book":"good book 2"}})
> db.user.findOne()
{
"_id" : ObjectId("4fdc893d89834bfb0bd267a6"),
"favorite book" : "good book 2",
"relationships" : {
"friends" : 32,
"enemies" : 2
},
"username" : "joe"
}
... {"$set":{"favorite book":"good book 2"}})
> db.user.findOne()
{
"_id" : ObjectId("4fdc893d89834bfb0bd267a6"),
"favorite book" : "good book 2",
"relationships" : {
"friends" : 32,
"enemies" : 2
},
"username" : "joe"
}
如果用户觉得喜欢一堆书,而且每次修改 用"_id" 太长 无意义 容易打错,这次用唯一字段代替。
> db.user.update({"username":"joe"},
... {"$set": {"favorite book":
... ["book1","book2","book3","book4"]}})
> db.user.findOne()
{
"_id" : ObjectId("4fdc893d89834bfb0bd267a6"),
"favorite book" : [
"book1",
"book2",
"book3",
"book4"
],
"relationships" : {
"friends" : 32,
"enemies" : 2
},
"username" : "joe"
}
... {"$set": {"favorite book":
... ["book1","book2","book3","book4"]}})
> db.user.findOne()
{
"_id" : ObjectId("4fdc893d89834bfb0bd267a6"),
"favorite book" : [
"book1",
"book2",
"book3",
"book4"
],
"relationships" : {
"friends" : 32,
"enemies" : 2
},
"username" : "joe"
}
如果不要用户喜欢读书这个键了,可以使用 $unset 将键完全删除
> db.user.update({"username":"joe"},
... {"$unset":{"favorite book":1}})
> db.user.findOne()
{
"_id" : ObjectId("4fdc893d89834bfb0bd267a6"),
"relationships" : {
"friends" : 32,
"enemies" : 2
},
"username" : "joe"
}
... {"$unset":{"favorite book":1}})
> db.user.findOne()
{
"_id" : ObjectId("4fdc893d89834bfb0bd267a6"),
"relationships" : {
"friends" : 32,
"enemies" : 2
},
"username" : "joe"
}
使用 $set 修改内嵌文档
例如:
给 post 增加 author 键 内嵌文档
> db.foo.find()
{ "_id" : ObjectId("4fdc7e1889834bfb0bd267a4"), "title" : "My Blog post", "conte
nt" : "Here's my blog post.", "date" : ISODate("2012-06-16T12:19:25.163Z") }
> db.foo.update({"title":"My Blog post"},
... {"$set":{"author":
... {"name":"joe",
... "email":"joe@example.com"}
... }})
> db.foo.findOne()
{
"_id" : ObjectId("4fdc7e1889834bfb0bd267a4"),
"author" : {
"name" : "joe",
"email" : "joe@example.com"
},
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
{ "_id" : ObjectId("4fdc7e1889834bfb0bd267a4"), "title" : "My Blog post", "conte
nt" : "Here's my blog post.", "date" : ISODate("2012-06-16T12:19:25.163Z") }
> db.foo.update({"title":"My Blog post"},
... {"$set":{"author":
... {"name":"joe",
... "email":"joe@example.com"}
... }})
> db.foo.findOne()
{
"_id" : ObjectId("4fdc7e1889834bfb0bd267a4"),
"author" : {
"name" : "joe",
"email" : "joe@example.com"
},
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
修改 author 内嵌文档 中的 name 为 joe schmoe
> db.foo.update({"title":"My Blog post"},
... {"$set":{"author.name":"joe schmoe"}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdc7e1889834bfb0bd267a4"),
"author" : {
"email" : "joe@example.com",
"name" : "joe schmoe"
},
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
... {"$set":{"author.name":"joe schmoe"}})
> db.foo.findOne()
{
"_id" : ObjectId("4fdc7e1889834bfb0bd267a4"),
"author" : {
"email" : "joe@example.com",
"name" : "joe schmoe"
},
"content" : "Here's my blog post.",
"date" : ISODate("2012-06-16T12:19:25.163Z"),
"title" : "My Blog post"
}
如果 不使用 $set 修改 ,如下面的修改方法
> db.foo.update({"title":"My Blog post"},
... {"author":"Hi,joe schmoe"})
> db.foo.findOne()
{ "_id" : ObjectId("4fdc7e1889834bfb0bd267a4"), "author" : "Hi,joe schmoe" }
>
... {"author":"Hi,joe schmoe"})
> db.foo.findOne()
{ "_id" : ObjectId("4fdc7e1889834bfb0bd267a4"), "author" : "Hi,joe schmoe" }
>
执行完成后事与愿违,它把整文档 替换掉了。 所以 一定要使用 以 $ 开头的修改器 来修改 键、值 对。