繁体   English   中英

Nodejs localField $lookup 作为_ids数组

[英]Nodejs localField $lookup as Array of _ids

我需要加入两个文档,其中第一个文档有一个属性作为_ids 数组,第二个文档要加入

db={
"tipo_pratica": [
{
  "_id": "618981a4c1b8b3bc67ff80b6",
  "descrizione": "anticipata",
  "modulo": [
    "628015cd2fd9dfee86ac6820",
    "62801a4c2fd9dfee86ac6821",
    "6278f8d9d4aa4f4cef1a8266"
  ]},
{
  "_id": "628238d6f97b57efcb1fc504",
  "descrizione": "Supporto",
  "modulo": [
    "6278f8d9d4aa4f4cef1a8266",
    "628015cd2fd9dfee86ac6820",
    "62801a4c2fd9dfee86ac6821"
  ]}
]};


db={
"moduli": [
{
  "_id": "6278f8d9d4aa4f4cef1a8266",
  "tipo": "Incentivi auto",
  "documento": [
    "1652095190015_la_scuola_2021_copia.pdf"
  ],
  "contenuto": "<p>Inserire il documento allegato</p>"
},
{
  "_id": "628015cd2fd9dfee86ac6820",
  "tipo": "Mandato di assistenza e rappresentanza",
  "documento": [
    "1652561335432_Mandato_di_rappresentanza_privacy_0.pdf"
  ],
  "contenuto": "<p>no</p>"
},
{
  "_id": "62801a4c2fd9dfee86ac6821",
  "tipo": "Modello red da far... ",
  "documento": [
    "1652562502599_Modello_REX.pdf"
  ],
  "contenuto": null
}]
};

如文档所述: 将 $lookup 与数组一起使用

我试过了:

const doc = await collection.aggregate([
 {
  $lookup: {
  from: "moduli",
  localField: "modulo",
  foreignField: "_id",
  as: "moduls"
   }
  }
  ])

没有成功

所以我在mongoplayground上测试了脚本

它似乎运作良好。 我认为问题出在 ID 数组中,我也尝试了很多选项,我经常在网上阅读文档和搜索,但是很多结果是特定于猫鼬驱动器的,而我使用的是本机驱动器。

我想要与游乐场示例相同的回报。

因此,任何帮助在很大程度上都会受到赞赏。

在我在节点中用于拨打电话的片段下方

app.post('/admin/moduli/join/', (req, res, error) => {

 async function run() {

 try {

      await client.connect();
      var ObjectId = require('mongodb').ObjectId;
      const db = client.db("admin");
      const collection = db.collection("tipo_pratica");

    // replace IDs array with lookup results passed to pipeline
     const doc = await collection.aggregate([
          {
           $lookup: {
               from: "moduli",
               localField: "modulo",
               foreignField: "_id",
            pipeline: [
               { $match: { $expr: {$in: ["$_id", "$$modulo"] } } },
              { $project: {_id: 0} } // suppress _id
              ],
              as: "productObjects"
                 }
             }
           ]);

// 文档不起作用!

          // Unwind
          const doc2 = await collection.aggregate([
            // Unwind the source
            { "$unwind": "$modulo" },
           // Do the lookup matching
            { "$lookup": {
               "from": "moduli",
               "localField": "modulo",
               "foreignField": "_id",
               "as": "productObjects"
             }
           }

// doc2 不工作!

        const doc3 = await collection.aggregate([
      {
       $facet: {
        moduli: [
       {
       $lookup: {
        from: "moduli",
        localField: "modulo",
        foreignField: "_id", // also tried ObjectId()
        as: "plugin"
      }
    },
    {
      $match: {
        plugin: {
          $not: {
            $size: 0
             }
           }
         }
       }
     ]
   }
  },
{
$project: {
  tipoPratiche: {
    "$concatArrays": [
      "$moduli"
       ]
     }
   }
 },
 {
   $unwind: "$tipoPratiche"
 },

]).toArray();

// doc3 不工作!

     res.send(doc4);


} finally {
  await client.close();
  }

 }

   run().catch(console.dir);

 });

提前感谢您的时间

一种方法是在$lookup之前使用$map

db.tipo_pratica.aggregate([
  {
    $set: {
      modulo: {
        $map: {
          input: "$modulo",
          as: "item",
          in: {$toObjectId: "$$item"}
        }
      }
    }
  },
  {
    $lookup: {
      from: "moduli",
      localField: "modulo",
      foreignField: "_id",
      as: "moduls"
    }
  }
])

操场

另一种选择是使用$lookup管道并将字符串转换为其中的ObjectId

问题在于格式文本。 所以喜欢这个解决方案

const doc1 = await db.collection("tipo_pratica").aggregate([
{
'$set': {
'modulo': {
'$map': {
'input': '$modulo',
'as': 'item',
'in': {
'$toObjectId': '$$item'
}
}
}
}
}, {
'$lookup': {
'from': 'moduli',
'localField': 'modulo',
'foreignField': '_id',
'as': 'moduls'
}
}
]).toArray();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM