起因
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"}}}}
图解:
- 1为新增uid字段,本例中需要 localField 一致
- 2为要联查的表,本例中为 users
- 3为2中user表的字段,本例中需要与1中的 localField 字段类型及内容一致 详解请移步至 变态的 mongodb 联表查询之 double to string
- 4为新增obj数组对象