I’m trying to learn how to query Mongo in more advanced ways. Let’s say my data structure is this:
{ "_id" : "-bcktick-ajman-ae-292932", "asciiname" : "`Ajman",
"alternatenames" : [
{
"isolanguage" : "no",
"alternateNameId" : 2698358,
"alternateName" : "Ajman"
},
{
"isolanguage" : "en",
"alternateNameId" : 2698357,
"alternateName" : "Ajman"
}
]
}
So to find Ajman is easy:
db.cities.find({ "asciiname":"`Ajman" })
However, I want to find cities that only have an isolanguage of en. You’ll notice the isolanguage is in the alternatenames array.
But I can’t seem to find the correct syntax in either the client or mongoid
Either one (or both) would be greatly appreciated.
Thanks
I think you are looking for the
$elemMatchkeyword:Currently, Mongoid does not have a helper for
$elemMatchso you have to pass in the raw query:More info here on
$elemMatchhere:More info on Mongoid support for
$elemMatchhere: