Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb $and表达式必须是非空数组_Mongodb_Go_Mgo - Fatal编程技术网

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

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), }

我正在尝试使用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]]]
但我在尝试执行查询时遇到错误
$,表达式必须是非空数组

它应该是:
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{}
只需将条件直接用作查询映射的键即可。