mongo 聚合查询
按照数量返回
db.getCollection("xxxx").aggregate([
{
$match: {
"daily.incomeMap": { $exists: true }
}
},
{
$group: {
_id: "$groupId",
totalCount: { $sum: 1 }
}
},
{
$sort: { totalCount: -1 }
},
{
$limit: 1000
}
])
datagrip idea pycharm database for sql
select * from group_daily_stats where groupId = objectid('6746d26f084995f282cfb51f')
// 自动转为 mongo raw sql
db.getSiblingDB("xxxx").getCollection("xxxx").find({"groupId": {$eq: new ObjectId('6746d26f084995f282cfb51f')}})
日期转换 天转为 月和年 聚合
db.getCollection('xxxx').aggregate([
{
'$project': {
'date': {
'$dateFromString': {
'dateString': '$date'
}
},
'rpt': 1
}
},
{
'$project': {
'year': {
'$year': '$date'
},
'rpt': 1,
'month': {
'$month': '$date'
}
}
},
{
'$group': {
'count': {
'$sum': '$rpt.count'
},
'lineCoins': {
'$sum': '$rpt.lineCoins'
},
'_id': {
'month': '$month',
'year': '$year'
}
}
}
])
存在某个字段
db.getCollection("Dealer").find({ "daily.incomeMap": { $exists: true } })
本文来自博客园,作者:vx_guanchaoguo0,转载请注明原文链接:https://www.cnblogs.com/guanchaoguo/p/18588211