Find usando _id não está funcionando com agregação [duplicate]
Esta pergunta já tem uma resposta aqui:
Moongoose agregado $ match não corresponde ao ID 1 respostaEu sou iniciante no mongodb e estou tentando escrever uma consulta usando o mongoose no aplicativo node.js:
const objectIdValue = secondId.valueOf();
const existedRelation = await this.model.aggregate([
{ $match: { _id: firstId } },
{ $project: {
relations: {
$filter: {
input: '$links',
as: 'link',
cond: {
$and: [
{ $eq: ['$link.target.entityId', `${objectIdValue}`] },
{ $eq: ['$link.linkTypeId', linkTypeId] },
],
},
},
},
},
},
]);
console.log('existedRelation: ', existedRelation);
Quando executo, obtive este resultado:
existedRelation: []
Tentei executá-lo usando o mongoShell:
db.tasks.aggregate([
{ $match:{ _id: ObjectId("5bbf5800be37394f38a9727e") }},
{
$project: {
relations:{
$filter:{
input: '$links',
as: "link",
cond: {
$and: [
{$eq:["$link.target.entityId", '5bbf52eabe37394f38a97276']},
{$eq: ["$link.linkTypeId", ObjectId("5bbf4bfcb075e03bd4a1b779")]}
]
}
}
}
}
}
E eu tenho os resultados que eu quero. Qual foi o erro que cometi?