Join 对于 ObjectId 和非 ObjectId 不返回任何结果

问题描述 投票:0回答:1

我有收藏expertKstag

{
    _id: ObjectId('6244213ec4c8aa000104d5ba'),
    userID: '60a65e6142e3320001cc8178',
    uid: 'klavidal',
    firstName: 'Kevin',
    name: 'Lavidal',
    email: '[email protected]',
    expertProfileInProgressList: {},
    expertProfileList: [
        {
            _id: ObjectId('6453abc94e5cd20001596e1c'),
            version: 0,
            language: 'fr',
            isReference: true,
            state: 'PUBLISHED',
            personalDetails: {
            firstName: 'Kevin',
            name: 'Lavidal',
            email: '[email protected]',
                isAbesIDFromLdap: false,
                requiredFieldsLeft: false
            },
            professionalStatus: {
                corpsID: '62442223b8fb982305a5bd67',
                lastUpdateDate: ISODate('2023-05-05T08:36:51.327Z')
            }
    ],
    _class: 'fr.ubordeaux.thehub.expertprofilesservice.model.dao.indexed.ExpertIndexed'
}

和收藏命名法Kstag

{
    _id: ObjectId('62442223b8fb982305a5bd67'),
    type: 'STATUT_CORPS',
    level: 1,
    hasCNU: true,
    labels: [
        {
            language: 'fr',
            text: 'Enseignant-chercheur'
        },
        {
            language: 'en',
            text: 'Teacher-Researcher'
        }
    ],
    isValid: true
}

我想加入 expertKstag ->

expertProfileList.professionalStatus.corpsID
nomenclatureKstag ->
_id

我尝试了,但没有任何返回,为什么?

db.expertKstag.aggregate([
   {
      $lookup:
         {
           from: "nomenclatureKstag",
           localField: "expertKstag.expertProfileList.professionalStatus.corpsID",
           foreignField: "_id",
           as: "joinresultat"
         }
   },
   {
      $unwind: "$join_resultat"
   },
   {
      $project: {
         "_id": 1,
         "userID": 1,
         "uid": 1,
         "firstName": 1,
         "name": 1,
         "email": 1,
         "join_resultat.isValid": 1
      }
   }
])

谢谢您的帮助,我认为问题是连接

_id
是类型
ObjectId
expertKstag.expertProfileList.professionalStatus.corpsID
不是类型。

mongodb mongodb-query aggregation-framework
1个回答
1
投票

您的查询中有一些错误:

  1. expertKstag.expertProfileList.professionalStatus.corpsID
    字段不存在。您指的是
    expertProfileList.professionalStatus.corpsID
    集合中的
    expertKstag

  2. 要比较/匹配值,两个值应具有相同类型才能正确匹配。

  3. $lookup
    阶段之后的结果将在数组中创建一个新的字段名称
    joinresultat
    。但
    $unwind
    阶段指的是这个
    join_resultat
    场,它永远不存在。因此结果输出为空。

您的查询应如下:

db.expertKstag.aggregate([
  {
    $lookup: {
      from: "nomenclatureKstag",
      let: {
        corpsIDs: {
          $map: {
            input: "$expertProfileList.professionalStatus.corpsID",
            in: {
              $toObjectId: "$$this"
            }
          }
        }
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $in: [
                "$_id",
                "$$corpsIDs"
              ]
            }
          }
        }
      ],
      as: "join_resultat"
    }
  },
  {
    $unwind: "$join_resultat"
  },
  {
    $project: {
      "_id": 1,
      "userID": 1,
      "uid": 1,
      "firstName": 1,
      "name": 1,
      "email": 1,
      "join_resultat.isValid": 1
    }
  }
])

演示@Mongo Playground

© www.soinside.com 2019 - 2024. All rights reserved.