gpt4 book ai didi

python - 返回集合中 ISODate 值的最小值

转载 作者:太空宇宙 更新时间:2023-11-03 18:11:57 33 4
gpt4 key购买 nike

我需要返回集合中每个用户的最小 client_timestamp 值,但是我无法让 $min 运算符以这种方式运行。有没有办法让查询返回集合中所有 user_ids 的最小 client_timestamp ?

查询=???db.collection.find(query).distinct("client_timestamp")

文档结构示例:

{
"device_type" : "Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz (8173 MB)",
"user_id" : "17204977745451858462",
"organization_id" : "1",
"client_timestamp" : "2014-09-10T04:46:39.201Z",
"client_session_id" : "PpfprJalFTNDZa2Ag1wUQ5D2Mfw",
"_id" : "0de1611e-d835-4557-9948-cbbf88afa098"
}

{
"device_type" : "Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz (8173 MB)",
"user_id" : "17204977745451858462",
"organization_id" : "1",
"client_timestamp" : "2014-09-10T04:46:39.368Z",
"client_session_id" : "PpfprJalFTNDZa2Ag1wUQ5D2Mfw",
"_id" : "21e71acf-43e4-4a2d-b946-074fe7983034"
}

最佳答案

aggregation framework是寻找最小值的答案。它有$min分组运算符在 $group 中正是执行此操作管道阶段:

db.collection.aggregate([
{ "$group": {
"_id": None,
"client_timestamp": { "$min": "$client_timestamp" }
}}
])

分组是通过 _id 中指定的值或字段值组合来完成的,其中任何 Null 或空值都会对整个集合进行分组。

另请参阅SQL to Aggregation mapping chart在许多常见示例的文档中。

关于python - 返回集合中 ISODate 值的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25774257/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com