我是mongo的新手,我尝试获取身份证明文件给出的一组,sql中的等价物将具有以下形式:
SELECT fecIni, idc , cic
from db.cllAuditoria
WHERE fecIni BETWEEN '2017-07-01T05:00:00.000Z' AND '2017-07-31T05:00:00.000Z'
group by idc , cic
请朋友们,这对我帮助很多我从不同来源研究过而没有提出解决方案,我提前谢谢你。
答案 0 :(得分:1)
假设U有像:
这样的集合
/* 1 */
{
"_id" : ObjectId("5a58dafe1aadbcd8ae74572e"),
"fecIni" : ISODate("2017-07-30T05:00:00.000Z"),
"idc" : "idc 1",
"cic" : "cic 1"
}
/* 2 */
{
"_id" : ObjectId("5a58dc3a1aadbcd8ae745758"),
"fecIni" : ISODate("2017-07-29T05:00:00.000Z"),
"idc" : "idc 1",
"cic" : "cic 1"
}
/* 3 */
{
"_id" : ObjectId("5a58dc671aadbcd8ae74575e"),
"fecIni" : ISODate("2017-07-28T05:00:00.000Z"),
"idc" : "idc 1",
"cic" : "cic 2"
}

按idc分组,cic字段可以通过查询执行:
db.cllAuditoria.aggregate([
{
$match : {
fecIni: {
$gte: ISODate("2017-07-01T05:00:00.000Z"),
$lte: ISODate("2017-07-31T05:00:00.000Z")
}
}
},
{ $group : { _id : { "idc": "$idc", "cic" : "$cic"} } }
]);

结果将会看起来:
/* 1 */
{
"_id" : {
"idc" : "idc 1",
"cic" : "cic 2"
}
}
/* 2 */
{
"_id" : {
"idc" : "idc 1",
"cic" : "cic 1"
}
}

如果您想要计算群组中的项目:
db.cllAuditoria.aggregate([
{
$match : {
fecIni: {
$gte: ISODate("2017-07-01T05:00:00.000Z"),
$lte: ISODate("2017-07-31T05:00:00.000Z")
}
}
},
{ $group : {
_id : { "idc": "$idc", "cic" : "$cic"},
count: { $sum: 1 }
} }
]);

结果:
/* 1 */
{
"_id" : {
"idc" : "idc 1",
"cic" : "cic 2"
},
"count" : 1.0
}
/* 2 */
{
"_id" : {
"idc" : "idc 1",
"cic" : "cic 1"
},
"count" : 2.0
}

希望,它会对你有帮助。如果你的数据有所不同,请提供收集的例子以及你想要得到的结果。