Database
[{
"my_id": {
"Product": "Pork Soup",
"Protein": 100.0
}
}, {
"my_id": {
"Product": "Duck Sandwich",
"Protein": 1000.1
}
}, {
"my_id": {
"Product": "Chicken Roll",
"Protein": 100.69
}
}, {
"my_id": {
"Product": "Disgusting Tofu",
"Protein": 0.1
}
}, {
"my_id": {
"Product": "Cardboard Casserole",
"Protein": 50.0
}
}]
Query
db.collection.aggregate([{
"$project": {
"w": {
"$split": ["$my_id.Product", " "]
},
"Product": "$my_id.Product",
"Protein": "$my_id.Protein"
}
},
{
"$project": {
"_id": 0,
"Product": 1,
"Protein": 1,
"Category": {
"$switch": {
"branches": [{
"case": {
"$in": ["Pork", "$w"]
},
"then": "Pork"
},
{
"case": {
"$in": ["Chicken", "$w"]
},
"then": "Chicken"
},
{
"case": {
"$in": ["Tofu", "$w"]
},
"then": "Tofu"
}
],
"default": "NA"
}
}
}
}
])