数组中未列出项的MongoDB聚合查找

数组中未列出项的MongoDB聚合查找,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我将以下内容用于总体,但不确定如何使用总体的否定 db.getCollection("storeCheckRecords").aggregate([ { $group: { _id: null, store_ids: { $addToSet: "$store_id" }, }, }, // { // $lookup: { // from: "allStores&q

我将以下内容用于总体,但不确定如何使用总体的否定

db.getCollection("storeCheckRecords").aggregate([
  {
    $group: {
      _id: null,
      store_ids: { $addToSet: "$store_id" },
    },
  },
  //   {
  //     $lookup: {
  //       from: "allStores",
  //       localField: "store_ids", // I want to do NOT IN store_ids
  //       foreignField: "_id",
  //     },
  //   },
]);
上面的管道相当简单,我们首先获得所有唯一的
$store\u id
,然后我们希望列出集合
allStores
中的所有存储,这样它就不会出现在
$stores
数组中

我的尝试:我知道如何填充字段
store\u id
中存在的存储,但我不知道如何获得否定


期待您的宝贵意见!谢谢。

您可以使用的以下语法获得结果,并且可以在其中使用
$not
$in

db.getCollection("storeCheckRecords").aggregate([
  { "$lookup":  {
    "from": "allStores",
    "let": { "store_ids": "$store_ids" },
    "pipeline": [
      { "$match": { "$expr": { "$not": { "$in": [ "$_id", "$$store_ids" ] }}}},
    ],
    "as": "allStores"
  }}
])