mongodb,通过散列键查找
mongodb, lookup by hash key
我有一个 collection 这样的:
- 类别:
{_id: Object:Id(...), code: 'drink', name: 'Soft Drink and Beer'}
{_id: Object:Id(...), code: 'fast-food', name: 'Burger and Chicken Fry'}
- 群组成员:
{_id: Object:Id(G1), categories: {'drink' => 5, 'fast-food' => 3}}
{_id: Object:Id(G2), categories: {'drink' => 2}}
我真正想要的许愿输出:
{_id: Object:Id(G1), categories: {'Soft Drink and Beer' => 5, 'Burger and Chicken Fry' => 3}}
{_id: Object:Id(G2), categories: {'Soft Drink and Beer' => 2}}
我尝试了很多方法,但没有运气。您有这方面的经验吗?
您可以将以下聚合与 MongoDB 3.6 及以上
一起使用
db.GroupPeople.aggregate([
{ "$addFields": { "categories": { "$objectToArray": "$categories" }}},
{ "$unwind": "$categories" },
{ "$lookup": {
"from": "Category",
"let": { "category": "$categories.k" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$$category", "$code"] }}}
],
"as": "category"
}},
{ "$unwind": "$category" },
{ "$group": {
"_id": "$_id",
"categories": {
"$push": {
"k": "$category.name",
"v": "$categories.v"
}
}
}},
{ "$project": {
"categories": {
"$arrayToObject": "$categories"
}
}}
])
以及3.4.4及以上
db.GroupPeople.aggregate([
{ "$addFields": { "categories": { "$objectToArray": "$categories" }}},
{ "$unwind": "$categories" },
{ "$lookup": {
"from": "Category",
"localField": "categories.k",
"foreignField": "code",
"as": "category"
}},
{ "$unwind": "$category" },
{ "$group": {
"_id": "$_id",
"categories": {
"$push": {
"k": "$category.name",
"v": "$categories.v"
}
}
}},
{ "$project": {
"categories": {
"$arrayToObject": "$categories"
}
}}
])
我有一个 collection 这样的:
- 类别:
{_id: Object:Id(...), code: 'drink', name: 'Soft Drink and Beer'}
{_id: Object:Id(...), code: 'fast-food', name: 'Burger and Chicken Fry'}
- 群组成员:
{_id: Object:Id(G1), categories: {'drink' => 5, 'fast-food' => 3}}
{_id: Object:Id(G2), categories: {'drink' => 2}}
我真正想要的许愿输出:
{_id: Object:Id(G1), categories: {'Soft Drink and Beer' => 5, 'Burger and Chicken Fry' => 3}}
{_id: Object:Id(G2), categories: {'Soft Drink and Beer' => 2}}
我尝试了很多方法,但没有运气。您有这方面的经验吗?
您可以将以下聚合与 MongoDB 3.6 及以上
一起使用db.GroupPeople.aggregate([
{ "$addFields": { "categories": { "$objectToArray": "$categories" }}},
{ "$unwind": "$categories" },
{ "$lookup": {
"from": "Category",
"let": { "category": "$categories.k" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$$category", "$code"] }}}
],
"as": "category"
}},
{ "$unwind": "$category" },
{ "$group": {
"_id": "$_id",
"categories": {
"$push": {
"k": "$category.name",
"v": "$categories.v"
}
}
}},
{ "$project": {
"categories": {
"$arrayToObject": "$categories"
}
}}
])
以及3.4.4及以上
db.GroupPeople.aggregate([
{ "$addFields": { "categories": { "$objectToArray": "$categories" }}},
{ "$unwind": "$categories" },
{ "$lookup": {
"from": "Category",
"localField": "categories.k",
"foreignField": "code",
"as": "category"
}},
{ "$unwind": "$category" },
{ "$group": {
"_id": "$_id",
"categories": {
"$push": {
"k": "$category.name",
"v": "$categories.v"
}
}
}},
{ "$project": {
"categories": {
"$arrayToObject": "$categories"
}
}}
])