我在mongo中有简单的数据,collection.It看起来像这样:
{
"_id" : ObjectId("52b73b1318a7be441dbf36b6"),
"nme" : "Vinod Kumar",
"unm" : "vkumar",
"cat" : ISODate("2013-12-22T19:18:43.873Z"),
"act" : [{
"nme" : "test activity one",
"dec" : "This is only about test activity",
"gat" : ISODate("2013-12-25T19:17:00.873Z"),
"cat" : ISODate("2013-12-22T19:18:43.873Z")
}]
},
{
"_id" : ObjectId("52b73b1318a7be441dbf36b6"),
"nme" : "Manoj Kumar",
"unm" : "mkumar",
"cat" : ISODate("2013-12-22T19:18:43.873Z"),
"act" : [{
"nme" : "test activity three",
"dec" : "This is only about test activity",
"gat" : ISODate("2013-12-25T19:17:00.873Z"),
"cat" : ISODate("2013-12-20T19:18:43.873Z")
},
{
"nme" : "test activity two",
"dec" : "This is only about test activity",
"gat" : ISODate("2013-12-25T19:17:00.873Z"),
"cat" : ISODate("2013-12-21T19:18:43.873Z")
}]
}我想要获得所有行为(活动)的排序(创建时间)和(Unm)用户名。我还想添加分页功能,因此我需要查询too.Finally中的限制和偏移量,我期望输出:
{
"nme" : "test activity one",
"dec" : "This is only about test activity",
"gat" : ISODate("2013-12-25T19:17:00.873Z"),
"cat" : ISODate("2013-12-22T19:18:43.873Z"),
"unm" : "vkumar",
},
{
"nme" : "test activity two",
"dec" : "This is only about test activity",
"gat" : ISODate("2013-12-25T19:17:00.873Z"),
"cat" : ISODate("2013-12-21T19:18:43.873Z"),
"unm" : "mkumar",
},
{
"nme" : "test activity three",
"dec" : "This is only about test activity",
"gat" : ISODate("2013-12-25T19:17:00.873Z"),
"cat" : ISODate("2013-12-20T19:18:43.873Z"),
"unm" : "mkumar",
}我在这个收藏中有200000张唱片。谁可以帮助我而不建议创建单独的活动集合。
使用聚合框架有什么变通方法吗?如果有,我该怎么做?因为我需要使用排序、跳过和限制,所以我不知道如何使用聚合来实现最佳性能。
我尝试了map reduce,但这没有什么帮助,因为map reduce将为每个查询生成静态结果,并且使用应用程序的多个用户在使用分页(限制、跳过)时可能会在结果中发生冲突。
任何帮助或建议都将不胜感激。
谢谢
下面是我当前的查询
$m = new MongoClient();
$c = $m->selectDB("test")->selectCollection("users");
$ops = array(
array(
'$project' => array(
"unm" => 1,
"act" => 1
)
),
array('$sort' => array('act.cat' => -1 )),
array('$limit' => 10),
);
$results = $c->aggregate($ops);发布于 2013-12-24 06:21:26
这是我自己问题的答案。我现在意识到mongo有隐藏的力量:P
$m = new MongoClient();
$c = $m->selectDB("test")->selectCollection("users");
$ops = array(
array(
'$project' => array(
"unm" => 1,
"act" => 1
)
),
array('$unwind' => '$act'),
array('$match' => array('act.sta' => 1)), // if you want some extra queries. In my case I am checking if status of activity is 1
array('$sort' => array('act.cat' => -1 )), // sort by created time
array('$skip' => $page), // skip int variable
array('$limit' =>$config['per_page']), // limit int variable
);
$results = $c->aggregate($ops);
print_r($results);https://stackoverflow.com/questions/20735147
复制相似问题