是否可以在 MongoDB 的单个集合中比较两个月的数据?
is It possible to compare two Months Data in single Collection in MongoDB?
我有包含 10 000 000 条通话记录的收集数据库。
我想比较上个月和下个月的通话使用情况。
收款文件示例
{
"_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")
}
持续时间是我的用法。
我想将 ISODate("2014-01-04T14:25:35.861Z")
的持续时间与所有记录的下个月 ISODate("2014-02-04T14:25:35.861Z")
的持续时间进行比较。
所有 msisdn
个数字在两个月中都相同。
这里明显的调用似乎是聚合数据,在 MongoDB 中,聚合框架非常适合。采用我在这里看到的一般用例字段。是的,我们通常用离散的月份来讨论,而不是假设从当前时间点算起一个月的某个值:
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 } }
])
目的是在响应中生成两条记录,将每个月表示为不同的值。
您基本上可以获取这两个结果并在客户端代码中比较它们之间的差异。
或者您可以对所有 "MSISDN" 个值执行此操作,其中月份在文档中成对分组:
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"
}}
}}
])
我有包含 10 000 000 条通话记录的收集数据库。 我想比较上个月和下个月的通话使用情况。
收款文件示例
{
"_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")
}
持续时间是我的用法。
我想将 ISODate("2014-01-04T14:25:35.861Z")
的持续时间与所有记录的下个月 ISODate("2014-02-04T14:25:35.861Z")
的持续时间进行比较。
所有 msisdn
个数字在两个月中都相同。
这里明显的调用似乎是聚合数据,在 MongoDB 中,聚合框架非常适合。采用我在这里看到的一般用例字段。是的,我们通常用离散的月份来讨论,而不是假设从当前时间点算起一个月的某个值:
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 } }
])
目的是在响应中生成两条记录,将每个月表示为不同的值。
您基本上可以获取这两个结果并在客户端代码中比较它们之间的差异。
或者您可以对所有 "MSISDN" 个值执行此操作,其中月份在文档中成对分组:
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"
}}
}}
])