Mongodb $and表达式必须是非空数组
我正在尝试使用mgo lib创建一个查询Mongodb $and表达式必须是非空数组,mongodb,go,mgo,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), }
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]]]
但我在尝试执行查询时遇到错误$,表达式必须是非空数组
它应该是:btId=“123”和timestamp>sensorDataStartPoint.timestamp和timestamp
谢谢试试:
q := bson.M{
"btId": neighbour.BtId,
"timestamp": bson.M{
"$gt": sensorDataStartPoint.Timestamp,
"$lt": sensorDataStartPoint.Timestamp.Add(time.Second * 3000),
},
}
没有必要使用$和,因为这是MongoDB查询的默认值
另外请注意,如果需要使用$和参数,则需要使用数组,而不是映射 请查看文档以了解更多信息。它的参数是一个“数组”[]
,您提供的BSON映射不是数组。您也不需要实际使用$和
,因为所有MongoDB查询条件都是altready和“and”条件,除非另有明确说明(即“timestamp”):{“$gt”:data,“$lt”:data}
是编写“and”的另一种方式。因此只需删除包装“$和”::bson.M{}
只需将条件直接用作查询映射的键即可。