简体   繁体   English

使用 $and 条件创建过滤器,SQL 到 golang 过滤器转换器?

[英]Create filter with $and condition, SQL to golang filter converter?

I have currently a working filter for.DeleteMany.我目前有一个适用于.DeleteMany 的过滤器。 It deletes all entries where _id is in the given array vids :它删除_id在给定数组vids中的所有条目:

filter := bson.D{{Key: "_id", Value: bson.D{{Key: "$in", Value: vids}}}}
res, err := DB.Collection("data").DeleteMany(context.TODO(), filter)

Now I want to enhance the filter and add some $and condition to only delete entries where _id is in the given array vids and(!) the value of providerid is 1234 .现在我想增强过滤器并添加一些$and条件以仅删除_id在给定数组vids中的条目并且(!) providerid的值为1234

Sadly I'm stuck on how to do that in go.可悲的是,我一直坚持如何在 go 中做到这一点。 For me it is extremely hard to read and write such filters.对我来说,阅读和编写这样的过滤器非常困难。 Especially with all that bson.D, bson.M and []bson.D and the many curly brackets etc.尤其是所有 bson.D、bson.M 和 []bson.D 以及许多大括号等。

In SQL I would write DELETE FROM data WHERE _id IN( {list} ) AND providerid=1234;在 SQL 我会写DELETE FROM data WHERE _id IN( {list} ) AND providerrid=1234;

Is there any SQL to golang mongodb filter converter?有没有SQL转golang mongodb滤镜转换器?

Try尝试

filter := bson.D{
    { "$and", []interface{}{
        bson.D{{ Key: "_id", Value: bson.D{{ Key: "$in", Value: vids }}}},
        bson.D{{ "providerid", 123}},
    }},
}

As per @VolkerSchmid comment根据@VolkerSchmid 评论

filter := bson.D{ { Key: "$and", Value: []interface{}{ bson.D{{ Key: "_id", Value: bson.D{{ Key: "$in", Value: vids }}}}, bson.D{{ Key: "providerid", Value: 123}}, }}, }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM