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

huangapple go评论88阅读模式
英文:

$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{
	&quot;$and&quot;: bson.M{
		&quot;btId&quot;: neighbour.BtId,
		&quot;timestamp&quot;: bson.M{
			&quot;$gt&quot;: sensorDataStartPoint.Timestamp,
			&quot;$lt&quot;: 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 = &quot;123&quot; AND timestamp &gt; sensorDataStartPoint.Timestamp AND timestamp &lt; 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{
    &quot;btId&quot;: neighbour.BtId,
    &quot;timestamp&quot;: bson.M{
        &quot;$gt&quot;: sensorDataStartPoint.Timestamp,
        &quot;$lt&quot;: 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!

huangapple
  • 本文由 发表于 2016年3月7日 04:26:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/35832113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定