mongodb - $and 表达式必须是非空数组

标签 mongodb go mgo

我正在尝试使用 mgo lib 创建查询。

q := bson.M{
    "$and": bson.M{
        "btId": neighbour.BtId,
        "timestamp": bson.M{
            "$gt": sensorDataStartPoint.Timestamp,
            "$lt": sensorDataStartPoint.Timestamp.Add(time.Second * 3000),
        },
    },
}

所以这会呈现为 map[$and:map[btId:BTR0102 timestamp:map[$gt:2012-04-11 19:08:59 +0200 CEST $lt:2012-04-11 19:58:59 +0200 CEST]]]但我收到错误 $and expression must be a nonempty array当尝试执行查询时

应该是:btId = "123" AND timestamp > sensorDataStartPoint.Timestamp AND timestamp < sensorDataStartPoint.Timestamp + 3000s

谢谢

最佳答案

尝试:

q := bson.M{
    "btId": neighbour.BtId,
    "timestamp": bson.M{
        "$gt": sensorDataStartPoint.Timestamp,
        "$lt": sensorDataStartPoint.Timestamp.Add(time.Second * 3000),
    },
}

没有必要使用 $and,因为这是 MongoDB 查询的默认值。

另请注意,如果需要使用 $and ,则预期参数是数组,而不是映射!

关于mongodb - $and 表达式必须是非空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35832113/

相关文章:

python - 是否有使用Mongodb和Django的明确方法?

java - 使用嵌入式 MongoDb 启动 Spring boot 时出现异常

go - BigTable 中多列的过滤器

go - 在 go 函数注释中编写代码块的正确方法是什么?

mongodb - 我如何在mongodb中找到附近的纬度和经度?

MongoDB .limit() 忽略 .sort()?

node.js - 如何在 Nodejs 中使用异步

go - 求两个数的公因数的最有效方法

mongodb - 如何在带有库mgo的golang中使用$ facet?

mongodb - 从 mgo golang 执行 mongodb 函数