我有expertKstag件藏品

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

和集合nomenclatureKstag

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

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

推荐答案

您的查询中有几个错误:

  1. expertKstag.expertProfileList.professionalStatus.corpsID字段不存在.你是说expertKstag件藏品中的expertProfileList.professionalStatus.corpsID件.

  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
    }
  }
])

Demo @ Mongo Playground

Mongodb相关问答推荐

无法在Ubuntu 22.04上安装MongoDB 7.0

在数组对象 Mongodb 中仅 Select 需要的数组

如何在 mongodb golang 的单个更新调用中使用 $set 和 $inc?

MongoDB 聚合使用 $match 和 $expr 和数组

将子文档中的所有字段设置为 false,然后在单个查询中将第二个字段设置为 true

如何在mongodb中级联删除文档?

创建索引需要很长时间

使用绝对类型在 Typescript 中编写 Mongoose 的类型化模型和模式的类和接口

MongoDB 1.6.5:如何重命名集合中的字段

在 GridFS、express、mongoDB、node.js 中存储来自 POST 请求的数据流

在 Mongoose 中填写所有必填字段

MongoDB 查找精确的数组匹配,但顺序无关紧要

MongoDB中超过2GB的数据库

MongoDB 日志(log)文件和 oplog 有何不同?

如何在 MongoDB Map-reduce 映射函数中使用变量

插入违反唯一索引的 MongoDB 文档时如何捕获错误?

在 MongoDB 上分片 GridFS

MongoDB 按数组元素对文档进行排序

Mongodb同时在多个字段上聚合(计数)

mongoose查询返回 null