聚合$cond if条件在Java中的实现

聚合$cond if条件在Java中的实现,java,mongodb,aggregation-framework,Java,Mongodb,Aggregation Framework,我需要在java中实现“if条件”,但我不知道如何实现,所以请给出一些建议 db.test.aggregate( [ { $project: { data: 1, result: { $cond: { if: { $cond: [ "$salary", 2500

我需要在java中实现“if条件”,但我不知道如何实现,所以请给出一些建议

db.test.aggregate(
       [
          {
             $project:
               {
                 data: 1,
                 result:
                   {
                     $cond: { if: { $cond: [ "$salary", 250000 ] }, then: 30, else: 20 }
                   }
               }
          }
       ]
    )
公正和清单

或交替使用以下列表形式:

这两种方法都是有效的

注意问题中的错误,您可能指或您可能指或条件中的其他逻辑运算符。期望返回一个布尔值

取决于您可能会与之交换的驱动程序

List<Document> pipeline = Arrays.asList(
  new Document("$project",
    new Document("data1", 1)
      .append("result", new Document(
        "$cond", new Document(
          "if", new Document("$eq", Arrays.asList("$salary", 250000) )
        )
        .append("then", 30)
        .append("else", 20) 
      ))
  )
);
[
  { "$project": {
    "data1": 1,
    "result": {
      "$cond": {
        "if": { "$eq": [ "$salary", 250000 ] },
        "then": 30,
        "else": 20
      }
    }
  }}
]
List<Document> pipeline = Arrays.asList(
  new Document("$project",
    new Document("data1", 1)
      .append("result", new Document(
        "$cond", Arrays.asList(
          new Document("$eq", Arrays.asList("$salary", 250000) ),
          30,
          20
        )
      ))
  )
);
[
  { "$project": {
    "data1": 1,
    "result": {
      "$cond": [
        { "$eq": [ "$salary", 250000 ] },
        30,
        20
      }
    }
  }}
]