【问题标题】:Rails Mongoid select where key/value pair in arrayRails Mongoid 选择数组中键/值对的位置
【发布时间】:2013-06-16 20:25:35
【问题描述】:

所以我有一个集合Groups,看起来像这样

[
    {
        \"_id\": \"51bdff3968c7c4dd30000003\",
        \"members\": [
            {
                \"id\": \"51bdedef68c7c4bc7c000001\",
                \"role\": \"admin\"
            },
            {
                \"id\": \"51be0d4568c7c473ef000007\",
                \"role\": \"user\"
            }
        ],
        \"name\": \"tetsing2\"
    },
    {
        \"_id\": \"51bdf97868c7c46604000002\",
        \"members\": [
            {
                \"id\": \"51be12ae68c7c4dcce000001\",
                \"role\": \"user\"
            },
            {
                \"id\": \"51be12db68c7c45e08000002\",
                \"role\": \"user\"
            }
        ],
        \"name\": \"ds\"
    }
]" 

我只是获取成员数组中 id 等于 51be12db68c7c45e08000002 的对象

我可以用这个命令在控制台mongo客户端中做到这一点

db.groups.find({ "members.id": ObjectId("51be12db68c7c45e08000002") })

它只返回一个名为'ds'的适当对象

但是在 Rails 中使用 Mongoid,当我尝试类似这样的命令时:

@groups = Group.where(' { "members.id": "'+current_user.id+'"}')

我拿回了两个对象。

我查看了文档,但有谁知道如何通过 Mongoid 实现这一点,或者我可以通过 Monogoid 或轻便摩托车或其他方式运行控制台命令吗?

谢谢

【问题讨论】:

  • 为什么要将条件包裹在字符串中?

标签: ruby-on-rails mongodb mongoid moped


【解决方案1】:
Group.where( :"members.id" => current_user.id )

【讨论】:

    猜你喜欢
    • 2018-06-01
    • 2012-04-08
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    相关资源
    最近更新 更多