记录如何在unicloud的聚合操作把联表查询出的数据第一线作为数据覆盖原始字段
##代码
let new_list = (await db_xudd
.aggregate()
.match({
_id: _.nin(gots),
user_id: store.userInfo._id
})
.lookup({
from: 'x_device_type_data',
let: {
type_id: '$device_type_id' // 把device_type_id作为type_id供下面使用
},
pipeline: $.pipeline()
.match(_.expr($.eq(['$type_id', '$$type_id'])))
.done(),
as: 'type_data'
})
.project({
device_id: true,
is_user_owner: true,
remark_name: true,
type_data: $.arrayElemAt(['$type_data', 0]) // 把联表获得的数组第一项的值覆盖到type_data字段,使得原本返回的素组变成了联表查询出的对象
})
.end()).result.data;