1

Hello I've been a little stumped on this for a few days. Due to mongo being pretty outdated where I work I can't use a switch statement since it's not supported in our version. I am trying to accomplish my task with nested if/else. The first condition gets evaluated and the second myField2 is getting set but none of the inner if/else conditions work. I can't tell what I'm doing wrong with this one.

db.getCollection("MyCollection").updateMany({
    
    
  //SOME CONDITIONS CHECKED HERE
},
[{
    "$set":{
        "myTs":{
            "$cond":{
                "if":{
                    "$and": [
                       {"myField1" : "value1"},
                       {"myField2": "value2"},
                    ]
                },
                "then":"$ThisTs",
                "else": {
                    "$cond":{
                        "if":{
                                "myField2": "value3"
                        },
                        "then":"$lastUpdatedTs",
                        "else":{
                               "$cond":{
                                    "if":{
                                        "$and": [
                                              {"myField1" : "value4"},
                                              {"$ne": ["$myField3.aTs", null]},
                                              {"$ne": ["$myField3.aTs", "0"]},
                                              {"$eq": ["$myField3.aBool", false]},
                                        ]
                                     },
                                    "then":"$myField3.aTs2",
                        
                                    "else":{
                                        "$cond":{
                                            "if":{
                                                "$and": [
                                                    {"myField1" : "value2"},
                                                    {"myField2" : "value1"},
                                                    {"$or": [
                                                        {"$eq": ["$myField3.aTs", null]},
                                                        {"$eq": ["$myField3.aTs", "0"]},
                                                        {"$eq": ["$myField3.aBool", false]},
                                                            ]
                                                    },
                                                ]
                                            },
                                            "then":"$myField3.aTs",
                        
                                            "else": "$lastTs",
                                        }
                                    }
                    
                                }
                            }
                        }
                    }
                }
            },
        "myField2":{
             "$cond":{
                        "if":{
                             "$and": [
                                     {"myField1" : "value2"},
                                     {"$ne": ["$myField3.aTs", null]},
                                     {"$ne": ["$myField3.aTs", "0"]},
                                     {"$eq": ["$myField3.aBool", false]},
                                    ]
                            },
                        "then":"FINISHED",
                        "else": "$myField2"
        
                }
    
            }
    }
}], {multi: true}
)

I'm getting a little turned around with this one. Any pointers in the right direction? I haven't been able to find too much information on nested if/else.

Daniella
  • 171
  • 2
  • 3
  • 14
  • you have double $$ iinstead of $ `"then":"$$myField3.aTs",`. also you might need to convert all that look like `{"myField1" : "value2"},` to `{"$eq": ["$myField1", "value2"]}`. i adjusted the query a bit https://mongoplayground.net/p/9NYsuY_Nwgd maybe you can have a look – cmgchess May 12 '23 at 16:28
  • The extra "$" was a typo. But I am going to explore wrapping all the statements into ```"$eq"``` Thank you! – Daniella May 12 '23 at 16:45
  • other than that i dont see anything else. i cant say anything about the logic without seeing data – cmgchess May 12 '23 at 17:54
  • Wrapping all the statements into "$eq" only broke the first if check so that didn't work. I don't believe it's a logic issue because I changed the order of the conditions around and still only the inner if/else doesn't work. – Daniella May 12 '23 at 19:24
  • Hmm I tested with https://mongoplayground.net/p/9NYsuY_Nwgd . From what I know you need $eq to check field equals value inside in aggregation. Maybe you can check with one of your documents where you intend the nested if s to affect – cmgchess May 13 '23 at 00:14
  • According documentation `$switch` exists since version 3.4, i.e. you may run version 3.2 which is EoL for 5 years. You should consider an upgrade. – Wernfried Domscheit May 13 '23 at 09:20
  • Thank you. I am working to reach the right people to get it updated in our org. – Daniella May 17 '23 at 19:56

1 Answers1

0

After messing with this further the last few days this is what I found to have resolved my specific issue: using $nin instead of $ne and $in instead of $eq.

My specific data was located inside of an object in the database so replacing {"$ne": ["$myField3.aTs", null]}, with "myField3.aTs" : {"$nin":[null,""]} worked in my case. I found this by separating each condition into it's own script and isolating to find what was not working.

Here are a few helpful links that I used to find this information:

How do you query for "is not null" in Mongo?

https://www.mongodb.com/docs/manual/reference/bson-type-comparison-order/#non-existent-fields

https://www.tutorialspoint.com/mongodb-query-which-represents-not-equal-to-null-or-empty

Also shout out to @cmgchess for the suggestions this got me using the mongodb playground and that ultimately helped me find what I needed.

Daniella
  • 171
  • 2
  • 3
  • 14