1

I have a really simple database-setup: one collection/ archive of all available documents, and users that on specific dates relate to a specific document/ archive _id.

student_123 exams (collection):

{
_id: "2015-02-05-1430162122744",
exam_id: "ex-34"
},
{
_id: "2014-12-05-1430162122744",
exam_id: "ex-981"
},
{
_id: "2015-04-08-1430162148984",
exam_id: "ex-34"
},
{
_id: "2015-01-01-1430162148984",
exam_id: "ex-2"
} ...


all exams (collection):

{
_id: "ex-1",
name: "exam1"
} ...
{
_id: "ex-1003",
name: "exam1003"
}

what i'm trying to achieve is to list all exam names by a descending date-sorted (student) exams-log. which means the log must be sorted (not registered chronologically) AND some exams must be listed several times (several attempts).

the easy and really dirty solution is this:

$test = $collection_student123->find();
$test->sort(array('_id' => -1));
foreach ( $test as $id => $value ) {
        $cursor = $collection_exams->findOne(array( '_id' => $value['exam_id'] ));
        echo $cursor['name'];
}

my "sophisticated" attempt fails, though:

$sorted = [];
$test = $collection_student123->find();
$test->sort(array('_id' => 1));
foreach ( $test as $id => $value ) {
        $sorted[] = $value['exam_id'];
}
$cursor = $collection_exams->find(array(
    'id' => array('$in' => $sorted)
));

foreach ( $cursor as $id => $value )
{
    echo $value['name'];
}
  • 1: The result is not sorted as in $sorted
  • 2: The $collection_exams->find function does NOT return identical IDs (eg. 2nd attempt on same exam ID)

Any clever ideas on how to achieve this? Thanks.

EDIT: The reason i'm interested is that after only 10 iterations the "sophisticated" method is 5x faster than the "dirty" one

frdnrdb
  • 61
  • 8
  • I'm having a bit of trouble understanding what you mean by not returning identical ids, find() should dump everything regardless of duplicate id? Also as a tip, you can use ->sort() on the same line as find() – Digits Apr 27 '15 at 20:18
  • the first method will naturally search and find "ex-34" 2 times and chronologically by date-id. the second method lists the exams in the same order as which they are stored and it only lists "ex-34" once. ideas? thanks for the ->sort()->find() tip! – frdnrdb Apr 27 '15 at 20:24
  • Oh, you're doing something that's called using a foreign key. It's not recommended in mongodb which has really poor support of it. You should read up what others have said http://stackoverflow.com/questions/6334048/foreign-keys-in-mongo . I recommend denormalizing your data and having exams inside students. Alternatively, you can start using aggregation queries which will start collecting up all the data you want in one query. – Digits Apr 27 '15 at 20:35
  • Actually, I just thought of something. You can find multiple values with $in. http://stackoverflow.com/questions/8303900/mongodb-mongoose-findmany-find-all-documents-with-ids-listed-in-array It can also be sorted, which will work out for you. – Digits Apr 28 '15 at 18:39

0 Answers0