是否可以通过$ lookup比较带有ObjectId的字符串 [英] Is it possible to compare string with ObjectId via $lookup
问题描述
table1
的字段字符串为"value",而table2
的字段值为"value",为ObjectId
,是否可以执行这样的查询或如何编写
table1
has a field string "value" and table2
has a field "value" as ObjectId
, Is it possible to do a query like this or how to write
table1.aggregate([
{
$lookup: {
from: "table2",
localField: "value",
foreignField: "_id",
as: "test"
}
}
])
推荐答案
据我所知,在 MongoDB 数据类型中使用$lookup
运算符加入集合应该是相同的.如果类型不匹配,则$lookup
将不起作用.因此,要加入,您应该使用相同类型的那些字段,因为它会检查平等.
As far I know to join collections using $lookup
operator in MongoDB data type should be same. If type mismatch then $lookup
will not work. So to join you should use those field that are same type because it check equality.
$ 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 键入
object
,则 foreignField 应为object
If localField type
object
then foreignField should beobject
如果 localField 键入
string
,则 foreignField 应该为string
如果 localField 键入
number
,则 foreignField 应该为number
这篇关于是否可以通过$ lookup比较带有ObjectId的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!