Seleccionar un elemento hijo con mongoide

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

preguntado el 01 de febrero de 12 a las 22:02

2 Respuestas

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 todos 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

Respondido 02 Feb 12, 03:02

yo usaría $ 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.

Respondido 02 Feb 12, 03:02

No necesitas $elemMatch when there's only one condition to meet. - Sergio Tulentsev

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.