如何在查询中使用数组字段匹配 mongoid 文档?

发布于 2025-01-07 18:30:32 字数 1687 浏览 2 评论 0原文

我正在尝试将文档与 Mongoid/Mongodb 相匹配,其中查询中使用数组字段。我一直在努力解决 $elemMatch 但似乎无法得到它。

上下文

  • 项目可以有adminmemberreader用户
  • 这些用户由项目<引用/code> (HABTM)
  • 我希望能够找到以下项目:
    • 用户 A 是管理员
    • 用户 B 是管理员成员
    • ..等等..

示例

给定一个 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_idsmember_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 have admin, 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 or member
    • .. etc ..

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北音执念 2025-01-14 18:30:32

您需要在查询时删除数组。

@projects = Project.any_of({:admin_ids => current_user.id}, 
                       {:member_ids => current_user.id}).entries

那应该有效。

You need to get rid of the array while querying.

@projects = Project.any_of({:admin_ids => current_user.id}, 
                       {:member_ids => current_user.id}).entries

That should work.

猫七 2025-01-14 18:30:32

我认为你应该能够使用 $in 而不是 $elemMatch - 作为 mongodocs 说:

“只有在必须匹配多个字段时才需要使用 [$elemMatch]
数组元素。”

您是否尝试过类似以下的操作?

Project.any_in(:member_ids =>  [ member_id_one, member_id_two ])

I think you should just be able to use $in rather than $elemMatch - as the mongodocs say:

"You only need to use [$elemMatch] when more than one field must be matched in
the array element."

Have you tried something like the following?

Project.any_in(:member_ids =>  [ member_id_one, member_id_two ])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文