deltaaruna
deltaaruna

Reputation: 538

Mongodb $cond with find

This is an example order document I have

{numberOfHours: 10, status: “BOOKED”, hidden: true},
{numberOfHours: 5, status: “NOT_BOOKED”,hidden: true},
{numberOfHours: 1, status: “BOOKED”, hidden: true},
{numberOfHours: 10, status: “PENDING”, hidden: true}

This is my requirement. I want to find all which are not hidden(hidden = false). But if the status is “BOOKED” it should show even if hidden is true. I will have to use a collection.find() as well

This is my query -

{  $expr: {
       $eq:[ {
          $cond: {
             if: { "$status" : { $ne: "BOOKED"} },
             then: {"$hidden"},
             else: {null}
           }
       },
       false ] }
}

How to proceed from here? Any help will be highly appreciated.

Upvotes: 3

Views: 1853

Answers (2)

Parth Raval
Parth Raval

Reputation: 4453

{$expr: {
    {$cond: {
        "if": {"$eq": ["$status", "BOOKED"]},
        "then": {"$hidden"},
        "else": {null}
      }
    },false}
}

Note:- I have given an answer based on your question. Hope this will help you.

Upvotes: 2

mickl
mickl

Reputation: 49985

You can just use $or to express both conditions:

{  $expr: {
       $or: [ { $eq: [ "$status", "BOOKED" ] }, { $eq: [ "$hidden", false ] } ]
}

(if status is different than BOOKED then it checks whether hidden set to false)

Upvotes: 4

Related Questions