skip to Main Content

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" }
            }
        }
    },

enter image description here

=> Result: cova_items column shows value like as woo_order_data column

Please show me the mistake if having. Thanks

2

Answers


  1. $cond has simplified syntax.

    { $cond: [ <boolean-expression>, <true-case>, <false-case> ] }
    

    Fast solution:

    Seems your $woo_order_id is not null, but empty.

    {$cond:[ {$eq:["$woo_order_id", ""] }, "$InStock", "$woo_order_data"]}
    

    Complete solutuion

    Whether $woo_order_id can be null, undefined or missing, with the $ifNull operator we cover them all.

    db.cova_order.aggregate([
      {
        $project: {
          InStock: 1,
          woo_order_id: 1,
          woo_order_data: 1,
          cova_items: {
            $cond: [
              { $eq: [ { "$ifNull": [ "$woo_order_id", ""] }, "" ] },
              "$InStock",
              "$woo_order_data"
            ]
          }
        }
      }
    ])
    

    MongoPlayground

    Login or Signup to reply.
  2. 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

    [
      {
        "InStock": 1,
        "woo_order_id": null,
        "woo_order_data": "1d",
        
      },
      {
        "InStock": 2,
        "woo_order_id": 2,
        "woo_order_data": "2d",
        
      },
      {
        "InStock": 3,
        "woo_order_id": 3,
        "woo_order_data": "3d",
        
      },
      {
        "InStock": 4,
        "woo_order_id": "",
        "woo_order_data": "4d",
        
      }
    ]
    

    Solution:

    db.collection.aggregate([
      {
        $project: {
          InStock: 1,
          woo_order_id: 1,
          woo_order_data: 1,
          cova_items: {
            $cond: {
              if: {
                "$or": [
                  {
                    "$eq": [
                      "$woo_order_id",
                      ""
                    ]
                  },
                  {
                    "$eq": [
                      "$woo_order_id",
                      null
                    ]
                  }
                ]
              },
              then: "$InStock",
              else: "$woo_order_data"
            }
          }
        }
      }
    ])
    

    Output :

    [
      {
        "InStock": 1,
        "_id": ObjectId("5a934e000102030405000000"),
        "cova_items": 1,
        "woo_order_data": "1d",
        "woo_order_id": null
      },
      {
        "InStock": 2,
        "_id": ObjectId("5a934e000102030405000001"),
        "cova_items": "2d",
        "woo_order_data": "2d",
        "woo_order_id": 2
      },
      {
        "InStock": 3,
        "_id": ObjectId("5a934e000102030405000002"),
        "cova_items": "3d",
        "woo_order_data": "3d",
        "woo_order_id": 3
      },
      {
        "InStock": 4,
        "_id": ObjectId("5a934e000102030405000003"),
        "cova_items": 4,
        "woo_order_data": "4d",
        "woo_order_id": ""
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search