is It possible to compare two Months Data in single Collection in MongoDB?

房东的猫 提交于 2021-02-08 11:58:17

问题


I have collection database with 10 000 000 call records. I want to compare call usage of previous month to next month.

Example of collection document

{
    "_id" : ObjectId("54ed74d76c68d23af73e230a"),
    "msisdn" : "9818441000",
    "callType" : "ISD"
    "duration" : 10.109999656677246,
    "charges" : 200,
    "traffic" : "Voice",
    "Date" : ISODate("2014-01-05T19:51:01.928Z")
}
{
    "_id" : ObjectId("54ed74d76c68d23af73e230b"),
    "msisdn" : "9818843796",
    "callType" : "Local",
    "duration" : 1,
    "charges" : 150,
    "traffic" : "Voice",
    "Date" : ISODate("2014-02-04T14:25:35.861Z") 

}

Duration is my usage.

I want to compare duration of ISODate("2014-01-04T14:25:35.861Z") with next month ISODate("2014-02-04T14:25:35.861Z") of all records.

All msisdn number are same in both months.


回答1:


The obvious call here seems to be to aggregate the data, which in MongoDB the aggregation framework is well suited to. Taking the general use case fields that I see present here. And yes, we generally talk in terms of discrete months rather than some value assumed to be one month from the current point in time:

db.collection.aggregate([
    { "$match": {
        "msisdn": "9818441000",
        "Date": {
            "$gte": new Date("2014-01-01"),
            "$lt": new Date("2014-03-01")
        }
    }},
    { "$group": {
        "_id": {
            "year": { "$year": "$Date" },
            "month": { "$month": "$Date" },
            "callType": "$callType",
            "traffic": "$traffic"
        },
        "charges": { "$sum": "$charges" },
        "duration": { "$sum": "$duration" }
    }},
    { "$sort": { "_id": 1 } }
])

The intent there is to produce two records in the response representing each month as a distinct value.

You can basically take those two results and compare the difference between them in client code.

Or you can do this over all "MSISDN" values with months grouped into pairs within the document:

db.collection.aggregate([
    { "$match": {
        "Date": {
            "$gte": new Date("2014-01-01"),
            "$lt": new Date("2014-03-01")
        }
    }},
    { "$group": {
        "_id": {
            "year": { "$year": "$Date" },
            "month": { "$month": "$Date" },
            "msisdn": "$msisdn",
            "callType": "$callType",
            "traffic": "$traffic"
        },
        "charges": { "$sum": "$charges" },
        "duration": { "$sum": "$duration" }
    }},
    { "$sort": { "_id": 1 } },
    { "$group": {
        "_id": { 
            "msisdn": "$_id.msisdn",
            "callType": "$_id.callType",
            "traffic": "$_id.traffic"
        },
        "data": { "$push": {
            "year": "$_id.year",
            "month": "$_id.month",
            "charges": "$charges",
            "duration": "$duration"
        }}
    }}
])


来源:https://stackoverflow.com/questions/28963788/is-it-possible-to-compare-two-months-data-in-single-collection-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!