Mongodb query mysql到mongodb查询转换问题
我是mongodb的新手。我试图将这个mysql查询转换为mongodb,但我只能得到(appId&count)。实际上我想要(日期和计数) 原始查询Mongodb query mysql到mongodb查询转换问题,mongodb-query,Mongodb Query,我是mongodb的新手。我试图将这个mysql查询转换为mongodb,但我只能得到(appId&count)。实际上我想要(日期和计数) 原始查询 SELECT count(distinct appID),created_date FROM table where created_date between 2015-1-1 and 2015-1-20 group by created_date db.collection.aggregate( [{ $match : { cr
SELECT count(distinct appID),created_date
FROM table
where created_date between 2015-1-1 and 2015-1-20
group by created_date
db.collection.aggregate(
[{ $match : { createdDate: { $gt : ISODate("2015-05-01T00:00:00.000Z"), $lte : ISODate("2015-05-20T23:59:59.000Z")}}},
{ $group : { _id : { year: { $year: "$createdDate" },month: { $month: "$createdDate" },day: { $dayOfMonth : "$createdDate" },appId : "$appId"}}},
{ $group : { _id : { appIdd: '$_id.appId'}, count: { $sum: 1 }}},
{ $project: { _id : 0,appId: '$_id.appIdd', count: 1}}
])
mongodb查询
SELECT count(distinct appID),created_date
FROM table
where created_date between 2015-1-1 and 2015-1-20
group by created_date
db.collection.aggregate(
[{ $match : { createdDate: { $gt : ISODate("2015-05-01T00:00:00.000Z"), $lte : ISODate("2015-05-20T23:59:59.000Z")}}},
{ $group : { _id : { year: { $year: "$createdDate" },month: { $month: "$createdDate" },day: { $dayOfMonth : "$createdDate" },appId : "$appId"}}},
{ $group : { _id : { appIdd: '$_id.appId'}, count: { $sum: 1 }}},
{ $project: { _id : 0,appId: '$_id.appIdd', count: 1}}
])
有人能帮忙吗?我认为您应该能够通过以下查询获得所需的结果:
db.collection.aggregate(
[{ $match : { createdDate: { $gt : ISODate("2015-05-01T00:00:00.000Z"), $lte : ISODate("2015-05-20T23:59:59.000Z")}}},
{ $group : { _id : { year: { $year: "$createdDate" },month: { $month: "$createdDate" },day: { $dayOfMonth : "$createdDate" },appId : "$appId"}}},
{ $group : { _id : { appIdd: '$_id.appId'}, count: { $sum: 1 }}},
{ $project: { created_date_str: { $concat: ["$_id.year","-","$_id.month","-","$_id.day","T00:00:00Z"] }, count: 1}},
{ $project: { _id : 0, created_date: { ISODate("$created_date_str") }, count: 1}}
])