如何在 Mongo 控制台和 Mongoid 中执行此查询?
我正在尝试学习如何以更高级的方式查询 Mongo。假设我的数据结构是这样的:
{ "_id" : "-bcktick-ajman-ae-292932", "asciiname" : "`Ajman",
"alternatenames" : [
{
"isolanguage" : "no",
"alternateNameId" : 2698358,
"alternateName" : "Ajman"
},
{
"isolanguage" : "en",
"alternateNameId" : 2698357,
"alternateName" : "Ajman"
}
]
}
因此找到 Ajman
很容易:
db.cities.find({ "asciiname":"`Ajman" })
但是,我想找到 isolanguage
仅为 <强>en。您会注意到 isolanguage
位于 alternatenames 数组中。
但我似乎无法在客户端或 mongoid 中找到正确的语法,
其中之一(或两者)将不胜感激。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在寻找
$elemMatch
关键字:目前,Mongoid 没有
$elemMatch
的帮助程序,因此您必须传递原始查询:更多信息请参见此处
$elemMatch
此处:有关 Mongoid 对
$elemMatch
支持的更多信息此处:I think you are looking for the
$elemMatch
keyword:Currently, Mongoid does not have a helper for
$elemMatch
so you have to pass in the raw query:More info here on
$elemMatch
here:More info on Mongoid support for
$elemMatch
here: