Mongodb 我有两个文档,如何创建查找以获得结果?
以下是类型文档的示例:Mongodb 我有两个文档,如何创建查找以获得结果?,mongodb,Mongodb,以下是类型文档的示例: { id: 't-123' attributes: [ { id: 'a-1', type: 'INPUT' }, { id: 'a-2', type: 'SELECT', option: [ {key: 'DE', label: 'Germany'}, {key:
{
id: 't-123'
attributes: [
{
id: 'a-1',
type: 'INPUT'
},
{
id: 'a-2',
type: 'SELECT',
option: [
{key: 'DE', label: 'Germany'},
{key: 'US', label: 'United States'}
]
}
]
}
{
id: 'f-1',
type: 't-123',
values: [
{attribute: 'a-1', value: 'my random string'},
{attribute: 'a-2', value: 'DE'}
]
}
此外,我还有一个表单集合,它引用类型集合并存储来自用户的所有填充字段
以下是表单文档的示例:
{
id: 't-123'
attributes: [
{
id: 'a-1',
type: 'INPUT'
},
{
id: 'a-2',
type: 'SELECT',
option: [
{key: 'DE', label: 'Germany'},
{key: 'US', label: 'United States'}
]
}
]
}
{
id: 'f-1',
type: 't-123',
values: [
{attribute: 'a-1', value: 'my random string'},
{attribute: 'a-2', value: 'DE'}
]
}
现在,我正在尝试查询表单集合,并自动从类型集合中为类型为SELECT的字段添加choosen键/标签对象
以下是我想要的结果:
{
id: 'f-1',
type: 't-123',
values: [
{attribute: 'a-1', value: 'my random string'},
{attribute: 'a-2', value: {key:'DE', label:'Germany'}}
]
}
到目前为止,您尝试了什么?@RaviShankarBharti db.form.aggregate([{$match:{id:{f-1'},{$match:{type:'t-123'},{$unwind:$values'},{$lookup:{from:'type',localField:'values.value',foreignField:'attributes.id',as:'val'},{$unwind:$val'},}。).pretty()这个查询的结果是什么?没有结果。到目前为止您尝试了什么?@RaviShankarBharti db.form.aggregate([{$match:{id:'f-1'},{$match:{type:'t-123'},{$unwind:$values},{$lookup:{from:'type',localField:'values.value',foreignField:'attributes.id',as:'val'}},{$unwind:$val'},].pretty()这个查询的结果是什么?没有结果。