0

I am facing a problem in running mogodb aggregate lookup function on object id of document i have two document from different tables Department Table document

{
"_id" : ObjectId("60b5d01d3cad3268ec30a4db"),
"designationId" : [
ObjectId("60b5d6483cad3268ec30a513")
],
"creationTime" : 1622525747967.0,
"isActive" : true,
"name" : "CT Dept One",
"organizationId" : ObjectId("60b5cf7d3cad3268ec30a4c7"),
"brandUserId" : ObjectId("60ae11608646230e9cea8ee9"),
"createdby" : ObjectId("60ae11608646230e9cea8ee9"),
"createdAt" : ISODate("2021-06-01T06:13:49.639Z"),
"updatedAt" : ISODate("2021-06-01T06:40:08.453Z"),
"v" : 1
}

Organization Table

{
"_id" : ObjectId("60b5cf7d3cad3268ec30a4c7"),
"creationTime" : 1622525747965.0,
"isActive" : true,
"name" : "CT PVT LTD",
"brandUserId" : ObjectId("60ae11608646230e9cea8ee9"),
"createdby" : ObjectId("60ae11608646230e9cea8ee9"),
"createdAt" : ISODate("2021-06-01T06:11:09.783Z"),
"updatedAt" : ISODate("2021-06-01T06:11:09.783Z"),
"v" : 0
}

I have tried every solution return mention here Mongodb Join on _id field from String to ObjectId to get data from Organization table in to department table. please help me to solve this problem.

i have tried this solution

db.department.aggregate([
        { 
            "$match": { 
                "organizationId": organizationId,
                "isActive": { $eq: true }
            } 
        },
        {
            "$project": {
              "organizationId": {
                "$toObjectId": "$organizationId"
              }
            }
        },
        {
            "$lookup": {
              "from": "organization",
              "localField": "organizationId",
              "foreignField": "_id",
              "as": "organizationData"
            }
        }
    ])

and also perform this one which will work perfectly in mongo playground but not working when i am running it from postman api

db.department.aggregate([
  {
    "$lookup": {
      "from": "organization",
      "localField": "organizationId",
      "foreignField": "_id",
      "as": "role"
    }
  }
])

0 Answers0