您好,这几天我一直被这个问题难住了.由于我工作的地方的mongo非常过时,我不能使用Switch语句,因为我们的版本不支持它.我正在try 使用嵌套的if/Else完成我的任务.判断了第一个条件,设置了第二个myField2,但内部的if/Else条件都不起作用.我不知道这件事我做错了什么.

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}
)

我对这件事有点反感.有什么正确方向的指示吗?我在嵌套的if/Else上找不到太多信息.

推荐答案

经过最近几天的进一步处理,我发现这解决了我的特定问题:使用$nin而不是$ne,使用$in而不是$eq.

我的特定数据位于数据库中的一个对象中,所以在我的例子中,用"myField3.aTs" : {"$nin":[null,""]}替换{"$ne": ["$myField3.aTs", null]},是可行的.我通过将每个条件分离到它自己的脚本中并隔离以找出哪些条件不起作用,从而找到了这一点.

以下是我用来查找这些信息的一些有用的链接:

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

我也向@cmgchess喊话,寻求那些让我使用MongoDBplayground 的建议,最终帮助我找到了我需要的东西.

Javascript相关问答推荐

调用SEARCH函数后,程序不会结束

如何提取Cypress中文本

如何使用JavaScript用等效的功能性HTML替换标记URL格式?

将数据从strapi提取到next.js,但响应延迟API URL

如何修复内容安全策略指令脚本-SRC自身错误?

使用JavaScript重新排序行

使用javascript将Plotly Expandable Sparkline转换为HighCharter Plot在bslib卡中

如何为我的astro页面中的相同组件自动创建不同的内容?

html + java script!需要帮助来了解为什么我得到(无效的用户名或密码)

如何从URL获取令牌?

Websocket错误—有一个或多个保留位开启:reserved1 = 1,reserved2 = 0,reserved3 = 0

在forEach循环中获取目标而不是父对象的属性

覆盖加载器页面避免对页面上的元素进行操作

Rxjs流中生成IMMER不能在对象上操作

我想使用GAS和HTML将从Electron 表格中获得的信息插入到文本字段的初始值中

使用API调用的VUE 3键盘输入同步问题

调用特定数组索引时,为什么类型脚本不判断未定义

设置复选框根据选中状态输入选中值

JSX/React -如何在组件props 中循环遍历数组

如果查询为空,则MongoDB将所有文档与$in匹配