如何在mongodb中使用intersect操作符?

bpzcxfmw  于 2021-08-13  发布在  Java
关注(0)|答案(1)|浏览(376)
(select A from 'TableB' where C = c and G = g)
intersect
(select A from 'TableB' where C = d and G = h)

首先,因为mysql不提供intersect操作符,所以我将上面编写的查询语句修改如下。

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

我想使用mongodb得到与上面相同的结果。
还有别的办法吗??

up9lanfz

up9lanfz1#

let mongoQuery = {
    $and:[
      {C: c},
      {D: d},
      {G: g},
      {G: h}
    ]
};

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

此查询将仅返回“a”中匹配的元素 C=c , D=d , G=h , G=g 希望有帮助

相关问题