查询与object匹配的订阅文档

问题描述 投票:0回答:1

我在数据库中有订阅集合,例如:

{
  _id: new ObjectId(...),
  query: {
    size: { $in: [1, 2, 3] }
  }
}

我需要找到与某个对象匹配的所有订阅。是否可以?例如:

Subscriptions.aggregate([{
  $reverseMatch: {
    size: 1,
  },
}]);
mongodb aggregation-framework
1个回答
0
投票
Subscriptions.aggregate([
  {
    $match: {
      $nor: [
        { "query.size": 1 }, // Exclude documents where size is 1
        { "query.size": 2 }, // Exclude documents where size is 2
        { "query.size": 3 }  // Exclude documents where size is 3
      ]
    }
  }
]);

此查询有效地过滤掉

size
对象内的
query
字段与任何指定值(1、2 或 3)匹配的订阅,从而为我们提供与指定对象不匹配的订阅。这是处理 MongoDB 聚合管道中此类过滤需求的巧妙方法。

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