从Mongodb聚合中的另一个集合返回匹配的子对象

v09wglhw  于 2022-11-28  发布在  Go
关注(0)|答案(1)|浏览(126)

集合商品具有item_code、tax_category_code和价格。

  1. {'item_code':'A001', 'tax_category_code':'T001', 'price': 98},
  2. {'item_code':'A002', 'tax_category_code':'T002', 'price': 39},
  3. {'item_code':'A003', 'tax_category_code':'T001', 'price': 77},
  4. {'item_code':'A004', 'tax_category_code':'T003', 'price': 52},

收集tax_schema具有状态、类别和日期。每个类别都具有tax_category_code和百分比。

  1. {'status':'active', 'categories': [ {'tax_category_code':'T001', 'priority': 1, 'percentage': 0.1}, {'tax_category_code':'T002', 'priority': 3, 'percentage': 0.5}, {'tax_category_code':'T003', 'priority': 2, 'percentage': 0.87} ], 'date': '2022-11-24T00:00:00-05:00'},
  2. {'status':'inactive', 'categories': [ {'tax_category_code':'T001', 'priority': 0, 'percentage': 0.08}, {'tax_category_code':'T002', 'priority': 2, 'percentage': 0.42}, {'tax_category_code':'T003', 'priority': 4, 'percentage': 0.74} ], 'date': '2022-06-06T00:00:00-05:00'},
  3. {'status':'inactive', 'categories': [ {'tax_category_code':'T001', 'priority': 0, 'percentage': 0.05}, {'tax_category_code':'T002', 'priority': 0, 'percentage': 0.41}, {'tax_category_code':'T003', 'priority': 0, 'percentage': 0.72} ], 'date': '2022-03-31T00:00:00-05:00'}

我尝试获取与tax_category_code匹配的所有项目的列表作为结果,考虑状态为的tax_schema:活动,并显示优先级和百分比:

  1. [
  2. {'item_code':'A001', 'tax_category_code':'T001', 'price': 98, {'priority': 1, 'percentage': 0.1} },
  3. {'item_code':'A002', 'tax_category_code':'T002', 'price': 39, {'priority': 3, 'percentage': 0.5} },
  4. {'item_code':'A003', 'tax_category_code':'T001', 'price': 77, {'priority': 1, 'percentage': 0.1}},
  5. {'item_code':'A004', 'tax_category_code':'T003', 'price': 52, {'priority': 2, 'percentage': 0.87}}
  6. ]

我已经尝试了这个查询,但是我得到了每个项目上的所有tax_schema:

  1. db.getCollection('items').aggregate([
  2. {
  3. $lookup: {
  4. from: 'tax_schema',
  5. localField: 'tax_category_code',
  6. foreignField: 'categories.tax_category_code',
  7. as: 'tax_schema',
  8. },
  9. },
  10. {
  11. $project: {
  12. item_code: 1,
  13. tax_category_code: 1,
  14. price: 1,
  15. tax_schema_category: { $arrayElemAt: ['$tax_schema.categories', 0] },
  16. }
  17. }
  18. ])
x759pob2

x759pob21#

您不能直接使用$lookupcategories.tax_category_code,因为$lookup会被解析为数组字段。如果数组中有任何项目符合,您就会撷取数组中的所有项目。相反,您可以在子管缐中使用$unwind$match

  1. db.items.aggregate([
  2. {
  3. "$lookup": {
  4. "from": "tax_schema",
  5. let: {
  6. tcc: "$tax_category_code"
  7. },
  8. pipeline: [
  9. {
  10. $match: {
  11. "status": "active"
  12. }
  13. },
  14. {
  15. "$unwind": "$categories"
  16. },
  17. {
  18. $match: {
  19. $expr: {
  20. $eq: [
  21. "$$tcc",
  22. "$categories.tax_category_code"
  23. ]
  24. }
  25. }
  26. }
  27. ],
  28. "as": "tax_schema"
  29. }
  30. },
  31. {
  32. $project: {
  33. item_code: 1,
  34. tax_category_code: 1,
  35. price: 1,
  36. tax_schema_category: {
  37. $arrayElemAt: [
  38. "$tax_schema.categories",
  39. 0
  40. ]
  41. },
  42. }
  43. }
  44. ])

Mongo Playground

展开查看全部

相关问题