I'm trying to create a computed status field in my Mongo query (statuses: created, payment received, shipped, received, finished).
db.orders.aggregate( [
{ $project: { status: {
$cond: { if: { $ne: ["$feedback", null] },
then: 'finished', else: {
$cond: { if: { $ne: ["$received", null] },
then: 'received', else: {
$cond: { if: { $ne: ["$shipped", null] },
then: 'shipped', else: {
$cond: { if: { $ne: ["$payment", null] },
then: 'payment received', else: 'created' }
} }
} }
} }
} } },
{ $match: { } }
] )
Example data:
{
"_id" : "xxxxxx0",
"payment" : ISODate("2016-02-03T10:45:00.011Z"),
"shipped" : ISODate("2016-02-03T11:55:00.011Z"),
"received" : ISODate("2016-02-03T12:45:00.011Z"),
"feedback" : ISODate("2016-02-03T14:34:00.011Z")
},
{
"_id" : "xxxxxx1",
"payment" : ISODate("2016-02-03T10:45:00.011Z"),
"shipped" : ISODate("2016-02-03T11:55:00.011Z"),
"received" : ISODate("2016-02-03T12:45:00.011Z")
},
{
"_id" : "xxxxxx2",
"payment" : ISODate("2016-02-03T10:45:00.011Z"),
"shipped" : ISODate("2016-02-03T11:55:00.011Z")
},
{
"_id" : "xxxxxx3",
"payment" : ISODate("2016-02-03T10:45:00.011Z")
},
{
"_id" : "xxxxxx4"
}
For some reason all my results show up as 'finished', am I using $cond wrong? Does it support nested $cond ?
You can't use $eq null if you want to check if the field exists or not, it will always return true There is a trick to do that with $gt. You can check full explanation here (https://docs.mongodb.com/manual/reference/bson-types/#bson-types-comparison-order)
db.orders.aggregate( [
{ $project: { status: {
$cond: { if: { $gt: ["$feedback", null] },
then: 'finished', else: {
$cond: { if: { $gt: ["$received", null] },
then: 'received', else: {
$cond: { if: { $gt: ["$shipped", null] },
then: 'shipped', else: {
$cond: { if: { $gt: ["$payment", null] },
then: 'payment received', else: 'created' }
} }
} }
} }
} } },
{ $match: { } }
] )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With