mongodb按照meterId分组查询communityId=46并且meteringTime在2023-09-06 000000和2023-09-06 235959之间的数据写出语句
以下是使用MongoDB查询语句按照meterId分组,查询communityId=46并且meteringTime在2023-09-06 00:00:00和2023-09-06 23:59:59之间的数据的示例:
db.collection.aggregate([
{
$match: {
communityId: 46,
meteringTime: {
$gte: ISODate("2023-09-06T00:00:00Z"),
$lte: ISODate("2023-09-06T23:59:59Z")
}
}
},
{
$group: {
_id: "$meterId",
data: { $push: "$$ROOT" }
}
}
])
请将collection替换为你实际的集合名称。这个查询语句使用了聚合管道(aggregate pipeline),首先使用$match阶段筛选出符合条件的数据,然后使用$group阶段按照meterId分组并将数据保存在data字段中
原文地址: http://www.cveoy.top/t/topic/i1zU 著作权归作者所有。请勿转载和采集!