在Golang中使用MongoDB聚合操作

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

Mongodb aggregation in golang

问题

我有一个像这样的mongodb集合:

{
    source: "...",
    url: "...",
    comments: [
        .....
    ]
}

我想根据评论数量找到前5个文档。我可以使用以下命令提示符中的查询找到所需的结果:

db.gmsNews.aggregate([
  {
     $match:{source:"..."}
  },
  {
     $unwind: "$comments"
  },
  {
     $group: {
        _id: "$url",
        size: {
           $sum: 1
        },
     }
  },
  {
     $sort : { size : -1 } 
  },
  { 
     $limit : 5
  }
])

这给我以下输出:

{ "_id" : "...", "size" : 684 }
{ "_id" : "...", "size" : 150 }

现在我想使用mgo驱动程序将此查询转换为golang。我在以下方式中使用管道:

o1 := bson.M{
    "$match" :bson.M {"source":"..."},
}

o2 := bson.M{
    "$unwind": "$comments",
}

o3 := bson.M{
    "$group": bson.M{
        "_id": "$url",
        "size": bson.M{
            "$sum": 1,
        },
    },
}

o4 := bson.M{
    "sort": bson.M{
        "size": -1,
    },
}

o5 := bson.M{
    "$limit": 5,
}

operations := []bson.M{o1, o2, o3, o4, o5}

pipe := c.Pipe(operations)

// 运行查询并捕获结果
results := []bson.M{}
err1 := pipe.One(&results)

if err1 != nil {
    fmt.Printf("ERROR : %s\n", err1.Error())
    return
}
 
fmt.Printf("URL : %s, Size: %s\n", results[0]["_id"], results[0]["size"])

不幸的是,这不起作用,我得到以下输出:

ERROR : Unsupported document type for unmarshalling: []bson.M

想知道我做错了什么,如何解决。

非常感谢您的帮助。

提前致谢。

Ripul

英文:

I have a mongodb collection like this:

{
    source: "...",
    url:  "...",
    comments: [
        .....
    ]
}

I would like to find the top 5 documents based on the number of comments. I can find the desired result using the following query in the command prompt:

db.gmsNews.aggregate([
  {
     $match:{source:"..."}
  },
  {
     $unwind: "$comments"
  },
  {
     $group: {
        _id: "$url",
        size: {
           $sum: 1
        },
     }
  },
  {
     $sort : { size : -1 } 
  },
  { 
     $limit : 5
  }
])

This gives me the following output:

{ "_id" : "...", "size" : 684 }
{ "_id" : "...", "size" : 150 }

Now I would like to translate this query into golang using the mgo driver. I am using the pipe for this in the following way:

o1 := bson.M{
		"$match" :bson.M {"source":"..."},
}

o2 := bson.M{
	"$unwind": "$comments",
}

o3 := bson.M{
	"$group": bson.M{
		"_id": "$url",
		"size": bson.M{
			"$sum": 1,
		},
	},
}

o4 := bson.M{
	"sort": bson.M{
		"size": -1,
	},
}

o5 := bson.M{
	"$limit": 5,
}

operations := []bson.M{o1, o2, o3, o4, o5}

pipe := c.Pipe(operations)

// Run the queries and capture the results
results := []bson.M{}
err1 := pipe.One(&results)

if err1 != nil {
	fmt.Printf("ERROR : %s\n", err1.Error())
	return
}
 
fmt.Printf("URL : %s, Size: %sn", results[0]["_id"], results[0]["size"])

Unfortunately this is not working and I am getting the following output:

ERROR : Unsupported document type for unmarshalling: []bson.M

Just wondering what I'm doing wrong and how to resolve this.

Any help will be highly appreciated.

Thanks in advance.

Ripul

答案1

得分: 6

 err1 := pipe.One(&results)

改为

err1 := pipe.All(&results)
英文:

Change

 err1 := pipe.One(&results)

to

err1 := pipe.All(&results)

huangapple
  • 本文由 发表于 2014年9月26日 23:04:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/26062658.html
匿名

发表评论

匿名网友

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

确定