I have the following mongodb collection
{ "id" : "yzes547",
"courseId" : "aw4a34y7",
"objectives" : [
{
"type" : "LESSON",
"id" : "dfhxytd"
},
{
"type" : "MODULE",
"id" : "rgzeerp"
}
]
}
I want to add new fields for ids of each type in objectives parameter.
Desired output
{ "id" : "yzes547",
"courseId" : "aw4a34y7",
"objectives" : [
{
"type" : "LESSON",
"id" : "dfhxytd"
},
{
"type" : "MODULE",
"id" : "rgzeerp"
}
]
"lessonId": "dfhxytd",
"moduleId": "rgzeerp"
}
I have tried the following query but it only returns False.
db.account.aggregate([
{
'$project': {
'_id': 0,
'questionId': 1,
'courseId': 1,
'lessonId': {
'$cond': [
{
'$eq': [
'$objectives.type', 'LESSON'
]
}, '$objectives.id', False
]
},
'moduleId': {
'$cond': [
{
'$eq': [
'$objectives.type', 'MODULE'
]
}, '$objectives.id', False
]
}
Is it possible to query the database such that we can get new fields of each type in one aggregation method?