现有两个集合如下:

company: {_id: ObjectId("..."), countryId: "566eb3c704c7b31facbb0007"}
country: { _id: ObjectId("566eb3c704c7b31facbb0007"), name:"..."} 
复制代码

正常关联:

{ $lookup: {
       from: "country",
       localField: "countryId",    <= string
       foreignField: "_id",    <= ObjectId
       as: "country"
复制代码

但结果却是无效

解决方案:

使用 $project

db.company.aggregate([
    "$project": {
      "countryId": {
        "$toObjectId": "$countryId"
    "$lookup": {
      "from": "country",
      "localField": "countryId",
      "foreignField": "_id",
      "as": "country"
复制代码

或:

db.company.aggregate([
    "$project": {
      "_id": {
        "$toString": "$_id"
    "$lookup": {
      "from": "country",
      "localField": "countryId",
      "foreignField": "_id",
      "as": "country"

使用 $lookuplet 属性

db.company.aggregate([
      "$lookup": {
          "let": { "countryId": { "$toObjectId": "$countryId" } },
          "from": "country",
          "pipeline": [
            { "$match": { "$expr": { "$eq": [ "$_id", "?countryId" ] } } }
          "as": "country"

使用 $addFields

db.role.aggregate([
  { "$addFields": { "countryId": { "$toObjectId": "$countryId" }}},
  { "$lookup": {
    "from": "country",
    "localField": "countryId",
    "foreignField": "_id",
    "as": "country"
  
分类:
阅读
  •