如何在MongoDB中使用相交运算符?

问题描述 投票:0回答:1
(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个回答
0
投票
let mongoQuery = {
    $and:[
      {C: c},
      {D: d},
      {G: g},
      {G: h}
    ]
};

const result = await TableB.find(mongoQuery, {A: 1});

此查询将仅返回'A'中与C=cD=d相匹配的元素

希望有帮助

© www.soinside.com 2019 - 2024. All rights reserved.