如何在MongoDB中从每个子对象数组字段中为父对象添加值。

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

How to add value in parent from each child array of object field in mongodb

问题

我无法在数组的每个元素上添加字段。

我使用mongodb 3.6,假设我在mongodb中有以下文档:

  1. [
  2. {
  3. "_id": ObjectId("648fc2857edfa7ee4f1ce31c"),
  4. "userId": "1",
  5. "groups": [
  6. {
  7. "_id": ObjectId("648fc2857edfa7ee4f1ce31d"),
  8. "groupId": "1",
  9. "groupLeads": [
  10. {
  11. "userId": "4"
  12. },
  13. {
  14. "userId": "5"
  15. }
  16. ]
  17. },
  18. {
  19. "_id": ObjectId("648fc2857edfa7ee4f1ce31d"),
  20. "groupId": "2",
  21. "groupLeads": [
  22. {
  23. "userId": "2"
  24. },
  25. {
  26. "userId": "3"
  27. }
  28. ]
  29. }
  30. ]
  31. }
  32. ]

我想在groups数组下添加一个名为gLead的字段,所以我使用了聚合函数来执行如下操作:

  1. db.collection.aggregate([
  2. {
  3. "$addFields": {
  4. "groups.gLead": {
  5. "$map": {
  6. "input": "$groups",
  7. "as": "g",
  8. "in": "$$g.groupLeads.userId"
  9. }
  10. }
  11. }
  12. }
  13. ])

为什么查询在第一个数组上返回如下结果:

  1. {
  2. "_id": ObjectId("648fc2857edfa7ee4f1ce31d"),
  3. "gLead": [
  4. [
  5. "4",
  6. "5"
  7. ],
  8. [
  9. "2",
  10. "3"
  11. ]
  12. ],
  13. "groupId": "1",
  14. "groupLeads": [
  15. {
  16. "userId": "4"
  17. },
  18. {
  19. "userId": "5"
  20. }
  21. ]
  22. }

而不是如下所示:

  1. {
  2. "_id": ObjectId("648fc2857edfa7ee4f1ce31d"),
  3. "gLead": [ "4", "5" ],
  4. "groupId": "1",
  5. "groupLeads": [
  6. {
  7. "userId": "4"
  8. },
  9. {
  10. "userId": "5"
  11. }
  12. ]
  13. }

我应该使用哪个聚合查询?

MongoDB游乐场链接:https://mongoplayground.net/p/xokkBwgRR2s

英文:

I cannot add the fields on each element in an array

I use mongodb 3.6, let say that I have a this documents in mongodb

  1. [
  2. {
  3. "_id": ObjectId("648fc2857edfa7ee4f1ce31c"),
  4. "userId": "1",
  5. "groups": [
  6. {
  7. "_id": ObjectId("648fc2857edfa7ee4f1ce31d"),
  8. "groupId": "1",
  9. "groupLeads": [
  10. {
  11. "userId": "4"
  12. },
  13. {
  14. "userId": "5"
  15. }
  16. ]
  17. },
  18. {
  19. "_id": ObjectId("648fc2857edfa7ee4f1ce31d"),
  20. "groupId": "2",
  21. "groupLeads": [
  22. {
  23. "userId": "2"
  24. },
  25. {
  26. "userId": "3"
  27. }
  28. ]
  29. }
  30. ]
  31. },
  32. ]

I want to add a field under the groups array lets called gLead
so I use the aggregate function to do that like this

  1. db.collection.aggregate([
  2. {
  3. "$addFields": {
  4. "groups.gLead": {
  5. "$map": {
  6. "input": "$groups",
  7. "as": "g",
  8. "in": "$$g.groupLeads.userId"
  9. }
  10. }
  11. }
  12. }
  13. ])

why the query return like this on first array

  1. {
  2. "_id": ObjectId("648fc2857edfa7ee4f1ce31d"),
  3. "gLead": [
  4. [
  5. "4",
  6. "5"
  7. ],
  8. [
  9. "2",
  10. "3"
  11. ]
  12. ],
  13. "groupId": "1",
  14. "groupLeads": [
  15. {
  16. "userId": "4"
  17. },
  18. {
  19. "userId": "5"
  20. }
  21. ]
  22. },

instead of

  1. {
  2. "_id": ObjectId("648fc2857edfa7ee4f1ce31d"),
  3. "gLead": [ "4", "5" ],
  4. "groupId": "1",
  5. "groupLeads": [
  6. {
  7. "userId": "4"
  8. },
  9. {
  10. "userId": "5"
  11. }
  12. ]
  13. },

what query of aggregate should I use?

mongodb playground: https://mongoplayground.net/p/xokkBwgRR2s

答案1

得分: 1

你是否正在寻找一个能够将$map处理后的信息与$mergeObject合并在一起的管道?类似于这样:

  1. db.collection.aggregate([
  2. {
  3. "$addFields": {
  4. groups: {
  5. "$map": {
  6. "input": "$groups",
  7. "as": "g",
  8. "in": {
  9. "$mergeObjects": [
  10. "$$g",
  11. {
  12. gLead: "$$g.groupLeads.userId"
  13. }
  14. ]
  15. }
  16. }
  17. }
  18. }
  19. }
  20. ])

在这里演示

这个答案最初使用了$zip,但输出格式不太正确

英文:

Are you looking for a pipeline that $mergeObjects the $mapped information together? Something like this:

  1. db.collection.aggregate([
  2. {
  3. "$addFields": {
  4. groups: {
  5. "$map": {
  6. "input": "$groups",
  7. "as": "g",
  8. "in": {
  9. "$mergeObjects": [
  10. "$$g",
  11. {
  12. gLead: "$$g.groupLeads.userId"
  13. }
  14. ]
  15. }
  16. }
  17. }
  18. }
  19. }
  20. ])

Playground demonstration here

This answer originally used $zip but the output format wasn't quite right

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

发表评论

匿名网友

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

确定