zoukankan      html  css  js  c++  java
  • Mongdb操作嵌套文档

    1、一个文档如下

    db.posts.find()
    {
        "_id" : ObjectId("5388162dfc164ee1f39be37f"),
        "title" : "Java Example",
        "content" : "This is a example for Java!",
        "comments" : []
    }

    2、往_id等于"5388162dfc164ee1f39be37f"的文档中的comments插入数据

    db.posts.update({"_id":ObjectId("5388162dfc164ee1f39be37f")},{$push:{"comments":{"content":"Good Article!","author":"Luxh"}}

     再插入一条

    db.posts.update({"_id":ObjectId("5388162dfc164ee1f39be37f")},{$push:{"comments":{"content":"Not bad!","author":"Chuliuxiang"}}})

     结果如下

    db.posts.find()
    
    
    {
        "_id" : ObjectId("5388162dfc164ee1f39be37f"),
        "title" : "Java Example",
        "content" : "This is a example for Java!",
        "comments" : [ 
            {
                "content" : "Good Article!",
                "author" : "Luxh"
            }, 
            {
                "content" : "Not bad!",
                "author" : "Chuliuxiang"
            }
        ]
    }

    3、根据内嵌文档查询

      1)查询出Luxh评论过的文章

    db.posts.find({"comments.author":"Luxh"})
    
    结果如下:
    {
        "_id" : ObjectId("5388162dfc164ee1f39be37f"),
        "title" : "Java Example",
        "content" : "This is a example for Java!",
        "comments" : [ 
            {
                "content" : "Good Article!",
                "author" : "Luxh"
            }, 
            {
                "content" : "Not bad!",
                "author" : "Chuliuxiang"
            }
        ]
    }

      2)查询Luxh评论过的文章,返回指定的键

    db.posts.find({"comments.author":"Luxh"},{"title":1,"content":1})
    
    结果如下:
    
    {
        "_id" : ObjectId("5388162dfc164ee1f39be37f"),
        "title" : "Java Example",
        "content" : "This is a example for Java!"
    }

      _id默认会返回。取消可设置"_id":0

    4、修改

      将author=Luxh评论的内容content修改为"I like it!"

    db.posts.update({"comments.author":"Luxh"},{$set:{"comments.$.content":"I like it!"}})
    
    结果如下:
    {
        "_id" : ObjectId("5388162dfc164ee1f39be37f"),
        "title" : "Java Example",
        "content" : "This is a example for Java!",
        "comments" : [ 
            {
                "content" : "I like it!",
                "author" : "Luxh"
            }, 
            {
                "content" : "Not bad!",
                "author" : "Chuliuxiang"
            }
        ]
    }

    5、删除

      删除comments中author=Luxh的记录

    db.posts.update({},{$pull:{"comments":{"author":"Luxh"}}})
    
    结果如下:
    
    {
        "_id" : ObjectId("5388162dfc164ee1f39be37f"),
        "title" : "Java Example",
        "content" : "This is a example for Java!",
        "comments" : [ 
            {
                "content" : "Not bad!",
                "author" : "Chuliuxiang"
            }
        ]
    }
  • 相关阅读:
    Ubuntu 上更新 Flash 插件
    Ubuntu 17.10 安装 “爱壁纸” 时,缺失了 python-support 依赖
    Windows 7 64 位操作系统安装 Ubuntu 17.10
    Linux CentOS 6.9(图形界面)安装中文输入法
    Linux 编译 apr-util 时报错
    Linux 添加普通用户到 sudoers 文件
    PHP 结合实例认识 Socket
    PHP 快速建立一个对象
    使用Git GUI,上传项目到github,并实现预览功能
    用JS判断号码
  • 原文地址:https://www.cnblogs.com/luxh/p/3760974.html
Copyright © 2011-2022 走看看