質問

I have a collection with documents that look something like this:

{
    _id: ObjectId("521d11014903728f8d000006"),
    association_chain: [
        {
            name: "Foobar",
            id: ObjectId("521d11014903728f8d000005")
        }
    ],
    // etc...
}

I can search by the name attribute with this query:

@results = Model.where 'association_chain.name' => 'Foobar'

This returns the results as expected. However, when I try to search using the id attribute:

@results = Model.where 'association_chain.id' => '521d11014903728f8d000005'

There are no results. As far as I can tell, the query that Mongoid generates looks correct:

MOPED: 127.0.0.1:27017 QUERY  database=x collection=x selector={"$query"=>{"association_chain.id"=>"521d11014903728f8d000005"}, "$orderby"=>{"created_at"=>-1}} flags=[] limit=25 skip=0 batch_size=nil fields=nil (244.7259ms)

What am I doing wrong?

役に立ちましたか?

解決

You are searching for the string. Try searching for an ObjectId, like

@results = Model.where 'association_chain.id' => BSON::ObjectId('521d11014903728f8d000005')
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top