MongoDB 之聚合函数查询统计

第一关:第1关:聚合管道操作符将文档定制格式输出(一)

(命令行)

mongo
use test1
db.educoder.insert([{  
    _id:1,  
    course:'Python表达式问题求解实训',  
    author:'李暾',  
    tags:['Python基础','求解'],  
    learning_num:1882  
},  
{  
    _id:2,  
    course:'Java语言之基本语法',  
    author:'余跃',  
    tags:['Java基础','语法'],  
    learning_num:814  
},  
{  
    _id:3,  
    course:'Python面向对象编程实训',  
    author:'李暾',  
    tags:['Python基础','面向对象'],  
    learning_num:143  
},  
{  
    _id:4,  
    course:'Android综合实训之物联网移动应用开发(1)',  
    author:'prophet5',  
    tags:['Android','物联网','移动开发'],  
    learning_num:207  
}  ])



(代码文件)

#********* Begin *********#
 
echo "
db.educoder.aggregate({\$project:{_id:0,course:1,learning_num:1}});
db.educoder.aggregate({\$match:{learning_num:1882}});
"
 
#********* End *********#

第二关:

(命令行)

mongo
use test2
db.educoder.insert([{  
    _id:1,  
    course:'Python表达式问题求解实训',  
    author:'李暾',  
    tags:['Python基础','求解'],  
    learning_num:1882  
},  
{  
    _id:2,  
    course:'Java语言之基本语法',  
    author:'余跃',  
    tags:['Java基础','语法'],  
    learning_num:814  
},  
{  
    _id:3,  
    course:'Python面向对象编程实训',  
    author:'李暾',  
    tags:['Python基础','面向对象'],  
    learning_num:143  
},  
{  
    _id:4,  
    course:'Android综合实训之物联网移动应用开发(1)',  
    author:'prophet5',  
    tags:['Android','物联网','移动开发'],  
    learning_num:207  
}  ])



(代码文件)

#********* Begin *********#
 
echo "
db.educoder.aggregate([{\$skip:0},{\$limit:3}]);
db.educoder.aggregate({\$sort:{learning_num:1}});
db.educoder.aggregate([{\$skip:2},{\$limit:2}]);
"
 
#********* End *********#

第三关:

(命令行)

mongo
use test3
db.educoder.insert([{  
    _id:1,  
    course:'Python表达式问题求解实训',  
    author:'李暾',  
    tags:['Python基础','求解'],  
    learning_num:1882  
},  
{  
    _id:2,  
    course:'Java语言之基本语法',  
    author:'余跃',  
    tags:['Java基础','语法'],  
    learning_num:814  
},  
{  
    _id:3,  
    course:'Python面向对象编程实训',  
    author:'李暾',  
    tags:['Python基础','面向对象'],  
    learning_num:143  
},  
{  
    _id:4,  
    course:'Android综合实训之物联网移动应用开发(1)',  
    author:'prophet5',  
    tags:['Android','物联网','移动开发'],  
    learning_num:207  
}  ])



(代码文件)

#********* Begin *********#
 
echo "
 
db.educoder.aggregate([{\$group:{_id:'\$author',first_course:{\$first:'\$course'}}}]);
 
db.educoder.aggregate([{\$group:{_id:'\$author',learning_avg:{\$avg:'\$learning_num'}}}]);
 
db.educoder.aggregate([{\$unwind:'\$tags'},{\$group:{_id:'\$tags',course_num:{\$sum:1}}}]);
 
"
 
#********* End *********#

你可能感兴趣的:(educoder,mongodb,数据库,database)