【问题标题】:MongoDB $lookup in an array of object with different valueMongoDB $查找具有不同值的对象数组
【发布时间】:2020-02-11 22:46:36
【问题描述】:

我有两个 MongoDB 集合,我想用图像对象替换所有 ID。

数据集集合:

{ 
  _id: 5d888b3a29cf9a5e93d80756,
  date: 2019-09-23T11:33:00.000Z,
  robot: { _id: 5d9c7caf0ad3ea493210527b, name: 'erx01' },
  images:[ 
    { 
      rgb: 5da57db7cacd6e00c2a0f677,
      ir: 5da57db7cacd6e00c2a0f678,
      lbl: 5da57db7cacd6e00c2a0f676 
    },
    { 
      rgb: 5da5808f65f3440032edee78,
      ir: 5da5808f65f3440032edee79,
      lbl: 5da5808f65f3440032edee77 
    } 
  ] 
}

图片集:

{ 
  _id: 5da57db7cacd6e00c2a0f677,
  filename: 'iI7gVXyf31b1wedzBXD.png',
  metadata: [Object],
  type: 'RGB' 
}

这就是我尝试并得到的结果:

{
  $unwind: "$images"
},
{
  $lookup: {
    from: "image",
    localField: "images.rgb",
    foreignField: "_id",
    as: "images.rgb"
  }
},
{
  $lookup: {
    from: "image",
    localField: "images.lbl",
    foreignField: "_id",
    as: "images.lbl"
  }
},
{
  $lookup: {
    from: "image",
    localField: "images.ir",
    foreignField: "_id",
    as: "images.ir"
  }
},

结果:

images: { 
         rgb: [ [Object] ], 
         ir: [ [Object] ], 
         lbl: [ [Object] ] 
        }

我想要什么:

images : [
           {
             rgb: [Object],
             ir:  [Object] , 
             lbl: [Object] 
           }
           { ... }
         ]

它工作了一半,因为我得到了正确的信息,但不是一个数组。我不想要一组 RGB/IR 和 LBL 图像,而是一组包含一个 RGB/IR/LBL 图像的对象。

我也尝试使用 unwind,但没有任何相关信息。

【问题讨论】:

    标签: node.js mongodb aggregation-framework mongodb-lookup


    【解决方案1】:

    你做了一半正确的事情,你只需要添加组和项目来将输出更改为你想要的格式

    {
        $unwind: "$images"
      },
      {
        $lookup: {
          from: "image",
          localField: "images.rgb",
          foreignField: "_id",
          as: "images.rgb"
        }
      },
      {
        $unwind: {
            "path": "$images.rgb",
            "preserveNullAndEmptyArrays": true
         }
      },
      {
        $lookup: {
          from: "image",
          localField: "images.lbl",
          foreignField: "_id",
          as: "images.lbl"
        }
      },
      {
       $unwind: {
            "path": "$images.lbl",
            "preserveNullAndEmptyArrays": true
         }
      },
      {
        $lookup: {
          from: "image",
          localField: "images.ir",
          foreignField: "_id",
          as: "images.ir"
        }
      },
      {
        $unwind: {
            "path": "$images.ir",
            "preserveNullAndEmptyArrays": true
         }
      },
      {
        $group: {
          _id: {
            id: '$_id',
          },
          date: { $last: '$date' },
          robot: { $last: '$robot' },
          images: { $push: '$images' }
        }
      },
      {
        $project: {
          _id: '$_id.id',
          date: 1,
          robot: 1,
          images: 1
        }
      }
    

    在每次查找后注意 $unwind ,以便查找的输出不在数组中。

    【讨论】:

    • 天哪,你是我的英雄,它有效! ,不知道 $project 做什么? id 使我成为一个我并不真正需要的附加 ID。删除它并正常工作!
    • $project 用于根据需要更改对象结构并选择键。有些人需要它,有些人不是你的选择。
    • 我不知道我在这里能不能做到。如果我的 3 张图像(rgb/ir/lbl)中的一张为空,它不会返回任何东西。我该如何解决这个问题?
    • 在每次展开时添加 "preserveNullAndEmptyArrays": true 以保持该值,即使它为空。例如 { $unwind: "$images.rgb", "preserveNullAndEmptyArrays": true, },
    猜你喜欢
    • 2019-11-14
    • 1970-01-01
    • 2021-05-29
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 2020-03-27
    相关资源
    最近更新 更多