如何将同一数据库的两个不同 collection 合并到一个新的 collection 中,该 collection 在 MongoDB 中具有相似的字段值?

How do I merge two different collections of same database into a new collection having similar field values in MongoDB?

我有一个名为“datas”的 collection,另一个 collection 在名为“challenge”的数据库中被命名为“aidretentionandgraduations”。两个 collections 都有相似的值存储在不同的字段中,称为 unitId 和 Unitid,它们的值应该用于合并这两个 collections。因此,如果 unitId==elevatorInfo.Institution_Characteristics.Unitid 显示文档,否则不显示。 以下是我尝试过的:

  db.aidretentionandgraduations([
  {
    '$lookup': {
      'from': 'datas', 
      'localField': 'Unitid', 
      'foreignField': 'unitId', 
      'as': 'nice'
    }
  }, {
    '$unwind': {
      'path': '$nice'
    }
  }, {
    '$match': {
      '$expr': {
        '$eq': [
          '$unitId', '$elevatorInfo.Institution_Characteristics.Unitid'
        ]
      }
    }
  }
])

数据collection:

{  "_id": {    "$oid": "627f925ffa5e617f51d5632e"  },  "elevatorInfo": {    "Institution_Characteristics": {      "Unitid": "139384",      "Name": "Georgia Northwestern Technical College",      "City": "Rome",      "State": "GA",      "Web_Address": "www.gntc.edu/",      "Distance_Learning": "Offers undergraduate courses and/or programs"    }  },  "studentCharges": {    "Cost": {      "Published_Tuition_And_Required_Fees": "",      "In-state": ",062",      "Out-of-state": ",462",      "Books_And_Supplies": ",500",      "Off-campus_(not_With_Family)_Room_And_Board": ",528",      "Off-campus_(not_With_Family)_Other_Expenses": ",191",      "Off-campus_(with_Family)_Other_Expenses": ",431",      "Total_Cost": "",      "Off-campus_(not_With_Family),_In-state": ",281",      "Off-campus_(not_With_Family),_Out_Of_State": ",681",      "Off-campus_(with_Family),_In-state": ",993",      "Off-campus_(with_Family),_Out-of-state": ",393"    },    "Level_of_student": {      "Undergraduate": {        "In-state": ",062",        "Out-of-state": ",462"      },      "Graduate": {        "In-state": "",        "Out-of-state": ""      }    }  }}

帮助留校和毕业 collection:

{  "_id": {    "$oid": "622ce9ba5d72be4d703e972d"  },  "financialAid": {    "Student_Financial_Aid": {      "All_Undergraduate_Students": {        "Percent_receiving_aid": "",        "Average_amount_of_aid_received": ""      },      "Any_Grant_Or_Scholarship_Aid": {        "Percent_receiving_aid": "90%",        "Average_amount_of_aid_received": ",603"      },      "Pell_Grants": {        "Percent_receiving_aid": "69%",        "Average_amount_of_aid_received": ",845"      },      "Federal_Student_Loans": {        "Percent_receiving_aid": "8%",        "Average_amount_of_aid_received": ",371"      },      "Full-time,_First-time,_Degree/certificate-seeking_Undergraduate_Students": {        "Percent_receiving_aid": "",        "Average_amount_of_aid_received": ""      }    }  },  "retentionAndGraduation": {    "Retention_And_Graduation": {      "Overall_Graduation_Rates": {        "Rate": " "      },      "Total": {        "Rate": "49%"      },      "Men": {        "Rate": "57%"      },      "Women": {        "Rate": "40%"      },      "Nonresident_Alien": {        "Rate": "100%"      },      "Transfer_Out-rate": {        "Rate": "7%"      }    }  },  "unitId": 139384,  "__v": 0}

最终输出存储为 uni:

{  "_id": {    "$oid": "622fffe6b9ccae37d3bd3b92"  },  "financialAid": {    "Student_Financial_Aid": {      "All_Undergraduate_Students": {        "Percent_receiving_aid": "",        "Average_amount_of_aid_received": ""      },      "Any_Grant_Or_Scholarship_Aid": {        "Percent_receiving_aid": "90%",        "Average_amount_of_aid_received": ",603"      },      "Pell_Grants": {        "Percent_receiving_aid": "69%",        "Average_amount_of_aid_received": ",845"      },      "Federal_Student_Loans": {        "Percent_receiving_aid": "8%",        "Average_amount_of_aid_received": ",371"      },      "Full-time,_First-time,_Degree/certificate-seeking_Undergraduate_Students": {        "Percent_receiving_aid": "",        "Average_amount_of_aid_received": ""      }    }  },  "retentionAndGraduation": {    "Retention_And_Graduation": {      "Overall_Graduation_Rates": {        "Rate": " "      },      "Total": {        "Rate": "49%"      },      "Men": {        "Rate": "57%"      },      "Women": {        "Rate": "40%"      },      "Nonresident_Alien": {        "Rate": "100%"      },      "Transfer_Out-rate": {        "Rate": "7%"      }    }  },  "unitId": 139384,  "studentCharges": {    "Cost": {      "Published_Tuition_And_Required_Fees": "",      "In-state": ",062",      "Out-of-state": ",462",      "Books_And_Supplies": ",500",      "Off-campus_(not_With_Family)_Room_And_Board": ",528",      "Off-campus_(not_With_Family)_Other_Expenses": ",191",      "Off-campus_(with_Family)_Other_Expenses": ",431",      "Total_Cost": "",      "Off-campus_(not_With_Family),_In-state": ",281",      "Off-campus_(not_With_Family),_Out_Of_State": ",681",      "Off-campus_(with_Family),_In-state": ",993",      "Off-campus_(with_Family),_Out-of-state": ",393"    },    "Level_of_student": {      "Undergraduate": {        "In-state": ",062",        "Out-of-state": ",462"      },      "Graduate": {        "In-state": "",        "Out-of-state": ""      }    }  },  "elevatorInfo": {    "Institution_Characteristics": {      "Unitid": "139384",      "Name": "Georgia Northwestern Technical College",      "City": "Rome",      "State": "GA",      "Web_Address": "www.gntc.edu/",      "Distance_Learning": "Offers undergraduate courses and/or programs"    }  },  "__v": 0}

在 $out 或 $merge 之前使用 $out or $merge aggregate stages. I didn't exactly understand what is your problem with $merge since you didn't provide your code, but if you need modify output of $lookup, you can do it via $project

真正的问题是类型转换。我没有注意到 unitId 是一个数字(int32)但是 Unitid 是一个字符串,因为没有进行这个聚合操作。 Mongodb 社区论坛帮助我意识到了这样一个微不足道的错误。这是 link。带有类型转换的代码如下:

    [
  {
    '$addFields': {
      'unitId': {
        '$toString': '$unitId'
      }
    }
  }, {
    '$lookup': {
      'from': 'datas', 
      'localField': 'unitId', 
      'foreignField': 'elevatorInfo.Institution_Characteristics.Unitid', 
      'as': 'nice'
    }
  }, {
    '$unwind': {
      'path': '$nice'
    }
  }, {
    '$project': {
      '__v': 0, 
      '_id': 0
    }
  }
]