我在mongodb中存储了以下文档
{
"_id" : ObjectId("5c13b65ef169123748e18ecb"),
"Countries" : [
{
"States" : [
{
"Cities" : [
"Eshkashem",
"Fayzabad",
"Jurm",
"Khandud",
"Qal'eh-ye Panjeh"
],
"StateName" : "Badakhshan"
},
{
"Cities" : [
"Bala Morghab",
"Qal'eh-ye Naw"
],
"StateName" : "Badgis"
}
],
"CountryName" : "Afghanistan"
},
{
"States" : [
{
"Cities" : [
"Berat",
"Polican",
"Ure Vajgurore"
],
"StateName" : "Berat"
},
{
"Cities" : [
"Bulqize"
],
"StateName" : "Bulqize"
}
],
"CountryName" : "Albania"
}
]
}
我想要的是,我想获取countryName为Afganisthan的数据
我尝试了问题查询
loc_country = loc.find({"Countries.CountryName":"Afghanistan"})
但是它提供了所有记录,而不仅仅是阿富汗人
请帮助我,我被困住了。
预先感谢!
答案 0 :(得分:1)
您可以使用positional operator:
loc_country = loc.find(
{ "Countries.CountryName":"Afghanistan"},
{ 'Countries.$': 1 }
)
如果可能的话,您应该考虑一个数据模型,每个文档一个国家/地区,以便于查找。 (如果有道理)