MongoDb LEFT Join返回的数据与MySQl类似,Left Left JOIN

时间:2019-04-23 09:17:02

标签: javascript mongodb collections mongoose-schema

我有两个MongoDB Tabel

应用列表

{
  "_id": {
    "$oid": "5cbac9172d962331401d0e48"
  },
  "appInstallCount": {
    "$numberInt": "6"
  },
  "status": {
    "$numberInt": "1"
  },
  "appName": "PC Access Log",
  "appInfo": "You can view users who access your PC",
  "appImageUrl": "assets/images/app/userlog-app-thismypc.png",
  "userID": "5c3d6a25227a1d01eba0afc9",
  "version": "1",
  "released_date": {
    "$date": {
      "$numberLong": "1555745047247"
    }
  },
  "lastUpdate_date": {
    "$date": {
      "$numberLong": "1555745047247"
    }
  },
  "__v": {
    "$numberInt": "0"
  }
}{
  "_id": {
    "$oid": "5cbd300316e4651c78167478"
  },
  "appInstallCount": {
    "$numberInt": "0"
  },
  "status": {
    "$numberInt": "1"
  },
  "appName": "PC Access Log TEST",
  "appInfo": "You can view users who access your PC",
  "appImageUrl": "assets/images/app/userlog-app-thismypc.png",
  "userID": "5c3d6a25227a1d01eba0afc9",
  "version": "1",
  "released_date": {
    "$date": {
      "$numberLong": "1555745047247"
    }
  },
  "lastUpdate_date": {
    "$date": {
      "$numberLong": "1555745047247"
    }
  },
  "__v": {
    "$numberInt": "0"
  }
}

UserAndApps Tabel

{
  "_id": {
    "$oid": "5cbd327294877a70b510c8b6"
  },
  "status": {
    "$numberInt": "1"
  },
  "userID": {
    "$oid": "5cbd2d3e0afcb16efa795563"
  },
  "appID": {
    "$oid": "5cbac9172d962331401d0e48"
  },
  "date": {
    "$date": {
      "$numberLong": "1555903090765"
    }
  },
  "__v": {
    "$numberInt": "0"
  }
}{
  "_id": {
    "$oid": "5cbd327394877a70b510c8b7"
  },
  "status": {
    "$numberInt": "1"
  },
  "userID": {
    "$oid": "5cbd2d3e0afcb16efa795563"
  },
  "appID": {
    "$oid": "5cbd300316e4651c78167478"
  },
  "date": {
    "$date": {
      "$numberLong": "1555903091265"
    }
  },
  "__v": {
    "$numberInt": "0"
  }
}{
  "_id": {
    "$oid": "5cbd329a94877a70b510c8b9"
  },
  "status": {
    "$numberInt": "1"
  },
  "userID": {
    "$oid": "5c3d6a25227a1d01eba0afc9"
  },
  "appID": {
    "$oid": "5cbac9172d962331401d0e48"
  },
  "date": {
    "$date": {
      "$numberLong": "1555903130918"
    }
  },
  "__v": {
    "$numberInt": "0"
  }
}{
  "_id": {
    "$oid": "5cbd329b94877a70b510c8ba"
  },
  "status": {
    "$numberInt": "1"
  },
  "userID": {
    "$oid": "5c3d6a25227a1d01eba0afc9"
  },
  "appID": {
    "$oid": "5cbd300316e4651c78167478"
  },
  "date": {
    "$date": {
      "$numberLong": "1555903131796"
    }
  },
  "__v": {
    "$numberInt": "0"
  }
}

我使用此代码将以上两个表格合并在一起

[
  {
    '$lookup': {
      'from': 'userandapps', 
      'localField': '_id', 
      'foreignField': 'appID', 
      'as': 'appData'
    }
  }, {
    '$unwind': {
      'path': '$appData', 
      'preserveNullAndEmptyArrays': true
    }
  }, {
    '$match': {
      'appData.userID': new ObjectId('5cbd2d3e0afcb16efa795563')
    }
  }
]

enter image description here

问题是它只返回填充 appData 数组,但是我需要那个 appData 数组来填充和清空两个阶段,就像在MYSQL LEFT JOIN中一样。如果'appData.userID': new ObjectId('5cbd2d3e0afcb16efa795563')不属于文档之一。它还返回一个空的appData数组。我尝试了许多方法,但没有任何方法可以解决此问题。有人可以帮我弄这个吗?大帮助。

我尝试使用此代码,它返回空没有任何数据

    [{
    $lookup: {
        from: 'userandapps',
        localField: '_id',
        foreignField: 'appID',
        as: 'appData'
    }
}, {
    $unwind: {
        path: "$appData",
        preserveNullAndEmptyArrays: true

    }
}, {
    $match: {
        "$or": [{
            'appData.userID': ObjectId('5cbd2d3e0afcb16efa795563')
        }, {
            'appData': null
        }]
    }
}]

0 个答案:

没有答案