MongoDB聚合用于基于ID过滤列表并将此过滤列表映射到另一个字段

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

MongoDb aggregation for filtering list based on ids and mapping this filtered list to another field

问题

以下是您提供的内容的翻译结果:

在我的 MongoDB 中,数据如下所示:

  1. {
  2. "classes": [
  3. {
  4. "classId": "SSC",
  5. "studentIds": [
  6. "1"
  7. ]
  8. },
  9. {
  10. "classId": "HSC",
  11. "studentIds": [
  12. "2",
  13. "3"
  14. ]
  15. }
  16. ],
  17. "students": [
  18. {
  19. "_id": "1",
  20. "student": {}
  21. },
  22. {
  23. "_id": "2",
  24. "student": {}
  25. },
  26. {
  27. "_id": "3",
  28. "student": {}
  29. }
  30. ]
  31. }

我希望进行聚合查询,使其返回如下所示的数据:

  1. "classes": [
  2. {
  3. "classId": "SSC",
  4. "students": [
  5. {
  6. "id": "1",
  7. "student": {}
  8. }
  9. ]
  10. },
  11. {
  12. "classId": "HSC",
  13. "students": [
  14. {
  15. "id": "2",
  16. "student": {},
  17. },
  18. {
  19. "id": "3",
  20. "student": {},
  21. }
  22. ]
  23. }
  24. ]

在这个问题中,我有一系列的 ID。它应该过滤该 ID 的学生列表,然后获取该对象,并将该对象放入班级数组中。

我尝试使用 MongoDB 聚合来解决这个问题。但不幸的是,我无法为此编写查询。那么,我们能否使用聚合来实现上述情况呢?

我正在使用 Spring Boot 的 mongoTemplate。

英文:

In my mongodb I have the data as shown below:

  1. {
  2. "classes": [
  3. {
  4. "classId": "SSC",
  5. "studentIds": [
  6. "1"
  7. ]
  8. },
  9. {
  10. "classId": "HSC",
  11. "studentIds": [
  12. "2",
  13. "3"
  14. ]
  15. }
  16. ],
  17. "students": [
  18. {
  19. "_id": "1",
  20. "student": {}
  21. },
  22. {
  23. "_id": "2",
  24. "student": {}
  25. },
  26. {
  27. "_id": "3",
  28. "student": {}
  29. }
  30. ],
  31. }

And I want an aggregation query such that it returns the data as shown below:

  1. "classes": [
  2. {
  3. "classId":"SSC",
  4. "students": [
  5. {
  6. "id": "1",
  7. "student": {}
  8. }
  9. ]
  10. },
  11. {
  12. "classId":"HSC",
  13. "students": [
  14. {
  15. "id": "2",
  16. "student": {},
  17. },
  18. {
  19. "id": "3",
  20. "student": {}
  21. }
  22. ]
  23. }
  24. ]

In this I have list of ids. It should filter the students list for that id and take that object and place this object in class array.

I have tried to use mongodb aggregation for this problem. But unfortunately I am not able write query for that. So can we achieve the above scenario using aggregation.

I am using spring boot mongoTemplate.

答案1

得分: 1

你可以使用$map$filter

  • 使用$map操作在classes数组中创建students字段,并在students数组中进行$filter操作,检查数组中是否存在符合条件的ids。
  1. db.collection.aggregate([
  2. {
  3. $project: {
  4. classes: {
  5. $map: {
  6. input: "$classes",
  7. as: "c",
  8. in: {
  9. classId: "$$c.classId",
  10. students: {
  11. $filter: {
  12. input: "$students",
  13. cond: { $in: ["$$this._id", "$$c.studentIds"] }
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }
  21. ])

Playground

英文:

You can use $map and $filter,

  • $map input classes array create students field and do $filter in students array and check condition ids in array or not
  1. db.collection.aggregate([
  2. {
  3. $project: {
  4. classes: {
  5. $map: {
  6. input: "$classes",
  7. as: "c",
  8. in: {
  9. classId: "$$c.classId",
  10. students: {
  11. $filter: {
  12. input: "$students",
  13. cond: { $in: ["$$this._id", "$$c.studentIds"] }
  14. }
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }
  21. ])

Playground

huangapple
  • 本文由 发表于 2020年9月9日 03:32:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63800466.html
匿名

发表评论

匿名网友

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

确定