table1有一个字段字符串“value”,table2有一个字段“value”作为ObjectId,有可能做这样的查询或如何写
table1.aggregate([
{
$lookup: {
from: "table2",
localField: "value",
foreignField: "_id",
as: "test"
}
}
])
最佳答案 据我所知,在MongoDB数据类型中使用$lookup运算符加入集合应该是相同的.如果类型不匹配,那么$lookup将不起作用.所以要加入你应该使用那些相同类型的字段,因为它检查相等性.
The $lookup stage does an equality match between a field from the
input documents with a field from the documents of the “joined”
collection
>如果是localField类型对象,那么foreignField应该是对象
>如果是localField类型字符串,那么foreignField应该是字符串
>如果是localField类型编号,则foreignField应为number