저는 Clinicas 와 Medicos의 두 가지 컬렉션이 있습니다 . Clinicas 는 많은 Medicos를 가질 수 있습니다 . 관계는 다음과 같이 만들어졌습니다.
medico: [{
medicoId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'medicos'
}
}],
그리고 다음과 같은 json 구조를 갖게됩니다.
[
{
"nome": "clinica teste",
"medico": [
{
"_id": "5e011a3796a5f80e3c0c8d20",
"medicoId": {
"_id": "5dc5eef455a8f61698a0f2cd",
"nome": "Hancho Crutis",
}
},
{
"_id": "5e011a3796a5f80e3c0c8d1f",
"medicoId": {
"_id": "5df16e5746783116709f09b7",
"nome": "camilinha",
}
}
],
}
]
내가 원하는 것은 이러한 데이터의 "조인"을 만드는 것입니다. 오랜 연구 끝에이 코드를 받았지만 응답은 항상 비어 있습니다. 누군가 내가 뭘 잘못하고 있는지 말해 줄 수 있습니까?
Clinicas.aggregate([
{ $unwind: "$medico"},
{ $lookup: {
from: "medicos",
localField: "medicoId._id",
foreignField: "_id",
as: 'nome'
}},
{ $match: {"medicoId._id": "5df16e5746783116709f09b7"}},
])
ObjectId에서 변환해야합니다.
var mongoose = require('mongoose');
Clinicas.aggregate([
{ $unwind: "$medico" },
{
$lookup: {
from: "medicos",
localField: "medicoId._id",
foreignField: "_id",
as: 'nome'
}
},
{ $match: { "medicoId._id": mongoose.Types.ObjectId('5df16e5746783116709f09b7') } },
])
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다