是否可以通过$ lookup比较带有ObjectId的字符串 [英] Is it possible to compare string with ObjectId via $lookup

查看:629
本文介绍了是否可以通过$ lookup比较带有ObjectId的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 be object

      如果 localField 键入string,则 foreignField 应该为string

      如果 localField 键入number,则 foreignField 应该为number

      $ lookup文档

      这篇关于是否可以通过$ lookup比较带有ObjectId的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