Rahul Anand
Rahul Anand

Reputation: 573

Mongodb query match with if condition form multiple fields with null values

I have a json where i am trying to filter my pipeline with multiple fields where some fields can have null value as well. sender.client and receiver.client may not always values

Below is the part of sample json:

{"sender" : {
        "id" : "5d95", 
        "name" : "Name1", 
        "phone" : "123456",
        "client" : "spec1"
    }, 
    "receiver" : {
        "id" : "5d95683", 
        "name" : "name2", 
        "phone" : "342235", 
        "client" : "spec1"
    }
}

{"sender" : {
        "id" : "52fes", 
        "name" : "Name2", 
        "phone" : "3334321",
        "client" : "spec2"
    }, 
    "receiver" : {
        "id" : "5efse", 
        "name" : "name3", 
        "phone" : "7353344", 
        "client" : "spec1"
    }
}

I am aiming to filter with condition if (sender.client = spec1 or receiver.client =spec1) then display all fields, i.e if the name of the client matches then i have to display other required fields. I have been trying with $project and $match but match is not working with $cond , so i have taken alternate route with $eq but it's not helping me filter out my requirement. Below is my code:

    { 
        "$project" : {
            "sendername" : {
                "$cond" : {
                    "if" : {
                        "$eq" : [
                            "$sender.client", 
                            "spec1"
                        ]
                    }, 
                    "then" : "$sender.name", 
                    "else" : 0.0
                }
            }, 
            "sendername1" : {
                "$cond" : {
                    "if" : {
                        "$eq" : [
                            "$receiver.client", 
                            "spec1"
                        ]
                    }, 
                    "then" : "$receiver.name", 
                    "else" : 0.0
                }
            }
        }
    }

I want to use $match inpace of $eq. How do i accomplish this?

Upvotes: 0

Views: 1212

Answers (1)

prasad_
prasad_

Reputation: 14287

The $match with $exprcan filter based on the condition: (sender.client = spec1 or receiver.client = spec1)

I am aiming to filter with condition if (sender.client = spec1 or receiver.client = spec1) then display all fields, i.e if the name of the client matches then i have to display other required fields.

You can add further filter to match the name field (there are two fields with "name", sender.name and receiver.name; which one to match upon is not clear. I am assuming it can be any one).

var queryFilter = { $cond: {
                      if: { $or: [ 
                               { $eq: [ "$sender.client", "spec1" ] },
                               { $eq: [ "$receiver.client", "spec1" ] }
                            ]
                      },
                      then: true,
                      else: false
                 }
};

db.test.aggregate( [
{
    $match: { 
              $expr: { $eq: [ queryFilter,  true ],
              $or: [ { "sender.name" : "name3" }, { "receiver.name" : "name3" } ]  } 
    }
}
] )

The match filter is: $expr: {...} and $or: [...].

Further, you can add other stages (e.g., $project) after the $match stage, as needed.

Upvotes: 1

Related Questions