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

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

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

问题

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

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

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

我在这里漏掉了什么?

  1. lte := 10
  2. gte := 0
  3. operations := []bson.M{
  4. {
  5. "$match":{
  6. "$and":[]interface{}{
  7. bson.M{"age":{"$gte":gte}},
  8. bson.M{"age":{"$lte":lte}},
  9. },
  10. },
  11. },
  12. {
  13. "$group":bson.M{"_id":"$user_id"},
  14. },
  15. }
  16. r := []bson.M{}
  17. pipe := c.Pipe(operations)
  18. err := pipe.All(&r)
  19. if err != nil {
  20. logrus.Errorf("Error: %v", err)
  21. return err
  22. }
英文:

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?

  1. lte := 10
  2. gte := 0
  3. operations := []bson.M{
  4. {
  5. "$match":{
  6. "$and":[]interface{}{
  7. bson.M{"age":{"$gte":gte}},
  8. bson.M{"age":{"$lte":lte}},
  9. },
  10. },
  11. },
  12. {
  13. "$group":bson.M{"_id":"$user_id"},
  14. },
  15. }
  16. r := []bson.M{}
  17. pipe := c.Pipe(operations)
  18. err := pipe.All(&r)
  19. if err != nil {
  20. logrus.Errorf("Error: %v", err)
  21. return err
  22. }

答案1

得分: 3

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

  1. operations := []bson.M{
  2. bson.M{
  3. "$match": bson.M{
  4. "date": bson.M{"$gte": gte, "$lte": lte}
  5. }
  6. },
  7. bson.M{
  8. "$group": bson.M{"_id": "$user_id"}
  9. }
  10. }

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

在JavaScript shell中,代码如下:

  1. var operations = [
  2. { "$match": { "date": { "$gte": gte, "$lte": lte } } },
  3. { "$group": { "_id": "$user_id" } }
  4. ];

因此,您需要记住,每个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:

  1. operations := []bson.M{
  2. bson.M{
  3. "$match": bson.M{
  4. "date": bson.M{ "$gte": gte, "$lte": lte }
  5. }
  6. },
  7. bson.M{
  8. "group": bson.M{ "_id": "$user_id" }
  9. }
  10. }

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:

  1. var operations = [
  2. { "$match": { "date": { "$gte": gte, "$lte": lte } } },
  3. { "$group": { "_id": "$user_id" } }
  4. ];

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

我相信这是你想要的:

  1. operations := []bson.M{
  2. bson.M{
  3. "$match": bson.M{
  4. "$and": []interface{}{
  5. bson.M{"age": bson.M{"$gte": gte}},
  6. bson.M{"age": bson.M{"$lte": lte}},
  7. },
  8. },
  9. },
  10. bson.M{
  11. "$group": bson.M{"_id": "$user_id"},
  12. },
  13. }

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

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

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

英文:

I believe this is what you want:

  1. operations := []bson.M{
  2. bson.M{
  3. "$match":bson.M{
  4. "$and":[]interface{}{
  5. bson.M{"age":bson.M{"$gte":gte}},
  6. bson.M{"age":bson.M{"$lte":lte}},
  7. },
  8. },
  9. },
  10. bson.M{
  11. "$group":bson.M{"_id":"$user_id"},
  12. },
  13. }

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:

确定