如何使用 mongodb 聚合和检索整个文档

编程入门 行业动态 更新时间:2024-10-24 22:31:13
本文介绍了如何使用 mongodb 聚合和检索整个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对 mongodb 的聚合函数感到非常困惑.我只想在我的收藏中找到最新的文档.假设每条记录都有一个已创建"字段

I am seriously baffled by mongodb's aggregate function. All I want is to find the newest document in my collection. Let's say each record has a field "created"

db.collection.aggregate({ $group: { _id:0, 'id':{$first:"$_id"}, 'max':{$max:"$created"} } })

产生正确的结果,但我想要结果中的整个文档?我该怎么做?

yields the correct result, but I want the entire document in the result? How would I do that?

这是文档的结构:

{ "_id" : ObjectId("52310da847cf343c8c000093"), "created" : 1389073358, "image" : ObjectId("52cb93dd47cf348786d63af2"), "images" : [ ObjectId("52cb93dd47cf348786d63af2"), ObjectId("52f67c8447cf343509d63af2") ], "organization" : ObjectId("522949d347cf3402c3000001"), "published" : 1392601521, "status" : "PUBLISHED", "tags" : [ ], "updated" : 1392601521, "user_id" : ObjectId("52214ce847cf344902000000") }

推荐答案

在文档中我发现 $$ROOT 表达式解决了这个问题.

In the documentation i found that the $$ROOT expression addresses this problem.

来自文档:docs.mongodb/手册/参考/操作员/聚合/组/#group-documents-by-author

更多推荐

如何使用 mongodb 聚合和检索整个文档

本文发布于:2023-11-22 18:42:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   文档   mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!