I am trying to match documents with Mongoid/Mongodb where array fields are used in the query. I've been struggling with $elemMatch but can't seem to get it.
Context
- A
Projectcan haveadmin,member,readerusers - These users are referenced by a
Project(HABTM) - I want to be able to find projects where:
- User A is
admin - User B is
adminormember - .. etc ..
- User A is
Example
Given a Project document from Rails console:
[#<Project _id: 4f44355a9f5b7f385a000003,
_type: nil, name: "Project ABC",
desc: "some description",
admin_ids:
[BSON::ObjectId('123')],
member_ids:
[BSON::ObjectId('456'),
BSON::ObjectId('789')],
reader_ids: []
>]
I had the following code:
@projects = Project.any_of({:admin_ids => [current_user.id]},
{:member_ids => [current_user.id]}).entries
Which matches current_user.id across either admin_ids and member_ids as long as there was only a single value in either of the arrays. As per the code above:
- Trying to match user
'123'gives correct result - Trying to match user
'456'gives no result (incorrect)
$elemMatch
Based on researching, I think I should be using $elemMatch but am missing something.
As per the Project document code above:
// test case: this works with array of one
Project.all(conditions: {:admin_ids => "123"}).entries
// failure case: empty result
Project.all(conditions: {:member_ids => {'$elemMatch' => {:id => '456' } }}).entries
// failure case: empty result
Project.all(conditions: {:member_ids => {'$elemMatch' => {:id => BSON::ObjectId('4f44a4019f5b7f3d5200000d') } }}).entries
$inrather than$elemMatch- as the mongodocs say: "You only need to use this when more than one field must be matched in the array element."