使用 mongoid 选择子元素

发布于 2025-01-01 08:14:20 字数 2091 浏览 0 评论 0原文

我有一个结构,其中

参与者将嵌入许多“Had_drinks”对象。每个“Had_drink”都有可能(并非总是)嵌入一个“照片”对象...

我有一张照片的 ID 和想要拉出整个照片对象。文档的根从参与者开始

。这是一个结构示例。下面是两个参与者,每个参与者只有一个“had_drink”,在这个示例中,第一个参与者有一张与他们的照片,

{
    _id: "4f0ecfe57e65260001000002",
    had_drinks: [
        {
        _id: "4f0ed1357e65260001000004",
        at: "2012-01-07T00:00:00-08:00",
        beer_id: "4f0ed1357e65260001000003",
        cost: 3.5,
        location: "Quakers",
        photo: {
            photo_id: "66832993035",
            page_url: "http://www.flickr.com/photos/733355f69@N02/66832993035/",
            square_url: "http://farm8.staticflickr.com/7035/668a3993035_fb180e39648_s.jpg",
            thumbnail_url: "http://farm8.staticflickr.com/7035/6sad683993035_fb65180e9648_t.jpg",
            small_url: "http://farm8.staticflickr.com/7035/66839asd93035_fb1s80e9648_m.jpg",
            medium_url: "http://farm8.staticflickr.com/7035/668s3993035_fb18h0e9648.jpg",
            large_url: "http://farm8.staticflickr.com/7035/66839v93035_fb180e9648_b.jpg",
            original_url: null
            },
        rating: "4",
        uploaded_at: "2012-01-12T04:25:25-08:00"
        }
    ],
    mongoid_user_id: "4f0ecf3e57e65260001000001",
    name: "Michael Mark"
},
{
    _id: "4f08aa477eddd05000100000c",
    had_drinks: [
    {
        _id: "4f0ff0513942d4e000100000b",
        at: "2012-01-07T030:00:00-08:00",
        beer_id: "4f0ff0561394d74e0001000009",
        cost: 4.1,
        location: "Bootleggers",
        photo: null,
        rating: "3",
        uploaded_at: "2012-01-13T030:50:25-08:00"
    }
    ],
    mongoid_user_id: "4f08aa477e34ddd0000100000b",
    name: "Daniel M"
},

我怎样才能实现将照片取出来? 根据我在短时间内学到的 mongoid 和文档数据库的知识,我需要从根开始查询并向下工作?

我正在尝试这样的事情

Participant.where(:had_drinks.where(:photo.photo_id => params[:id]))

但我认为到目前为止我还很遥远

----编辑:) 经过思考,我想我需要执行以下查询

1:获取任何带有此照片的参与者

2:选择第一个参与者

3:将他们的照片过滤为具有此 id 的照片

4:选择第一张照片(实际上只会是一张)

只是不确定如何以我有限的 mongoid/mongodb 知识进行该查询

I have the structure where

A Participant will have embedded many "Had_drinks" objects.. each "Had_drink" has the possibility (not always) of having a single "Photo" object embedded in it...

I have the ID of a Photo and want to pull out the entire of the photo object. The root of document starts at Participant

Here is a example of the structure.. Below is TWO participants each only have one "had_drink" in this example and the first participant has a photo with theirs

