聚合在Golang/Mongodb中出现错误“复合字面量中缺少类型”。

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

Aggregation With Golang/Mongodb Gives Error "Missing type in composite literal"

问题

我正在尝试通过聚合来获取特定年龄范围内的用户。

**编辑:**我能够通过mongo shell工作,查询正常运行,但是我无法在go中使其工作。

这段代码给我返回了"missing type in composite literal"错误。

我在这里漏掉了什么?

lte := 10
gte := 0

operations := []bson.M{
	{
		"$match":{
			"$and":[]interface{}{
				bson.M{"age":{"$gte":gte}},
				bson.M{"age":{"$lte":lte}},
			},
		},
	},
	{
		"$group":bson.M{"_id":"$user_id"},
	},

}

r := []bson.M{}

pipe := c.Pipe(operations)
err := pipe.All(&r)
if err != nil {
	logrus.Errorf("Error: %v", err)
	return err
}
英文:

I am trying to get users in a certain age range via aggregation.

Edit: I am able to work through mongo shell, query works fine however I am not able to make it work with go

>This code gives me "missing type in composite literal" error.

What am I missing here?

lte := 10
gte := 0

operations := []bson.M{
	{
		"$match":{
			"$and":[]interface{}{
				bson.M{"age":{"$gte":gte}},
				bson.M{"age":{"$lte":lte}},
			},
		},
	},
	{
		"$group":bson.M{"_id":"$user_id"},
	},

}

r := []bson.M{}

pipe := c.Pipe(operations)
err := pipe.All(&r)
if err != nil {
	logrus.Errorf("Error:  %v", err)
	return err
}

答案1

得分: 3

您尚未将"each"管道阶段定义为bson.M。它是一个bson.M的"数组",因此出现了错误:

operations := []bson.M{
    bson.M{
        "$match": bson.M{
            "date": bson.M{"$gte": gte, "$lte": lte}
        }
    },
    bson.M{
        "$group": bson.M{"_id": "$user_id"}
    }
}

此外,$and是不必要的。所有 MongoDB 查询条件已经是一个"AND"表达式。在同一个对象表达式中,$gte$lte的组合作为键已经是对同一"date"属性的AND条件。就像在$match对象中添加更多键一样。

在JavaScript shell中,代码如下:

var operations = [
    { "$match": { "date": { "$gte": gte, "$lte": lte } } },
    { "$group": { "_id": "$user_id" } }
];

因此,您需要记住,每个JavaScript对象表示法{}等于.bson.M{}语句。这是您在使用此库时在go中表示BSON对象的方式。

注意:{ "$group": { "_id": "$user_id" } }除了返回每个"distinct"的"user_id"值之外,不会有任何有意义的操作。因此,在任何实际结果中,您可能需要更多的累加器选项

英文:

You have not defined "each" pipeline stage as a bson.M. It's an "array" of bson.M, hence the error:

operations := []bson.M{
    bson.M{
        "$match": bson.M{
            "date": bson.M{ "$gte": gte, "$lte": lte }
        }
    },
    bson.M{
        "group": bson.M{ "_id": "$user_id" }
    }
}

Also $and is not necessary. ALL MongoDB query conditions are already an "AND" expression anyway. The combination of $gte and $lte as keys in the same object expression is already an AND condtion on the same "date" property. Just as would be the addition of more keys within the $match object.

In the JavaScript shell, this is the same as:

var operations = [
    { "$match": { "date": { "$gte": gte, "$lte": lte } } },
    { "$group": { "_id": "$user_id" } }
];

So you need to remember that every JavaScript object notation {} equals a .bson.M{} statement. That's how you notate BSON objects in go with this libary.

Note: { "$group": { "_id": "$user_id } } is not going to do anything meaningful other than just return each "distinct" "user_id" value. So you likely want more accumulator options in any real result

答案2

得分: -1

我相信这是你想要的:

operations := []bson.M{
    bson.M{
        "$match": bson.M{
            "$and": []interface{}{
                bson.M{"age": bson.M{"$gte": gte}},
                bson.M{"age": bson.M{"$lte": lte}},
            },
        },
    },
    bson.M{
        "$group": bson.M{"_id": "$user_id"},
    },
}

你只是忘记在"$and"之前加上bson.M

编辑:看起来年龄也是一样的,我已经修复了。

编辑2:我错过了更多的bson.M,不仅仅是最初看到的那3个。

英文:

I believe this is what you want:

operations := []bson.M{
    bson.M{
        "$match":bson.M{
            "$and":[]interface{}{
                bson.M{"age":bson.M{"$gte":gte}},
                bson.M{"age":bson.M{"$lte":lte}},
            },
        },
    },
    bson.M{
        "$group":bson.M{"_id":"$user_id"},
    },

}

You simply forgot bson.M before "$and".

Edit: Looks like the ages too, I went ahead and fixed those.

Edit2: I missed more bson.M's than the initial 3 missing that I saw.

huangapple
  • 本文由 发表于 2016年3月22日 06:40:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/36142981.html
匿名

发表评论

匿名网友

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

确定