typescript 从多个mongodb集合中查询数据

0x6upsns  于 2023-06-24  发布在  TypeScript
关注(0)|答案(1)|浏览(225)

你好,我有一个数据库,有这些以下集合的

  1. db={
  2. "category": [
  3. {
  4. "_id": 1,
  5. "item": "Cat A",
  6. },
  7. {
  8. "_id": 2,
  9. "item": "Cat B",
  10. },
  11. ],
  12. "sticker": [
  13. {
  14. "_id": 1,
  15. "item": "Sticker 1",
  16. },
  17. ],
  18. "prefix": [
  19. {
  20. "_id": 1,
  21. "item": "prefix 1",
  22. },
  23. ],
  24. "store": [
  25. {
  26. "_id": 1,
  27. "item": "Item 1",
  28. "category_id": "1",
  29. "sticker_id": "1",
  30. "prefix_id": "1",
  31. },
  32. {
  33. "_id": 2,
  34. "item": "Item 2",
  35. "category_id": "2",
  36. "sticker_id": "1",
  37. "prefix_id": "1",
  38. },
  39. {
  40. "_id": 3,
  41. "item": "Item 3",
  42. "category_id": "1",
  43. "sticker_id": "1",
  44. "prefix_id": "1",
  45. },
  46. ],
  47. }

我正在使用下面的查询从mongo集合中获取数据

  1. await category.aggregate([
  2. {
  3. $match: {
  4. _id: 1,
  5. }
  6. },
  7. {
  8. $lookup: {
  9. "from": "store",
  10. let: { cid: "$_id" },
  11. pipeline: [
  12. {$match: {$expr: {$eq: [ "$category_id", "$$cid" ]} }},
  13. ],
  14. as: "stores"
  15. }
  16. }
  17. ])

通过这个查询,我得到了category、prefix和sticker的objectId作为响应,但我需要与objectId沿着的数据,如下所示

预期响应

  1. [
  2. {
  3. "_id": 1,
  4. "item": "Cat A",
  5. "stores": [{
  6. "_id": 1,
  7. "item": "item 1",
  8. "stickerData": {
  9. "_id": 1,
  10. "item": "Sticker 1",
  11. },
  12. "prefixData": {
  13. "_id": 1,
  14. "item": "prefix 1",
  15. },
  16. },
  17. {
  18. "_id": 3,
  19. "item": "item 3",
  20. "stickerData": {
  21. "_id": 1,
  22. "item": "Sticker 1",
  23. },
  24. "prefixData": {
  25. "_id": 1,
  26. "item": "prefix 1",
  27. },
  28. },
  29. ],
  30. },
  31. ]

有没有人能提出一些适当的问题

m2xkgtsf

m2xkgtsf1#

$lookup stage中,您必须有多个$lookup stage,并通过管道连接 prefixsticker 集合。
请注意,storeprefixsticker 集合中的_id都是数字,而 store 集合中的store_idprefix_idsticker_id是字符串,请确保在连接时,两个id应作为相同类型进行比较。下面的查询示例将_id转换为字符串。

  1. db.category.aggregate([
  2. {
  3. $match: {
  4. _id: 1,
  5. }
  6. },
  7. {
  8. $lookup: {
  9. "from": "store",
  10. let: {
  11. cid: {
  12. $toString: "$_id"
  13. }
  14. },
  15. pipeline: [
  16. {
  17. $match: {
  18. $expr: {
  19. $eq: [
  20. "$category_id",
  21. "$$cid"
  22. ]
  23. }
  24. }
  25. },
  26. {
  27. $lookup: {
  28. from: "sticker",
  29. let: {
  30. sticker_id: "$sticker_id"
  31. },
  32. pipeline: [
  33. {
  34. $match: {
  35. $expr: {
  36. $eq: [
  37. {
  38. $toString: "$_id"
  39. },
  40. "$$sticker_id"
  41. ]
  42. }
  43. }
  44. }
  45. ],
  46. as: "stickerData"
  47. }
  48. },
  49. {
  50. $lookup: {
  51. from: "prefix",
  52. let: {
  53. prefix_id: "$prefix_id"
  54. },
  55. pipeline: [
  56. {
  57. $match: {
  58. $expr: {
  59. $eq: [
  60. {
  61. $toString: "$_id"
  62. },
  63. "$$prefix_id"
  64. ]
  65. }
  66. }
  67. }
  68. ],
  69. as: "prefixData"
  70. }
  71. },
  72. {
  73. $project: {
  74. _id: 1,
  75. item: 1,
  76. prefixData: {
  77. $first: "$prefixData"
  78. },
  79. stickerData: {
  80. $first: "$stickerData"
  81. }
  82. }
  83. }
  84. ],
  85. as: "stores"
  86. }
  87. }
  88. ])

Demo @ Mongo Playground

展开查看全部

相关问题