Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check for null condition in mongodb aggregation

bookSchema collection:

[
    {
        _id: ObjectId("637d05dc32428ed75ea08d09"),
        book_details: {
            book_subscription: null,
            book_auth: 'Amber'
        },
        book_name: 'random123'
    },
    {
        _id: ObjectId("637d0673ce0f17f6c473dee2"),
        book_details: {
            book_subscription: ObjectId('637d06a545a17f0e686ecf3a'),
            book_auth: 'Shurya'
        },
        book_name: 'random321'
    },
    {
        _id: ObjectId("637d069a3d597c8458ebe4ec"),
        book_details: {
            book_subscription: ObjectId('637d06ac82f0760b03fdea0d'),
            book_auth: 'Aman'
        },
        book_name: 'random676'
    },
    {
        _id: ObjectId("637d06c05b32d503007bcb54"),
        book_details: {
            book_subscription: null,
            book_auth: 'Saurav'
        },
        book_name: 'random999'
    }
]

I want the book_count to be 1 if book_subscription is not null and 0 if book_subscription is null.

For this I tried as:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

db.bookSchema.aggregate([
  {
    "$addFields": {
      "book_count": {
        "$cond": {
          "if": {
            "$ne": [
              "book_details.book_subscription",
              null
            ]
          },
          "then": 1,
          "else": 0
        }
      }
    }
  }
])

But the book_count I get is 1 eventhough book_subscription is null.

How to check whether the value of book_subscription is null and get the count as 1 only for book_subscription with value and 0 for book_subscription, null?

Mongodb Playground: https://mongoplayground.net/p/Ff4Q43-nSQe

>Solution :

One option is:

db.bookSchema.aggregate([
  {
    "$addFields": {
      "book_count": {
        "$cond": {
          "if": {
            "$ne": [
              "$book_details.book_subscription",
              null
            ]
          },
          "then": 1,
          "else": 0
        }
      }
    }
  }
])

See how it works on the playground example

Another one is:

db.bookSchema.aggregate([
  {
    "$addFields": {
      "book_count": {
        "$toInt": {
          "$toBool": {
            "$ifNull": [
              "$book_details.book_subscription",
              0
            ]
          }
        }
      }
    }
  }
])

See how it works on the playground example- bool

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading