我在下面的场景中:我有两个集合,medicos和clinicas。医学与临床有关的是其ObjectId。我只想弄到里面有医生的临床医生。所以我有个密码:
Clinicas.aggregate([
$group: {
_id: {
nome: "$nome"
}
}
},
{
$lookup: {
from: "medicos",
localField: "medico.medicoId",
foreignField: "_id._id",
as: 'result'
}
},
但有些地方不对劲:它返回as中的所有医疗人员:“结果”。例如:我在医疗收集中有4个文档,在临床集合中有两个文档。第一个诊所有2个医生,第二个医生有1个,但是as:“结果”数组为我带来了两个临床集合的所有医疗文档,正如你所看到的。
型医疗器械
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const MedicoSchema = new Schema({
//cod_medico:{type:Integer},
nome:{
type: String,
required: true,
maxlength: 80
},
},
);
module.exports = mongoose.model('medicos', MedicoSchema);
在数据库中,集合名也是“medicos”。
发布于 2020-03-06 18:39:55
错误在组中,您首先要更改id,然后使用新id连接数据。尝试更改管道的顺序,或移除组阶段。如下所示:
db.clinica.aggregate([
{
$lookup: {
from: "medicos",
localField: "medico.medicoId",
foreignField: "_id",
as: 'result'
}
},
])
https://stackoverflow.com/questions/60566429
复制相似问题