mongodb 连接不返回具有ObjectId和非ObjectId的结果

amrnrhlw  于 2023-10-16  发布在  Go
关注(0)|答案(1)|浏览(101)

我有收藏MavenKstag

{
    _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.corpsIDnomenclatureKstag-> _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
      }
   }
])

谢谢你的帮助,我认为问题是连接_idObjectId类型,而expertKstag.expertProfileList.professionalStatus.corpsID不是。

xa9qqrwz

xa9qqrwz1#

您的查询中有几个错误:

  1. expertKstag.expertProfileList.professionalStatus.corpsID字段不存在。您是指expertKstag集合中的expertProfileList.professionalStatus.corpsID
    1.为了比较/匹配值,两个值应该是相同的类型,以便正确匹配。
  2. $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
    }
  }
])

Demo @ Mongo Playground

相关问题