MongoDB学习记录

2021-04-29 09:32:18 | 编辑 | 添加

mongoDb常用查询命令持续学习笔记

1.group分组统计

db.getCollection('full_score').aggregate([
? ? {"$group" : {_id:"$target.name", count:{$sum:1}}}
])

2.排序

db.getCollection('preselection').find({project:"584cb710fa4",
? ? "range.name":"student",
? ? "target.id":"001002003008",
? ? }).sort({"originalScoreAnalyzeProvinceRank":1})

3.打印查询结果

var rows=db.getCollection('average').find({
? ? ?project:"cb710fa4",
? ? "range.id":"40990a"
? ??
})
while(rows.hasNext()){
? ?print(rows.next().target.name)?
}

4.执行js命令

js内容:

var rows=db.getCollection('rank').find({project:"430000-6a6409b8abc243828ed0d584cb710fa4","target.id":"001","target.name":"subject","range.name":"province"})
.sort({rank:1,student:1})

while(rows.hasNext()) {
    printjson(rows.next());
}

mongo 192.168.1.12:29000/data projectRank.js>projectRank.json

 

5.mongo命令行连接命令

连接mongo

mongo --host host --port port --authenticationDatabase database -u user -p password

显示数据库

show dbs;

切换数据库

use db;

列出集合

show collections;

 

关注编程学问公众号