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.

7
  • 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 mongoplayground.net/p/9NYsuY_Nwgd maybe you can have a look Commented May 12, 2023 at 16:28
  • The extra "$" was a typo. But I am going to explore wrapping all the statements into "$eq" Thank you! Commented May 12, 2023 at 16:45
  • other than that i dont see anything else. i cant say anything about the logic without seeing data Commented May 12, 2023 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. Commented May 12, 2023 at 19:24
  • Hmm I tested with 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 Commented May 13, 2023 at 0:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.