本文介绍了如何使用 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 聚合和检索整个文档
发布评论