你如何否定 mongodb 中的 $match 聚合

Pas*_*lly 6 mongodb aggregation-framework

我有以下查询需要找到相反的查询。

db.contracts.aggregate([ 
    { $match: { currentBalance: { $gt: 0 }, status: 'open' } }, 
    { $project: { customer_id: 1, lastTransactionDate: 1, currentBalance: 1, status: 1 } }
])
Run Code Online (Sandbox Code Playgroud)

我试图不使用

$or: [ currentBalance: { $ne: 0 }, status: { $ne: 'open' } ]
Run Code Online (Sandbox Code Playgroud)

我真正想要的是使用,$not: [ condition ]因为我将有更难编写的聚合。但是我找不到正确的语法。任何帮助表示赞赏。我正在使用 mongodb 3.0.7

小智 10

我认为这基本上就是你的意思(如果不是,请道歉)

db.contracts.aggregate([
  {
    $match: {
      currentBalance: { $not: { $gt: 0 } },
      status: { $not: { $eq: 'open' } },
    },
  },

  {
    $project: {
      customer_id: 1,
      lastTransactionDate: 1,
      currentBalance: 1,
      status: 1
    }
  }
])
Run Code Online (Sandbox Code Playgroud)