对 MongoDB 结果进行排序
我在 MongoDB 中有以下结构(是一个示例结构):
Array
(
[_id] => MongoId Object
(
[$id] => 4e465de048177e8105000003
)
[id_usuario] => MongoId Object
(
[$id] => 4e43b20648177e5305000000
)
[mensaje] => lero lero si?
[created_at] => MongoDate Object
(
[sec] => 1313234400
[usec] => 160000
)
[comentarios] => Array
(
[0] => Array
(
[comentario] => hola mundo
[usuario] => [_id] => MongoId Object
(
[$id] => 4e465de048177e8105000003
)
[created_at] => MongoDate Object
(
[sec] => 1313234400
[usec] => 160000
)
)
[1] => Array
(
[comentario] => hola mundo
[usuario] => [_id] => MongoId Object
(
[$id] => 4e465de048177e8105000003
)
[created_at] => MongoDate Object
(
[sec] => 1313234400
[usec] => 160000
)
)
)
)
我想获取按created_at降序排序的最后 10 条评论,我写了以下内容:(
$db->usuarios->find()->sort(array('created_at' => -1))->limit(10)->skip(0);
这是驱动 MongoDB 的 PHP 代码)
任何人都可以帮我做同样的事情,但是还显示按照created_at降序排列的最后3条评论?
这可以在单个查询中实现吗?你能帮忙写一下代码吗?
I have the following structure in MongoDB (is an example structure):
Array
(
[_id] => MongoId Object
(
[$id] => 4e465de048177e8105000003
)
[id_usuario] => MongoId Object
(
[$id] => 4e43b20648177e5305000000
)
[mensaje] => lero lero si?
[created_at] => MongoDate Object
(
[sec] => 1313234400
[usec] => 160000
)
[comentarios] => Array
(
[0] => Array
(
[comentario] => hola mundo
[usuario] => [_id] => MongoId Object
(
[$id] => 4e465de048177e8105000003
)
[created_at] => MongoDate Object
(
[sec] => 1313234400
[usec] => 160000
)
)
[1] => Array
(
[comentario] => hola mundo
[usuario] => [_id] => MongoId Object
(
[$id] => 4e465de048177e8105000003
)
[created_at] => MongoDate Object
(
[sec] => 1313234400
[usec] => 160000
)
)
)
)
I want to get the last 10 comments sorted by created_at in descending and I write the following:
$db->usuarios->find()->sort(array('created_at' => -1))->limit(10)->skip(0);
(this is PHP code driving MongoDB)
Could anyone help me to do the same but also display the last 3 comments ordered by created_at in descending?
Is that possible in a single query? Can you help with code please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 php 不太了解,无法理解您所描述的文档结构。因此,我假设
comment
文档“嵌入”在您的usuario
文档中。如果是这样,那么您就无法直接处理
comment
文档。您将首先获取一个usuario
文档。这样,您将获得一组comment
文档,您必须在客户端对这些文档进行排序和过滤。或者,如果您的评论对象以特定顺序存储,您可以使用 $slice 来获取前 3 个或前 10 个或任何此类子集。更多信息:http://www.mongodb.org/display/DOCS/Retriving+a+Subset+of+Fields#RetrivingaSubsetofFields-RetrivingaSubrangeofArrayElements
I do not know php well enough to understand the document structure you have described. So I am assuming that the
comment
documents are "embedded" in yourusuario
documents.If it is true, then you cannot address the
comment
documents directly. You will have first fetch ausuario
document. With that you will get an array ofcomment
documents which you will have to sort and filter on the client side.Or if your comment objects are stored in a particular order you can use the $slice to fetch the first 3 or first 10 or any such subset. More info : http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields#RetrievingaSubsetofFields-RetrievingaSubrangeofArrayElements
该查询返回最后 10 个 usarios(用户)及其所有评论:
要获取最后 3 条评论,您必须按
' 排序commentarios.created_at'
然后过滤这些结果。像这样:请注意,find 返回 3 个用户及其评论,因此您必须找到这些用户之间最近的 3 个用户。
That query returns the last 10 usarios (users) and all of their comments:
To get the last 3 comments you have to sort by
'commentarios.created_at'
and then filter those results. Like this:Notice that find returns 3 users and their comments, so you have to find the 3 most recent between those users.