MongoDB分组查询

1.查看集合

show collections

1.png

2.添加多条数据

db.runoob.insertMany([

{

'title':'MongoDB Overview', 

    'description':'MongoDB is no sql database',

    'by_user':'demo.runoob.com',

    'url':'http://www.runoob.com',

    'tags':['mongodb', 'database', 'NoSQL'],

    'likes':100

},

{

'title':'NoSQL Overview', 

    'description':'No sql database is very fast',

    'by_user':'demo.runoob.com',

    'url':'http://www.runoob.com',

    'tags':['mongodb', 'database', 'NoSQL'],

    'likes':10

},

{

'title':'MongoDB Overview1', 

    'description':'MongoDB is no sql database',

    'by_user':'study.runoob.com',

    'url':'http://www.runoob.com',

    'tags':['mongodb', 'database', 'NoSQL'],

    'likes':100

},

{

'title':'NoSQL Overview1', 

    'description':'No sql database is very fast',

    'by_user':'study.runoob.com',

    'url':'http://www.runoob.com',

    'tags':['mongodb', 'database', 'NoSQL'],

    'likes':10

}

])

2.png

3.查看所有数据

db.runoob.find().pretty()

3.png

4.分组查询

db.runoob.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])

4.png

QQ截图20181024105632.png

猜你喜欢

转载自blog.51cto.com/suyanzhu/2308245