MongoDB
 SQL >> Base de Dados >  >> NoSQL >> MongoDB

Corrigir $lookup com string

Se a referência está em string e o _id de destino é ObjectId, converta antes de comparar.

{ $lookup: {
  from: "collectionb",
  let: { id: "$b_id" },
  pipeline: [
    { $project: { _id: 1, bid: { $toObjectId: "$$id" } } },
    { $match: { $expr: { $eq: ["$_id", "$bid"] } } }
  ],
  as: "b"
} }