变态的 mongodb 联表查询之 $lookup

1,536 阅读1分钟

起因

user 表 code 字段类型为 String

activity 表 userId 字段为 Double

因 活动表 需要 用户表 的部分数据,所以两表联查

localField: {"$toString": {"$toLong": "$userId"}, 是不支持的

"message" : "$lookup argument 'localField' must be a string, ...

必须是 'localField' string ,what fuxk x 2

解决方案 1 增加 addField

{$addFields: {uid: {"$toString": {"$toLong": "$userId"}}}}

图解:

遗留

虽然两表联查数据已经都包含再结果中了,

但并未拉平,其中新增临时的uid,及user数组对象 预知后事如何 变态的 mongodb 联表查询之 数据扁平化

新增的数据