• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
安安的BLOG
安安目前专注电子商务解决方案^_^
博客园    首页    新随笔    联系   管理    订阅  订阅

修改器 $set 设置某个键的值 ,修改器 $unset 删除某个键

修改器 $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" }

如果用户觉得现在更喜欢另一本书

> 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"
}

如果用户觉得喜欢一堆书,而且每次修改 用"_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"
}

如果不要用户喜欢读书这个键了,可以使用 $unset 将键完全删除

> db.user.update({"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"
}

修改 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 修改 ,如下面的修改方法

> db.foo.update({"title":"My Blog post"},
... {"author":"Hi,joe schmoe"})
> db.foo.findOne()
{ "_id" : ObjectId("4fdc7e1889834bfb0bd267a4"), "author" : "Hi,joe schmoe" }
>

 

 执行完成后事与愿违,它把整文档 替换掉了。 所以 一定要使用 以 $ 开头的修改器 来修改 键、值 对。

 

posted @ 2012-06-18 10:47  安安  阅读(348)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3