mongo-go-driver mongo filter

摘要:
andor判断数组为空rd:=bson.M{"num":"1","$or":[]bson.M{{"$where":"this.logo.length˃0"},{"$where":"this.ziliao.length˃0"}}}mongo-go-driver:nestedOR/ANDqueryfilterItrytocreateaMongoDBqueryfilterwiththenestedope

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 abson.Dand passbson.Eelements into it. If I need to haveOR/ANDinsideAND/OR- I need to putbson.M + bson.Dinsidebson.Dlike 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 abson.D

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

What matters is that$orrequires an array, which isbson.A ,Also$andis 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},
    },
}

免责声明:文章转载自《mongo-go-driver mongo filter》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇redis源代码结构解析将文件以流的形式另存为下载下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

下载特定区域内百度街景照片数据

下载特定区域内百度街景照片数据 本文是在康博的博文的基础上再整理的。 01 下载路网数据 基本上都是使用 Open Street Map (OSM) 的路网数据。下载 OSM 数据的方法有很多,这里就不再赘述。 我个人是使用 OpenStreetMap Data Extracts 下载了整个中国的数据,然后用研究区的边界数据进行了裁剪。具体是使用了 Arc...

MongoDB的安装与使用

  MongoDB是一款NoSql数据库。NoSql数据库叫非关系型数据库,NoSql的全名Not only sql。是为了解决高并发、高可用、高可扩展,以及大数据存储等一系列问题而产生的数据库解决方案。NoSql,它不能替代关系型数据库,只能作为关系型数据库的一个良好补充。   NoSql数据库又分为多种,如键值(Key-Value)存储数据库(如red...

pymongo 一篇文章搞定

一 安装 pip install pymongo 二 python连接mongodb数据库的前提 确保pymongo安装完毕 mongodb数据库的服务器端(mongod)必须处于启动状态 三 连接mongodb 连接MongoDB我们需要使用PyMongo库里面的MongoClient,一般来说传入MongoDB的IP及端口即可,第一个参数为地...

MongoDB存储时间

之前一篇博客C++的时间中提到了MongoDB保存时间类型数据可以使用timestamp类型。 不过在实际编程过程中,发现保存timestamp容易,读取难。MongoDB C++的这方面的例子还没有找到。现在提供一个简便的方法。因为时间就是自1970年0点的秒数(或者毫秒数),类型是 64bit整数。 下面的代码可以将一个uint64_t(C++11的新...

MongoDb问题集

一、解决SpringBoot MongoDB插入文档默认生成_class字段问题  @Configuration public class SpringMongoConfig{ @Bean public MongoTemplate mongoTemplate() throws Exception { //remove _clas...

MongoDB 3.4.2 安装填坑

1,安装 mongodb-win32-x86_64-enterprise-windows-64-3.4.2-signed  到  Win Server 2008 Enterprise R2 上;安装打开后报错如下: ---------------------------mongo.exe - 无法找到入口--------------------------...