英文:
$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
。
谢谢。
英文:
I'm trying to create a query with mgo lib.
q := bson.M{
"$and": bson.M{
"btId": neighbour.BtId,
"timestamp": bson.M{
"$gt": sensorDataStartPoint.Timestamp,
"$lt": sensorDataStartPoint.Timestamp.Add(time.Second * 3000),
},
},
}
So this renders into 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]]]
but I get error $and expression must be a nonempty array
when trying to execute the query
It should be : btId = "123" AND timestamp > sensorDataStartPoint.Timestamp AND timestamp < sensorDataStartPoint.Timestamp + 3000s
Thank you
答案1
得分: 2
尝试一下:
q := bson.M{
"btId": neighbour.BtId,
"timestamp": bson.M{
"$gt": sensorDataStartPoint.Timestamp,
"$lt": sensorDataStartPoint.Timestamp.Add(time.Second * 3000),
},
}
在 MongoDB 查询中,默认情况下不需要使用 $and
。
另外,请注意,如果确实需要使用 $and
,那么参数应该是一个数组,而不是一个映射。
英文:
Try:
q := bson.M{
"btId": neighbour.BtId,
"timestamp": bson.M{
"$gt": sensorDataStartPoint.Timestamp,
"$lt": sensorDataStartPoint.Timestamp.Add(time.Second * 3000),
},
}
It's unnecessary to use $and
since that's the default for a MongoDB query.
Also note, if it was necessary to use an $and
the parameters expected there are an array, not a map!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论