英文:
How to remove nested field in golang mongodb group aggregation?
问题
我是你的中文翻译助手,以下是翻译好的内容:
我是golang和mongodb的初学者,我在使用mongodb的聚合操作中遇到了一个问题,即如何删除嵌套字段。我刚刚阅读了mongodb的文档,我认为可以使用mongodb的$project操作来删除字段,但是我找不到相关的示例。这是查询的结果:
{
"data": [
{
"_id": "60db0920a2f13ba5037c90f5",
"email": "jodi@admin.com",
"is_verified": false,
"password": "...",
"username": "jodi"
}
],
"page": 1,
"per_page": 3,
"total": 1
}
如何删除password字段?对于这段代码,我应该怎么做?
q := c.Query("q")
perPage, err := strconv.Atoi(c.Query("per_page"))
if err != nil || perPage < 1 {
perPage = 10
}
page, err := strconv.Atoi(c.Query("page"))
if err != nil || page < 1 {
page = 1
}
startIndex, err := strconv.Atoi(c.Query("page"))
if err != nil || startIndex < 1 {
startIndex = 0
} else if startIndex > 0 && page < 1 {
startIndex = (startIndex * perPage) - perPage
} else {
startIndex = 0
}
matchStage := bson.D{{"$match", bson.D{{}}}}
if q != "" {
matchStage = bson.D{
{"$match", bson.D{
{"username", q},
}},
}
}
groupStage := bson.D{
{"$group", bson.D{
{"_id", bson.D{{"_id", "null"}}},
{"total", bson.D{{"$sum", 1}}},
{"data", bson.D{{"$push", "$$ROOT"}}},
{"page", bson.D{{"$first", page}}},
{"per_page", bson.D{{"$first", perPage}}},
}}}
projectStage := bson.D{
{"$project", bson.D{
{"_id", 0},
{"total", 1},
{"page", 1},
{"per_page", 1},
{"data", bson.D{{"$slice", []interface{}{"$data", startIndex, perPage}}}},
}}}
result, err := userCollection.Aggregate(ctx,
mongo.Pipeline{
matchStage, groupStage, projectStage,
},
)
希望对你有帮助!如果你有任何其他问题,请随时提问。
英文:
I beginner in golang and mongodb, and I have problem with remove nested field using aggregate group mongodb. I just read the mongodb doc and I think remove field can handle with mongodb $project but I cant find the example to do that. this is the result of query
{
"data": [
{
"_id": "60db0920a2f13ba5037c90f5",
"email": "jodi@admin.com",
"is_verified": false,
"password": "...",
"username": "jodi"
}
],
"page": 1,
"per_page": 3,
"total": 1
}
how to remove password field? what should I do with this code?
q := c.Query("q")
perPage, err := strconv.Atoi(c.Query("per_page"))
if err != nil || perPage < 1 {
perPage = 10
}
page, err := strconv.Atoi(c.Query("page"))
if err != nil || page < 1 {
page = 1
}
startIndex, err := strconv.Atoi(c.Query("page"))
if err != nil || startIndex < 1 {
startIndex = 0
} else if startIndex > 0 && page < 1 {
startIndex = (startIndex * perPage) - perPage
} else {
startIndex = 0
}
matchStage := bson.D{{"$match", bson.D{{}}}}
if q != "" {
matchStage = bson.D{
{"$match", bson.D{
{"username", q},
}},
}
}
groupStage := bson.D{
{"$group", bson.D{
{"_id", bson.D{{"_id", "null"}}},
{"total", bson.D{{"$sum", 1}}},
{"data", bson.D{{"$push", "$$ROOT"}}},
{"page", bson.D{{"$first", page}}},
{"per_page", bson.D{{"$first", perPage}}},
}}}
projectStage := bson.D{
{"$project", bson.D{
{"_id", 0},
{"total", 1},
{"page", 1},
{"per_page", 1},
{"data", bson.D{{"$slice", []interface{}{"$data", startIndex, perPage}}}},
}}}
result, err := userCollection.Aggregate(ctx,
mongo.Pipeline{
matchStage, groupStage, projectStage,
},
)
答案1
得分: 0
你可以明确指定你想在 data
中看到的内部文档的字段。
MongoDB文档:$project嵌入文档字段
英文:
You can explicitly specify which fields in inner document you wanna see in data
.
Mongo doc: $project embeded document fields
答案2
得分: 0
根据@sirsova的建议,我在mongo.Pipeline中添加了另一个$project,并解决了这个问题。
removeGroupItem := bson.D{
{"$project", bson.D{
{"data.password", 0},
}},
}
result, err := userCollection.Aggregate(ctx,
mongo.Pipeline{
matchStage, groupStage, projectStage, removeGroupItem
},
)
英文:
based on @sirsova suggestion, I add another $project inside mongo.Pipline and solve the problem.
removeGroupItem := bson.D{
{"$project", bson.D{
{"data.password", 0},
}},
}
result, err := userCollection.Aggregate(ctx,
mongo.Pipeline{
matchStage, groupStage, projectStage, removeGroupItem
},
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论