Please refer the attached image and help me explain this case.
Why didn’t cova_items column assigned value of InStock column when woo_order_id is null?
query command:
db.cova_order.aggregate([
{
$project:{
InStock:1,
woo_order_id:1,
woo_order_data:1,
cova_items:{
$cond: { if: { $eq:["$woo_order_id",null] }, then:"$InStock" , else:"$woo_order_data" }
}
}
},
=> Result: cova_items column shows value like as woo_order_data column
Please show me the mistake if having. Thanks
2
Answers
$cond
has simplified syntax.Fast solution:
Seems your
$woo_order_id
is notnull
, but empty.Complete solutuion
Whether
$woo_order_id
can benull
,undefined
ormissing
, with the $ifNull operator we cover them all.MongoPlayground
Given Data:
Task : If woo_order_id is null or "" (empty string) then return Instock and if it is not null/empty string then return woo_order_data
Solution:
Output :