如何在查询中使用数组字段匹配 mongoid 文档?
我正在尝试将文档与 Mongoid/Mongodb 相匹配,其中查询中使用数组字段。我一直在努力解决 $elemMatch
但似乎无法得到它。
上下文
项目
可以有admin
、member
、reader
用户- 这些用户由
项目<引用/code> (HABTM)
- 我希望能够找到以下项目:
- 用户 A 是
管理员
- 用户 B 是
管理员
或成员
- ..等等..
- 用户 A 是
示例
给定一个 Project
来自Rails控制台的文档:
[#<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: []
>]
我有以下代码:
@projects = Project.any_of({:admin_ids => [current_user.id]},
{:member_ids => [current_user.id]}).entries
只要admin_ids
和member_ids
匹配current_user.id
/em> 两个数组中都只有一个值。根据上面的代码:
- 尝试匹配用户
'123'
给出正确的结果 - 尝试匹配用户
'456'
没有给出结果(不正确)
$elemMatch
基于研究中,我认为我应该使用 $elemMatch
但缺少一些东西。
根据上面的 Project
文档代码:
// 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
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
Project
can haveadmin
,member
,reader
users - These users are referenced by a
Project
(HABTM) - I want to be able to find projects where:
- User A is
admin
- User B is
admin
ormember
- .. 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在查询时删除数组。
那应该有效。
You need to get rid of the array while querying.
That should work.
我认为你应该能够使用
$in
而不是 $elemMatch - 作为 mongodocs 说:您是否尝试过类似以下的操作?
I think you should just be able to use
$in
rather than $elemMatch - as the mongodocs say:Have you tried something like the following?