【问题标题】:How to use intersect operator in MongoDB?如何在 MongoDB 中使用相交运算符?
【发布时间】:2020-09-18 15:03:47
【问题描述】:
(select A from 'TableB' where C = c and G = g)
intersect
(select A from 'TableB' where C = d and G = h)

首先,由于Mysql没有提供相交运算符,所以我把上面写的查询语句改成​​如下。

select A
from 'TableB'
    where C = c and G = g and A in(
        select A
        from 'TableB'
        where C = d and G = h)

我想使用 MongoDB 来获得与上述相同的结果。

还有其他方法吗??

【问题讨论】:

    标签: sql database mongodb nosql


    【解决方案1】:
    let mongoQuery = {
        $and:[
          {C: c},
          {D: d},
          {G: g},
          {G: h}
        ]
    };
    
    const result = await TableB.find(mongoQuery, {A: 1});
    

    此查询将仅返回“A”中与 C=cD=dG=hG=g 匹配的元素

    希望对你有帮助

    【讨论】:

    • 非常感谢您回答我的问题。但是当我现在检查时,这个问题有点错误。对不起,你能再检查一下这个问题吗?再次感谢。
    猜你喜欢
    • 2016-06-13
    • 2013-12-09
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 2012-03-12
    相关资源
    最近更新 更多