I am trying to map documents to Mongoid / Mongodb, where the request uses array fields. I struggled with $elemMatch but didn't seem to be able to get it.
Context
- A
Project may have admin , member , reader users - These users link to
Project (HABTM) - I want to find projects where:
- User A
admin - User B
admin or member - .. etc.
Example
A Project document is provided from the 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 corresponds to current_user.id for all admin_ids and member_ids , if there was only one value in any of the arrays. According to the code above:
- Attempting to match user
'123' gives correct result - Attempting to map user
'456' does not produce result (incorrect)
$ elemMatch
Based on the research, I think I should use $elemMatch , but something is missing.
According to the Project 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
source share