{
    _id: "4f0ecfe57e65260001000002",
    had_drinks: [
        {
        _id: "4f0ed1357e65260001000004",
        at: "2012-01-07T00:00:00-08:00",
        beer_id: "4f0ed1357e65260001000003",
        cost: 3.5,
        location: "Quakers",
        photo: {
            photo_id: "66832993035",
            page_url: "http://www.flickr.com/photos/733355f69@N02/66832993035/",
            square_url: "http://farm8.staticflickr.com/7035/668a3993035_fb180e39648_s.jpg",
            thumbnail_url: "http://farm8.staticflickr.com/7035/6sad683993035_fb65180e9648_t.jpg",
            small_url: "http://farm8.staticflickr.com/7035/66839asd93035_fb1s80e9648_m.jpg",
            medium_url: "http://farm8.staticflickr.com/7035/668s3993035_fb18h0e9648.jpg",
            large_url: "http://farm8.staticflickr.com/7035/66839v93035_fb180e9648_b.jpg",
            original_url: null
            },
        rating: "4",
        uploaded_at: "2012-01-12T04:25:25-08:00"
        }
    ],
    mongoid_user_id: "4f0ecf3e57e65260001000001",
    name: "Michael Mark"
},
{
    _id: "4f08aa477eddd05000100000c",
    had_drinks: [
    {
        _id: "4f0ff0513942d4e000100000b",
        at: "2012-01-07T030:00:00-08:00",
        beer_id: "4f0ff0561394d74e0001000009",
        cost: 4.1,
        location: "Bootleggers",
        photo: null,
        rating: "3",
        uploaded_at: "2012-01-13T030:50:25-08:00"
    }
    ],
    mongoid_user_id: "4f08aa477e34ddd0000100000b",
    name: "Daniel M"
},

How can I achieve getting the photo out?
From what I have learnt in my short time with mongoid and document database I need to start the query from the root and work down?

I was trying something like this

Participant.where(:had_drinks.where(:photo.photo_id => params[:id]))

But think i am miles off so far

----EDIT :)
Thinking it through I guess I need to do the following query

1: Get any Participant With this Photo

2: Select the First Participant

3: Filter their photos to photos with this id

4: Select the first photo (will only actually ever be one)

Just unsure how to do that query in my limited mongoid/mongodb knowledge

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

瑶笙 2025-01-08 08:14:20

如果您有照片 ID,则可以执行以下操作:

db.participants.find({'had_drinks.photo.photo_id': '66832993035'}, 
                     {'had_drinks.photo': 1})

这将从存在此类照片的 had_drinks 数组中提取所有照片。您必须在客户端对其进行过滤。 AFAIK,目前无法检索单个数组元素(当然,map-reduce 除外)。

在红宝石中,这将是这样的:

participant = Participant.where('had_drinks.photo.photo_id': '66832993035').first
photo = participant.had_drinks.map{|hd| hd['photo']}. # get only photos
                    compact.  # remove nils
                    select{|p| p['photo_id'] == '66832993035'}. # get only relevant photos
                    first  # and pick first
                    # done

If you have photo id, you can do something like this:

db.participants.find({'had_drinks.photo.photo_id': '66832993035'}, 
                     {'had_drinks.photo': 1})

That'll pull all photos from had_drinks array where such photo exists. You have to filter it client-side. AFAIK, there currently is no way to retrieve a single array element (except for map-reduce, of course).

In ruby this would be something like this:

participant = Participant.where('had_drinks.photo.photo_id': '66832993035').first
photo = participant.had_drinks.map{|hd| hd['photo']}. # get only photos
                    compact.  # remove nils
                    select{|p| p['photo_id'] == '66832993035'}. # get only relevant photos
                    first  # and pick first
                    # done
空城之時有危險 2025-01-08 08:14:20

我将使用 $elemMatch 来获取实际参与者,并且然后根据您的编辑从中取出照片。类似的东西:

Participant.where(:had_drinks => {"$elemMatch" => {:photo => {:id => params[:id]}}})

可能有一种方法可以限制仅返回照片(或者,像其他答案一样,限制所有饮料),但我想说,如果您确实需要大量随机访问照片,那么它们应该属于他们自己的收藏。

I would use $elemMatch to get the actual participant, and then pull the photo out of that, per your edit. Something like:

Participant.where(:had_drinks => {"$elemMatch" => {:photo => {:id => params[:id]}}})

There may be a way to limit the return to just the photo (or, like in the other answers, all of the drinks), but I'd say if you really need a lot of random access to photos, they should be in their own collection.

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