Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB聚合查询自连接_Mongodb_Aggregation - Fatal编程技术网

MongoDB聚合查询自连接

MongoDB聚合查询自连接,mongodb,aggregation,Mongodb,Aggregation,我有以下数据: [ { "_id": 1, "B": 1, "C": 1 }, { "_id": 2, "B": 1, "C": 1 }, { "_id": 3, "B": 1, "C": 2 }, { "_id": 5, "B": 2, "C": 1 }, { "_id": 6, "B": 2, "C": 1 }, { "_id": 7, "B": 2, "C": 3 }, { "_id": 8, "B": 3, "C": 1 }, { "_id"

我有以下数据:

[
  { "_id": 1,  "B": 1, "C": 1 },
  { "_id": 2,  "B": 1, "C": 1 },
  { "_id": 3,  "B": 1, "C": 2 },
  { "_id": 5,  "B": 2, "C": 1 },
  { "_id": 6,  "B": 2, "C": 1 },
  { "_id": 7,  "B": 2, "C": 3 },
  { "_id": 8,  "B": 3, "C": 1 },
  { "_id": 9,  "B": 3, "C": 1 },
  { "_id": 10, "B": 3, "C": 1 }
]
我想知道的是:

我喜欢得到所有不同的“B”,其中不存在值为2或3的“C”

对于给定数据,结果应仅为3,因为对于其他“B”行,存在“C”值2或3


在SQL中,我将创建一个左外部联接来获得此结果。

下面的查询应该可以

db.getCollection('foo').distinct("B", {"C" : {$nin: [2, 3]}});

我发现此查询返回了正确的结果:

db.mytest.distinct("B", {"B" : {$nin: db.mytest.distinct("B", {"C" : {$in: [2, 3]}})}});

这不是我想要的。您的查询过滤掉带有C2和3的行,然后给我唯一的Bs。