Python PyMongo按日期计算元素数
我与我的用户有数据库:Python PyMongo按日期计算元素数,python,mongodb,pymongo,aggregate-functions,Python,Mongodb,Pymongo,Aggregate Functions,我与我的用户有数据库: { "_id": { "$oid": "5a0decadefcb09087c08a868" }, "user_id": "5b232a5a-b333-4320-ba63-722b9e167ef3", "email": "email@email.com", "password": "***", "registration_date": { "$date": "2017-11-16T19:53:1
{
"_id": {
"$oid": "5a0decadefcb09087c08a868"
},
"user_id": "5b232a5a-b333-4320-ba63-722b9e167ef3",
"email": "email@email.com",
"password": "***",
"registration_date": {
"$date": "2017-11-16T19:53:17.946Z"
},
"type": "user"
},
{
"_id": {
"$oid": "5a0ded3aefcb090887d7f4fb"
},
"user_id": "0054bbde-3ba0-490f-8d54-ffaf72958888",
"email": "second@gmail.com",
"password": "***",
"registration_date": {
"$date": "2017-11-16T19:55:38.194Z"
},
"type": "user"
}
我想按每个日期(注册日期)计算用户数,并得到如下结果:
01.01.2017 – 10
01.02.2017 – 20
01.03.2017 – 15
...
我正在尝试该代码,但它不起作用:
def registrations_by_date(self):
users = self.users_db.aggregate([
{'$group': {
'_id': {'registration_date':'$date'},
'count': {'$sum':1}
}},
])
return users
我做错了什么?如何获取这些数据 看来你有一个额外的,
db.userReg.aggregate([
{$group: {_id: "$registration_date", count: {$sum:1}}}
])
这将给出正确的结果(基于我的McHine上的记录):
{
“_id”:ISODate(“2017-11-15T19:55:38.194Z”),
“计数”:1.0}
{
“_id”:ISODate(“2017-11-16T19:55:38.194Z”),
“计数”:2.0}
似乎您有一个额外的,
db.userReg.aggregate([
{$group: {_id: "$registration_date", count: {$sum:1}}}
])
这将给出正确的结果(基于我的McHine上的记录):
{
“_id”:ISODate(“2017-11-15T19:55:38.194Z”),
“计数”:1.0}
{
“_id”:ISODate(“2017-11-16T19:55:38.194Z”),
“计数”:2.0}
如果架构中的日期为ISODate
然后,下面的聚合查询将工作,在分组之前完成日期格式,以便在分组数据时不使用时间戳
{
"_id" : "5a0decadefcb09087c08a868",
"user_id" : "5b232a5a-b333-4320-ba63-722b9e167ef3",
"email" : "email@email.com",
"password" : "***",
"registration_date" : ISODate("2017-11-16T19:53:17.946Z"),
"type" : "user"
}
{
"_id" : "5a0ded3aefcb090887d7f4fb",
"user_id" : "0054bbde-3ba0-490f-8d54-ffaf72958888",
"email" : "second@gmail.com",
"password" : "***",
"registration_date" : ISODate("2017-11-16T19:55:38.194Z"),
"type" : "user"
}
获取结果的聚合查询是
db.userReg.aggregate([
{$project:
{ formattedRegDate:
{ "$dateToString": {format:"%Y-%m-%d", date:"$registration_date"}}
}
},
{$group:{_id:"$formattedRegDate", count:{$sum:1}}}]);
{ "_id" : "2017-11-16", "count" : 2 }
{ "_id" : { "date" : "2017-11-16" }, "count" : 2 }
结果是
db.userReg.aggregate([
{$project:
{ formattedRegDate:
{ "$dateToString": {format:"%Y-%m-%d", date:"$registration_date"}}
}
},
{$group:{_id:"$formattedRegDate", count:{$sum:1}}}]);
{ "_id" : "2017-11-16", "count" : 2 }
{ "_id" : { "date" : "2017-11-16" }, "count" : 2 }
如果架构中的日期为字符串
然后使用下面的方法
样本数据
{
"_id" : "5a0decadefcb09087c08a868",
"user_id" : "5b232a5a-b333-4320-ba63-722b9e167ef3",
"email" : "email@email.com",
"password" : "***",
"registration_date" : "2017-11-16T19:53:17.946Z",
"type" : "user"
}
{
"_id" : "5a0ded3aefcb090887d7f4fb",
"user_id" : "0054bbde-3ba0-490f-8d54-ffaf72958888",
"email" : "second@gmail.com",
"password" : "***",
"registration_date" : "2017-11-16T19:55:38.194Z",
"type" : "user"
}
质疑
结果是
db.userReg.aggregate([
{$project:
{ formattedRegDate:
{ "$dateToString": {format:"%Y-%m-%d", date:"$registration_date"}}
}
},
{$group:{_id:"$formattedRegDate", count:{$sum:1}}}]);
{ "_id" : "2017-11-16", "count" : 2 }
{ "_id" : { "date" : "2017-11-16" }, "count" : 2 }
如果架构中的日期为ISODate
然后,下面的聚合查询将工作,在分组之前完成日期格式,以便在分组数据时不使用时间戳
{
"_id" : "5a0decadefcb09087c08a868",
"user_id" : "5b232a5a-b333-4320-ba63-722b9e167ef3",
"email" : "email@email.com",
"password" : "***",
"registration_date" : ISODate("2017-11-16T19:53:17.946Z"),
"type" : "user"
}
{
"_id" : "5a0ded3aefcb090887d7f4fb",
"user_id" : "0054bbde-3ba0-490f-8d54-ffaf72958888",
"email" : "second@gmail.com",
"password" : "***",
"registration_date" : ISODate("2017-11-16T19:55:38.194Z"),
"type" : "user"
}
获取结果的聚合查询是
db.userReg.aggregate([
{$project:
{ formattedRegDate:
{ "$dateToString": {format:"%Y-%m-%d", date:"$registration_date"}}
}
},
{$group:{_id:"$formattedRegDate", count:{$sum:1}}}]);
{ "_id" : "2017-11-16", "count" : 2 }
{ "_id" : { "date" : "2017-11-16" }, "count" : 2 }
结果是
db.userReg.aggregate([
{$project:
{ formattedRegDate:
{ "$dateToString": {format:"%Y-%m-%d", date:"$registration_date"}}
}
},
{$group:{_id:"$formattedRegDate", count:{$sum:1}}}]);
{ "_id" : "2017-11-16", "count" : 2 }
{ "_id" : { "date" : "2017-11-16" }, "count" : 2 }
如果架构中的日期为字符串
然后使用下面的方法
样本数据
{
"_id" : "5a0decadefcb09087c08a868",
"user_id" : "5b232a5a-b333-4320-ba63-722b9e167ef3",
"email" : "email@email.com",
"password" : "***",
"registration_date" : "2017-11-16T19:53:17.946Z",
"type" : "user"
}
{
"_id" : "5a0ded3aefcb090887d7f4fb",
"user_id" : "0054bbde-3ba0-490f-8d54-ffaf72958888",
"email" : "second@gmail.com",
"password" : "***",
"registration_date" : "2017-11-16T19:55:38.194Z",
"type" : "user"
}
质疑
结果是
db.userReg.aggregate([
{$project:
{ formattedRegDate:
{ "$dateToString": {format:"%Y-%m-%d", date:"$registration_date"}}
}
},
{$group:{_id:"$formattedRegDate", count:{$sum:1}}}]);
{ "_id" : "2017-11-16", "count" : 2 }
{ "_id" : { "date" : "2017-11-16" }, "count" : 2 }
有一个问题,它没有按天递增进行排序,“日期”:“2017-11-27”“日期”:“2017-11-21”“日期”:“2017-11-20”“日期”:“2017-11-26”对日期字段使用$sort聚合管道,
{$sort:{date 1}}
对升序和{$sort:{date 1}}
对降序返回相同的顺序(-1或1无差异)–在Python端修复它。再次感谢!有一个问题,它没有按天递增进行排序,“日期”:“2017-11-27”“日期”:“2017-11-21”“日期”:“2017-11-20”“日期”:“2017-11-26”对日期字段使用$sort聚合管道,{$sort:{date 1}}
对升序和{$sort:{date 1}}
对降序返回相同的顺序(-1或1无差异)–在Python端修复它。再次感谢!