当前位置:Gxlcms > 数据库问题 > MongoDB - 聚合

MongoDB - 聚合

时间:2021-07-01 10:21:17 帮助过:39人阅读

//www.baidu.com/", "web_name": "百度", "click_num": 605, "year": 2019 }
 1 # 输出文档中除了web_name和click_num外,还包含默认的 _id 域
 2 db.test_study.aggregate([
 3     {
 4         $project: {
 5             web_name: "$web_name",
 6             click_num: "$click_num"
 7         }
 8     }
 9 ])
10 
11 
12 # 设置 _id:0 输出文档中只包括 web_name 和 click_num,不包含 _id 域
13 db.test_study.aggregate([
14     {
15         $project: {
16          _id:0,
17             web_name: "$web_name",
18             click_num: "$click_num"
19         }
20     }
21 ])

 

  • 输出嵌套字段中的部分字段
    # 事例数据
    { "_id":
    1, "title": "789", "author": { "last": "Li", "first": "Lucy", "country": "China" }, "copies": 5, "lastModified": "2019-07-28" }

    需求:输出 title,author.country

    db.test.aggregate([{
        $project: {
        _id:0,
            title: "$title",
            author_country: "$author.country"
        }
    }])

     

  • 排除_id、author.country、copies 其他字段均输出(事例数据如上)
    db.test.aggregate([{
        $project: {
            "_id":0,
            "copies": 0,
            "author.country": 0
        }
    }])

    注意:
    "copies" 要加双引号

     

  • MongoDB - 聚合

    标签:选择   baidu   com   双引号   增加   ast   输出   author   ODB   

    人气教程排行