如何在Golang的MongoDB分组聚合中删除嵌套字段?

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

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

{
&quot;data&quot;: [
{
&quot;_id&quot;: &quot;60db0920a2f13ba5037c90f5&quot;,
&quot;email&quot;: &quot;jodi@admin.com&quot;,
&quot;is_verified&quot;: false,
&quot;password&quot;: &quot;...&quot;,
&quot;username&quot;: &quot;jodi&quot;
}
],
&quot;page&quot;: 1,
&quot;per_page&quot;: 3,
&quot;total&quot;: 1
}

how to remove password field? what should I do with this code?

q := c.Query(&quot;q&quot;)
perPage, err := strconv.Atoi(c.Query(&quot;per_page&quot;))
if err != nil || perPage &lt; 1 {
perPage = 10
}
page, err := strconv.Atoi(c.Query(&quot;page&quot;))
if err != nil || page &lt; 1 {
page = 1
}
startIndex, err := strconv.Atoi(c.Query(&quot;page&quot;))
if err != nil || startIndex &lt; 1 {
startIndex = 0
} else if startIndex &gt; 0 &amp;&amp; page &lt; 1 {
startIndex = (startIndex * perPage) - perPage
} else {
startIndex = 0
}
matchStage := bson.D{{&quot;$match&quot;, bson.D{{}}}}
if q != &quot;&quot; {
matchStage = bson.D{
{&quot;$match&quot;, bson.D{
{&quot;username&quot;, q},
}},
}
}
groupStage := bson.D{
{&quot;$group&quot;, bson.D{
{&quot;_id&quot;, bson.D{{&quot;_id&quot;, &quot;null&quot;}}},
{&quot;total&quot;, bson.D{{&quot;$sum&quot;, 1}}},
{&quot;data&quot;, bson.D{{&quot;$push&quot;, &quot;$$ROOT&quot;}}},
{&quot;page&quot;, bson.D{{&quot;$first&quot;, page}}},
{&quot;per_page&quot;, bson.D{{&quot;$first&quot;, perPage}}},
}}}
projectStage := bson.D{
{&quot;$project&quot;, bson.D{
{&quot;_id&quot;, 0},
{&quot;total&quot;, 1},
{&quot;page&quot;, 1},
{&quot;per_page&quot;, 1},
{&quot;data&quot;, bson.D{{&quot;$slice&quot;, []interface{}{&quot;$data&quot;, 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{
{&quot;$project&quot;, bson.D{
{&quot;data.password&quot;, 0},
}},
}
result, err := userCollection.Aggregate(ctx,
mongo.Pipeline{
matchStage, groupStage, projectStage, removeGroupItem
},
)

huangapple
  • 本文由 发表于 2021年6月30日 19:15:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/68193720.html
匿名

发表评论

匿名网友

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

确定