简体   繁体   中英

MongoDB query using map-reduce/Aggregation?

I have simple data in mongo collection.It looks like:

{
"_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")
    }]
}

I want to get all act(activities) orderby cat(created time) along with (unm)username.I also want to add pagination feature so I would need limit and offset in query too.Finally I am expecting output:

        {
    "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",
    }

I have 200000 records in this collection. Can anybody able to help me without suggesting creating activities collection separate.

Is there any workaround using aggregation framework if yes how would I do? As I need to use sort, skip and limit, I dont know how to do that with aggregation with optimal performance.

I tried map reduce but that would not help as map reduce will generate static results for each query and multiple users using application might conflict in result while using pagination (limit, skip).

Any help or suggestion will be appreciated.

Thanks

Here is my current query

$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);

Here is answer of my own question. I now realised mongo have hidden powers :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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM