OK, so case scenario is I have a collection of Product Categories, with each document similar to:
Category {
_id => ...,
name => ...,
products => array(
0 => new MongoID(PID...1),
1 => new MongoID(PID...2),
2 => new MongoID(PID...3),
....
)
}
And a collection of Products:
Product {
_id => ...,
name => ...,
active => true,
status => 'published',
categories => array(
0 => new MongoID(CATID...1),
1 => new MongoID(CATID...2),
2 => new MongoID(CATID...3)
)
}
I am refactoring my code because at the moment because when I do any limiting / offsetting, I have to return all products in a category as objects, then unset them if they aren't active (there are a couple more content matches, but I'm leaving them out for brevity). Obviously, we're far from optimised at this stage.
I have built a conditional query that looks like this:
array(
'$and' => array(
array(
'$or' => array(
0 => array(
'_id' => new MongoID(PID...1)
),
1 => array(
'_id' => new MongoID(PID...2)
),
2 => array(
'_id' => new MongoID(PID...3)
),
...
)
),
array(
'$and' => array(
array(
'active' => true
),
array(
'status' => 'published'
)
)
)
)
)
What I need to do is respect the order of the _id's in the first part of the query and offset / limit based on which documents match the second part in the sequence of the _id's.
The query ultimately ends up in a find() command but setting a sort order obviously won't work, because we're not sorting.
Short of splitting some fields from the Product and adding to Category->products[] I'm struggling to find a way to do this. Is it simply a case of handling it with MapReduce /aggregation now or is there a simpler alternative?
Thanks!