我有一个包含事件的集合。每个事件都有精确到毫秒的时间戳。我想将每天的事件分组。 例如:
我有
key value _id 111222333444555ddd666fff time 2023-04-23t15:35:19.631z type pizza-event _id 111222333444555ddd666fff time 2023-04-23t01:41:20.631z type tv-event _id 111222333444555ddd666fff time 2023-04-22t05:00:05.631z type some-event
我愿意
key value date 04-22-2023 count 1.0 date 04-23-2023 count 2.0
最终目标是在 golang 项目中使用查询。
到目前为止我已经
[ { "$match" : { "$and" : [ { "type" : "foo.bar.event" }, { "time" : { "$gte" : isodate("2023-04-23t00:00:00.000+0000") } }, { "time" : { "$lte" : isodate("2023-04-25t00:00:00.000+0000") } } ] } }, { "$group" : { "_id" : { "$datetostring" : { "format" : "%m-%d-%y", "date" : "$time" } }, "count" : { "$sum" : 1.0 } } } ]
返回
key value _id 04-24-2023 count 476.0 _id 04-23-2023 count 28.0
这本来可以工作,但是当我在 go 项目中编写此查询时,“$datetostring”下会出现红色波浪线,并显示消息“无效的字段名称”,理想情况下我希望日期有一个“时间”键而不是“_id”。当我在小组赛阶段进行以下更改时:
{ _id: null, "date": {"$dateToString": { "format": "%m-%d-%Y", "date": "$time"}}, "count": {"$sum": 1} }
我收到未知组运算符“$datetostring”错误。因此,我想到创建组,然后为“$datetostring”添加一个项目阶段,但现在小组阶段每毫秒都会返回组,这违背了分组的目的。
我意识到我正在讨论两个不同的问题。然而,虽然一切方面的帮助都会很棒,但这个问题具体是关于修复 mongo 查询的。如果有必要,我会在另一个线程上返回 golang 编码。 如果我能说得更清楚,请告诉我。
正确答案
首先,如果您有一个应用程序 ui 来显示查询结果,则不必费心格式化查询中的输出。这是应用程序 ui 的职责。顺便说一句,如果您有应用程序 ui,请考虑使用 $datetrunc
而不是 $datetostring
。
无论如何,关于您问题中的要求,像这样的 $project
阶段应该适合您:
[ { "$group": { "_id": { "$datetostring": { "date": "$time", "format": "%m-%d-%y" } }, "count": { "$sum": 1 } } }, { "$project": { "_id": 0, "time": "$_id", "count": "$count" } } ]
mongodb shell 的输出:
{ "time" : "02-08-2020", "count" : 2 } { "time" : "05-18-2020", "count" : 2 } { "time" : "03-20-2021", "count" : 3 } { "time" : "01-11-2021", "count" : 1 }
关于在go项目中使用查询,这里有一个演示:
package main import ( "context" "fmt" "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func main() { ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017")) if err != nil { panic(err) } coll := client.Database("baz").Collection("cakeSales") matchStage := bson.D{ {"$match", bson.D{ {"$and", []bson.D{ {{"time", bson.D{ {"$gte", time.Date(2019, 6, 1, 0, 0, 0, 0, time.UTC)}, }}}, {{"time", bson.D{ {"$lte", time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC)}, }}}, }}, }}, } groupStage := bson.D{ {"$group", bson.D{ {"_id", bson.D{ {"$dateToString", bson.D{ {"date", "$time"}, {"format", "%m-%d-%Y"}, }}, }}, {"count", bson.D{ {"$sum", 1}, }}, }}, } projectStage := bson.D{ {"$project", bson.D{ {"_id", 0}, {"time", "$_id"}, {"count", "$count"}, }}, } cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{matchStage, groupStage, projectStage}) if err != nil { panic(err) } var results []bson.M if err = cursor.All(context.TODO(), &results); err != nil { panic(err) } for _, result := range results { fmt.Printf( "time: %s count: %v\n", result["time"], result["count"]) } }
以上是检索 MongoDB 聚合上每个日期的项目计数的详细内容。更多信息请关注PHP中文网其他相关文章!

Golang更适合高并发任务,而Python在灵活性上更有优势。1.Golang通过goroutine和channel高效处理并发。2.Python依赖threading和asyncio,受GIL影响,但提供多种并发方式。选择应基于具体需求。

Golang和C 在性能上的差异主要体现在内存管理、编译优化和运行时效率等方面。1)Golang的垃圾回收机制方便但可能影响性能,2)C 的手动内存管理和编译器优化在递归计算中表现更为高效。

selectgolangforhighpperformanceandcorrency,ifealforBackendServicesSandNetwork程序; selectpypypythonforrapiddevelopment,dataScience和machinelearningDuetoitsverserverserverserversator versator anderticality andextility andextentensivelibraries。

Golang和Python各有优势:Golang适合高性能和并发编程,Python适用于数据科学和Web开发。 Golang以其并发模型和高效性能着称,Python则以简洁语法和丰富库生态系统着称。

Golang和Python分别在哪些方面更易用和学习曲线更平缓?Golang更适合高并发和高性能需求,学习曲线对有C语言背景的开发者较平缓。Python更适合数据科学和快速原型设计,学习曲线对初学者非常平缓。

Golang和C 在性能竞赛中的表现各有优势:1)Golang适合高并发和快速开发,2)C 提供更高性能和细粒度控制。选择应基于项目需求和团队技术栈。

Golang适合快速开发和并发编程,而C 更适合需要极致性能和底层控制的项目。1)Golang的并发模型通过goroutine和channel简化并发编程。2)C 的模板编程提供泛型代码和性能优化。3)Golang的垃圾回收方便但可能影响性能,C 的内存管理复杂但控制精细。

GoimpactsdevelopmentPositationalityThroughSpeed,效率和模拟性。1)速度:gocompilesquicklyandrunseff,ifealforlargeprojects.2)效率:效率:ITScomprehenSevestAndArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdEcceSteral Depentencies,增强开发的简单性:3)SimpleflovelmentIcties:3)简单性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

Atom编辑器mac版下载
最流行的的开源编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器