zoukankan      html  css  js  c++  java
  • mongo-go-driver mongo filter

    and or 判断数组为空

    rd:=bson.M{"num":"1","$or":[]bson.M{{"$where":"this.logo.length>0"},{"$where":"this.ziliao.length>0"}}}

     

    mongo-go-driver: nested OR/AND query filter

    I try to create a MongoDB query filter with the nested operators (OR/AND/...). But lib requires to create a bson.D and pass bson.E elements into it. If I need to have OR/AND inside AND/OR - I need to put bson.M + bson.D inside bson.D like this:

    filter := bson.M{"$and": bson.D{{"p", 10}, bson.M{"$or": bson.D{{"s", 30}, {"a", 1}}}}}
    

    .. and of course it doesn't work: cannot use primitive.M literal (type primitive.M) as type primitive.E in slice literal. Probably the same problem will happen if later I try to use a ... in [] logics inside a bson.D

    How do I create such nested queries in Go and official MongoDB driver?

     

    What matters is that $or requires an array, which is bson.A , Also $and is the default, you don't have to indicate that.

    Your filter can be defined like this:

    filter := bson.D{
        {"p", 10},
        {"$or", bson.A{
            bson.D{{"s", 30}},
            bson.D{{"a", 10}},
        }},
    }
    

    You may also use this:

    filter = bson.D{
        {"p", 10},
        {"$or", bson.A{
            bson.M{"s": 30},
            bson.M{"a": 10},
        }},
    }
    

    Or this:

    filter := bson.M{
        "p": 10,
        "$or": bson.A{
            bson.M{"s": 30},
            bson.M{"a": 10},
        },
    }
  • 相关阅读:
    CF1260F
    牛客挑战赛34 A~E
    CSP-S2019游记&拆塔记
    6424. 【NOIP2019模拟2019.11.13】我的订书机之恋
    CF1257E/F
    6423. 【NOIP2019模拟11.11】画
    1222/2516. Kup
    Comet OJ
    浅析CSS定位
    css文字颜色渐变的3种实现
  • 原文地址:https://www.cnblogs.com/eiguleo/p/14070146.html
Copyright © 2011-2022 走看看