$and 表达式必须是一个非空数组

$and expression must be a nonempty array

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

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,则预期的参数是数组,而不是映射!