mongodb - 如何使用golang在mongodb中使用$lookup获取计数值?
问题描述
使用聚合我正在使用 golang 加入两个 mongodb 集合。结果如下所示:-
输出:-
{
"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 代码获取:-
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,
}
}
问题是: -我只需count
要不是. 如上所示,在输出中意味着显示数据,但我只需要如下值:-但它正在显示数据。我怎样才能得到输出中数据的计数值。先感谢您。data
data
resultField
count
"resultField":[2]
解决方案
因此,您的聚合实际上返回comment
该字段中的所有文档resultField
,其中隐含包含结果的数量,它是您可以使用内置len()
函数在 Go 中检查长度的切片。
由于您只需要长度(comment
文档数),因此您只想检索此数组的大小。为此,您可以使用$addFields
舞台将数组替换为该resultField
数组长度的数字。
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
stage 等效于$project
显式指定输入文档中所有现有字段并添加新字段的阶段。仅从 MongoDB 版本 3.4 开始可用。
推荐阅读
- python-3.x - x 秒后超时 discord.py 重写
- python - exe 文件可以修改包含的文本文件吗?
- node.js - 巢穴 | 配置模块 | 在 `validate` 函数中使用(或注入)服务
- python - Discord 机器人扩展运行良好,但不起作用
- python - jupyter notebook 中由 shift+tab 打开的帮助选项卡无法正确呈现方法/函数的文档字符串
- user-interface - 如何结合谷歌登录和普通登录?
- javascript - 如何在引导程序中创建自动完成搜索框?
- javascript - FAILURE:构建失败并出现异常。* 其中:构建文件 'C:\Users\mayev\dev\remplazo\Chronos\android\app\build.gradle' 行:81
- javascript - axios将get查询url转换成简单的正斜杠url
- vb.net - 使用 vb.net 可以构造动态变量