我正在尝试使用 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/