任何人都可以帮助我提取包含“ Australia”作为国家/地区的记录,而我们不知道Mongo Query中日期图中的键(日期)。
我的样本数据....
`{
"_id" : ObjectId("5bfbdf1f123ee9f1b2bb82e0"),
"fruit" : "apple",
"datemap" : {
"11/26/2018" : [
{
"_id" : "bbc2bb53-b7fe-43ec-9e88-38efbd69c345",
"country" : "Denmark"
}
],
"11/27/2018" : [
{
"_id" : "bbc2bb53-b7fe-43ec-9e88-38efbd68c345",
"country" : "Norway"
}
],
"11/28/2018" : [
{
"_id" : "bbc2bb54-b7fe-43ec-9e88-38efbd68c345",
"country" : "Australia"
}
]
}
},
{
"_id" : ObjectId("5bfbdf1f123ee9f1b2bb8245"),
"fruit" : "Orange",
"datemap" : {
"11/26/2018" : [
{
"_id" : "bbc2bb53-b7fe-43ec-9e88-38efbd69c345",
"country" : "Brazil"
}
],
"11/29/2018" : [
{
"_id" : "bbc2bb53-b7fe-43ec-9e88-38efbd68c345",
"country" : "England"
}
],
"11/30/2018" : [
{
"_id" : "bbc2bb54-b7fe-43ec-9e88-38efbd68c345",
"country" : "Australia"
}
]
}
}`
提前谢谢。...
答案 0 :(得分:3)
您可以使用$objectToArray
将未知密钥转换成k
v
对,然后设置$match
条件。
类似
db.collection.aggregate([
{ "$addFields": { "country": { "$objectToArray": "$datemap" } } },
{ "$match": { "country.v.country": "Australia" } },
{ "$project": { "country": 0 }}
])