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

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

How to remove nested field in golang mongodb group aggregation?

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是golang和mongodb的初学者,我在使用mongodb的聚合操作中遇到了一个问题,即如何删除嵌套字段。我刚刚阅读了mongodb的文档,我认为可以使用mongodb的$project操作来删除字段,但是我找不到相关的示例。这是查询的结果:

  1. {
  2. "data": [
  3. {
  4. "_id": "60db0920a2f13ba5037c90f5",
  5. "email": "jodi@admin.com",
  6. "is_verified": false,
  7. "password": "...",
  8. "username": "jodi"
  9. }
  10. ],
  11. "page": 1,
  12. "per_page": 3,
  13. "total": 1
  14. }

如何删除password字段?对于这段代码,我应该怎么做?

  1. q := c.Query("q")
  2. perPage, err := strconv.Atoi(c.Query("per_page"))
  3. if err != nil || perPage < 1 {
  4. perPage = 10
  5. }
  6. page, err := strconv.Atoi(c.Query("page"))
  7. if err != nil || page < 1 {
  8. page = 1
  9. }
  10. startIndex, err := strconv.Atoi(c.Query("page"))
  11. if err != nil || startIndex < 1 {
  12. startIndex = 0
  13. } else if startIndex > 0 && page < 1 {
  14. startIndex = (startIndex * perPage) - perPage
  15. } else {
  16. startIndex = 0
  17. }
  18. matchStage := bson.D{{"$match", bson.D{{}}}}
  19. if q != "" {
  20. matchStage = bson.D{
  21. {"$match", bson.D{
  22. {"username", q},
  23. }},
  24. }
  25. }
  26. groupStage := bson.D{
  27. {"$group", bson.D{
  28. {"_id", bson.D{{"_id", "null"}}},
  29. {"total", bson.D{{"$sum", 1}}},
  30. {"data", bson.D{{"$push", "$$ROOT"}}},
  31. {"page", bson.D{{"$first", page}}},
  32. {"per_page", bson.D{{"$first", perPage}}},
  33. }}}
  34. projectStage := bson.D{
  35. {"$project", bson.D{
  36. {"_id", 0},
  37. {"total", 1},
  38. {"page", 1},
  39. {"per_page", 1},
  40. {"data", bson.D{{"$slice", []interface{}{"$data", startIndex, perPage}}}},
  41. }}}
  42. result, err := userCollection.Aggregate(ctx,
  43. mongo.Pipeline{
  44. matchStage, groupStage, projectStage,
  45. },
  46. )

希望对你有帮助!如果你有任何其他问题,请随时提问。

英文:

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

  1. {
  2. &quot;data&quot;: [
  3. {
  4. &quot;_id&quot;: &quot;60db0920a2f13ba5037c90f5&quot;,
  5. &quot;email&quot;: &quot;jodi@admin.com&quot;,
  6. &quot;is_verified&quot;: false,
  7. &quot;password&quot;: &quot;...&quot;,
  8. &quot;username&quot;: &quot;jodi&quot;
  9. }
  10. ],
  11. &quot;page&quot;: 1,
  12. &quot;per_page&quot;: 3,
  13. &quot;total&quot;: 1
  14. }

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

  1. q := c.Query(&quot;q&quot;)
  2. perPage, err := strconv.Atoi(c.Query(&quot;per_page&quot;))
  3. if err != nil || perPage &lt; 1 {
  4. perPage = 10
  5. }
  6. page, err := strconv.Atoi(c.Query(&quot;page&quot;))
  7. if err != nil || page &lt; 1 {
  8. page = 1
  9. }
  10. startIndex, err := strconv.Atoi(c.Query(&quot;page&quot;))
  11. if err != nil || startIndex &lt; 1 {
  12. startIndex = 0
  13. } else if startIndex &gt; 0 &amp;&amp; page &lt; 1 {
  14. startIndex = (startIndex * perPage) - perPage
  15. } else {
  16. startIndex = 0
  17. }
  18. matchStage := bson.D{{&quot;$match&quot;, bson.D{{}}}}
  19. if q != &quot;&quot; {
  20. matchStage = bson.D{
  21. {&quot;$match&quot;, bson.D{
  22. {&quot;username&quot;, q},
  23. }},
  24. }
  25. }
  26. groupStage := bson.D{
  27. {&quot;$group&quot;, bson.D{
  28. {&quot;_id&quot;, bson.D{{&quot;_id&quot;, &quot;null&quot;}}},
  29. {&quot;total&quot;, bson.D{{&quot;$sum&quot;, 1}}},
  30. {&quot;data&quot;, bson.D{{&quot;$push&quot;, &quot;$$ROOT&quot;}}},
  31. {&quot;page&quot;, bson.D{{&quot;$first&quot;, page}}},
  32. {&quot;per_page&quot;, bson.D{{&quot;$first&quot;, perPage}}},
  33. }}}
  34. projectStage := bson.D{
  35. {&quot;$project&quot;, bson.D{
  36. {&quot;_id&quot;, 0},
  37. {&quot;total&quot;, 1},
  38. {&quot;page&quot;, 1},
  39. {&quot;per_page&quot;, 1},
  40. {&quot;data&quot;, bson.D{{&quot;$slice&quot;, []interface{}{&quot;$data&quot;, startIndex, perPage}}}},
  41. }}}
  42. result, err := userCollection.Aggregate(ctx,
  43. mongo.Pipeline{
  44. matchStage, groupStage, projectStage,
  45. },
  46. )

答案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,并解决了这个问题。

  1. removeGroupItem := bson.D{
  2. {"$project", bson.D{
  3. {"data.password", 0},
  4. }},
  5. }
  6. result, err := userCollection.Aggregate(ctx,
  7. mongo.Pipeline{
  8. matchStage, groupStage, projectStage, removeGroupItem
  9. },
  10. )
英文:

based on @sirsova suggestion, I add another $project inside mongo.Pipline and solve the problem.

  1. removeGroupItem := bson.D{
  2. {&quot;$project&quot;, bson.D{
  3. {&quot;data.password&quot;, 0},
  4. }},
  5. }
  6. result, err := userCollection.Aggregate(ctx,
  7. mongo.Pipeline{
  8. matchStage, groupStage, projectStage, removeGroupItem
  9. },
  10. )

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:

确定