我有收藏MavenKstag
{
_id: ObjectId('6244213ec4c8aa000104d5ba'),
userID: '60a65e6142e3320001cc8178',
uid: 'klavidal',
firstName: 'Kevin',
name: 'Lavidal',
email: '[email protected]',
expertProfileInProgressList: {},
expertProfileList: [
{
_id: ObjectId('6453abc94e5cd20001596e1c'),
version: 0,
language: 'fr',
isReference: true,
state: 'PUBLISHED',
personalDetails: {
firstName: 'Kevin',
name: 'Lavidal',
email: '[email protected]',
isAbesIDFromLdap: false,
requiredFieldsLeft: false
},
professionalStatus: {
corpsID: '62442223b8fb982305a5bd67',
lastUpdateDate: ISODate('2023-05-05T08:36:51.327Z')
}
],
_class: 'fr.ubordeaux.thehub.expertprofilesservice.model.dao.indexed.ExpertIndexed'
}
命名Kstag
{
_id: ObjectId('62442223b8fb982305a5bd67'),
type: 'STATUT_CORPS',
level: 1,
hasCNU: true,
labels: [
{
language: 'fr',
text: 'Enseignant-chercheur'
},
{
language: 'en',
text: 'Teacher-Researcher'
}
],
isValid: true
}
我想加入expertKstag-> expertProfileList.professionalStatus.corpsID
和nomenclatureKstag-> _id
我试过了,但没有任何回应,为什么?
db.expertKstag.aggregate([
{
$lookup:
{
from: "nomenclatureKstag",
localField: "expertKstag.expertProfileList.professionalStatus.corpsID",
foreignField: "_id",
as: "joinresultat"
}
},
{
$unwind: "$join_resultat"
},
{
$project: {
"_id": 1,
"userID": 1,
"uid": 1,
"firstName": 1,
"name": 1,
"email": 1,
"join_resultat.isValid": 1
}
}
])
谢谢你的帮助,我认为问题是连接_id
是ObjectId
类型,而expertKstag.expertProfileList.professionalStatus.corpsID
不是。
1条答案
按热度按时间xa9qqrwz1#
您的查询中有几个错误:
expertKstag.expertProfileList.professionalStatus.corpsID
字段不存在。您是指expertKstag
集合中的expertProfileList.professionalStatus.corpsID
。1.为了比较/匹配值,两个值应该是相同的类型,以便正确匹配。
$lookup
阶段之后的结果将在数组中创建一个新的字段名称joinresultat
。但是$unwind
阶段引用了这个join_resultat
字段,它从来不存在。因此,结果输出为空。您的查询应如下所示:
Demo @ Mongo Playground