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

mongo 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 } })
posted @ 2024-12-05 11:47  vx_guanchaoguo0  阅读(6)  评论(0)    收藏  举报