如何使用golang在mongodb中使用$lookup获取计数值? [英] How to get the count value using $lookup in mongodb using golang?
问题描述
使用聚合,我正在使用 golang 加入两个 mongodb 集合.结果如下:-
Using aggregation I am joining two collections of mongodb using golang. The result is seems like below:-
输出:-
{
"response": {
"code": 1,
"api_status": 1,
"message": "Success",
"total_record": [
{
"_id": 1,
"author_name": "mohit",
"category": 232,
"content": "This is the content",
"date_time": 1524632713,
"excerpt": "This is a short text",
"image": "pic.jpg",
"resultField": [
{
"_id": 6,
"comment": "this is a least comment",
"comment_on": 1524644601,
"email": "puneet@bookingkoala.com",
"name": "puneet",
"post_id": 1,
"reply_to": 1524644601,
"status": 1
},
{
"_id": 7,
"comment": "this is a least comment",
"comment_on": 1524647808,
"email": "puneet@bookingkoala.com",
"name": "puneet",
"post_id": 1,
"reply_to": 1524647808,
"status": 1
}
],
"status": 0,
"tags": "this",
"title": "how to do the code"
},
{
"_id": 2,
"author_name": "mohit",
"category": 232,
"content": "This is the content",
"date_time": 1524632713,
"excerpt": "This is a short text",
"image": "pic.jpg",
"resultField": [
{
"_id": 8,
"comment": "this is a least comment",
"comment_on": 1524648059,
"email": "puneet@bookingkoala.com",
"name": "puneet",
"post_id": 2,
"reply_to": 1524648059,
"status": 1
}
],
"status": 0,
"tags": "this",
"title": "how to do the code"
},
{
"_id": 3,
"author_name": "puneet",
"category": 2,
"content": "this is content",
"date_time": 1524641086,
"excerpt": "this is excerpt",
"image": "pic.jpg",
"resultField": [],
"status": 1,
"tags": "go",
"title": "how to do the code"
}
]
}
}
这个输出是由下面的golang代码获取的:-
This output is taken by the below code of golang:-
mongoSession := config.ConnectDb()
collection := mongoSession.DB(config.Database).C(config.BlogCollection)
pipeline := []bson.M{
bson.M{"$match": bson.M{}},
bson.M{"$lookup": bson.M{"from" : "comment", "localField" : "_id", "foreignField": "post_id","as": "resultField" }},
}
fmt.Println(pipeline)
pipe := collection.Pipe(pipeline)
resp := []bson.M{}
err = pipe.All(&resp)
if err != nil {
fmt.Println("Errored: %#v \n", err)
}
fmt.Println(resp)
if err != nil {
response = ResponseControllerList{
config.FailureCode,
config.FailureFlag,
config.FailureMsg,
nil,
nil,
}
} else {
response = ResponseControllerList{
config.SuccessFlag,
config.SuccessFlag,
config.SuccessMsg,
nil,
resp,
}
}
问题是:- 我只需要 data
的 count
不需要 data
.表示在如上所示的输出中有 resultField
显示数据,但我只需要 count
值,如:- "resultField":[2]
但它正在显示数据.如何获得输出中数据的计数值.提前致谢.
Issue is:- I need only count
of the data
not the data
. Means in the output as shown above there is the resultField
in which data is show but I only need the count
value like:- "resultField":[2]
but it is showing the data. How can I will get the count values of the data in output. Thank you in advance.
推荐答案
所以你的聚合实际上返回了 resultField
字段中的所有 comment
文档,其中隐式包含了结果,它是一个切片,您可以使用内置的 len()
函数在 Go 中检查其中的长度.
So your aggregation actually returns all comment
documents in the resultField
field, which implicitly contains the number of results, it's a slice of which you can check the length in Go using the builtin len()
function.
由于您只需要长度(comment
文档的数量),这就是为什么您只想检索此数组的大小.为此,您可以使用 $addFields
阶段将 resultField
数组替换为该数组的长度.
Since you only need the length (number of comment
documents), that's why you want to only retrieve the size of this array. For that purpose you may use the $addFields
stage to replace the resultField
array with a number being the length of this array.
pipe := c.Pipe([]bson.M{
{
"$lookup": bson.M{
"from": "comment",
"localField": "_id",
"foreignField": "post_id",
"as": "resultField",
},
},
{
"$addFields": bson.M{
"resultField": bson.M{"$size": "$resultField"},
},
},
})
请注意,$addFields
阶段等效于 $project
阶段,它明确指定输入文档中的所有现有字段并添加新字段.仅自 MongoDB 3.4 版起可用.
Note that $addFields
stage is equivalent to a $project
stage that explicitly specifies all existing fields in the input documents and adds the new fields. Available only since MongoDB version 3.4.
这篇关于如何使用golang在mongodb中使用$lookup获取计数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!