skip to Main Content

I cannot figure to multiple fields by using $addFields for if else condition in MongoDB with Java operator. My requirement is to add Mutiple fields based of if else condition. I know how to add multiple fields bu using $addFields and $project but not with if else condition.

Can anyone help me how to implement in Java with MongoDB operators?

Appreciate any help. Thanks in advance.

$addFields syntax:

.append(system, new Document()
    .append("$cond", new Document()
        .append("if", new Document()
                .append("$eq", Arrays.asList(
                                "$system",
                                "AA"
                        )
                )
        )
        .append("then",  new Document()
                .append("value.amount", new Document()
                        .append("$multiply", Arrays.asList(
                                        "$value.amount",
                                        Decimal128.parse("-1.0")
                                )
                        )
                )
                .append("value2", new Document()
                        .append("$multiply", Arrays.asList(
                                        "$value.amount",
                                        Decimal128.parse("-1.0")
                                )
                        )
                )
                
        )

$project :

new Document()
    .append("$project", new Document()
        .append("_id", 0.0)
        .append("value.amount", 0.0)
        .append(sourceSystem, new Document(
                "$cond", new Document(
                "if", new Document("$eq", "true") )
        )
                .append("then", new Document()
                        .append("original.amount", 0.0)
                        .append("amount.amount", 0.0)
                        

        )

pseudo-code:

"$addFields" : { 
                    'system.amount':'$_id',
                    "system.amount1" : Period, 
 {"system": "AA",
                    "amount" : { 
                        "$multiply" : [
                            "$amount", 
                            -1.0
                        ]
                    }, 
                    "amount1" : { 
                        "$multiply" : [
                            "$.amount1", 
                            -1.0
                        ]
                    }, 
                    "amount2" : { 
                        "$multiply" : [
                            "$amount2", 
                            -1.0
                        ]
                    }, 
                    }
                    {"system": "BB",
                    "amount3" : { 
                        "$multiply" : [
                            "$amount3", 
                            -1.0
                        ]
                    }, 
                   
                    
                }
            }, 

2

Answers


  1. Chosen as BEST ANSWER
                         new Document()
                            .append("$addFields", new Document()
                                    .append("RecordID", "$_id")
                                    .append("Period", samplePeriod)
                                     .append("Reserve", "$ClosingAmt")
                                    .append("Identifier", new Document()
                                            .append("$concat", Arrays.asList(
                                                            "$key",
                                                            "  Balance"
                                                    )
                                            )
                                    )
                                    .append("Reserve", true)
    
                            ),
                    //
                    new Document()
                            .append("$project", new Document()
                                    .append("tmp", new Document()
                                            .append("$cond", Arrays.asList(
                                                            new Document()
                                                                    .append("$eq", Arrays.asList(
                                                                                    "$system",
                                                                                    "AA"
                                                                            )
                                                                    ),
    
                                                                    Arrays.asList(
                                                                            "amount",
                                                                            new Document()
                                                                                    .append("$multiply", Arrays.asList(
                                                                                                    "$amount",
                                                                                                    -1.0
                                                                                            )
                                                                                    )
                                                                    ),
                                                                    Arrays.asList(
                                                                            "amount1",
                                                                            new Document()
                                                                                    .append("$multiply", Arrays.asList(
                                                                                                    "$amount1",
                                                                                                    -1.0
                                                                                            )
                                                                                    )
                                                                    ),Arrays.asList(
                                                                            "amount2",
                                                                            new Document()
                                                                                    .append("$multiply", Arrays.asList(
                                                                                                    "$amount2",
                                                                                                    -1.0
                                                                                            )
                                                                                    )
                                                                    ),
                                                                    Arrays.asList(
                                                                            "amount3",
                                                                            new Document()
                                                                                    .append("$multiply", Arrays.asList(
                                                                                                    "$amount3",
                                                                                                    -1.0
                                                                                            )
                                                                                    )
                                                                    ),
    
    
    
    
                                                    )
                                            )
                                    )
                            ),
                    new Document()
                            .append("$addFields", new Document()
                                    .append("tmp2", new Document()
                                            .append("$arrayToObject", "$tmp")
                                    )
                            ),
                    new Document()
                            .append("$replaceWith", "$tmp2"),
    
      
    

  2. We can use $arrayToObject operator.

    Main idea: We create an array structure of fields we need (operator format), convert it to an object, and reshape the final result.

    If you comment pipeline steps (from backwards), you can observe how we reshape documents.

    db.collection.aggregate([
      {
        "$project": {
          "tmp": {
            $cond: [
              {$eq: ["$system","AA"]},
              [
                [ "amount", { "$multiply": ["$value.amount",2.0]}],
                [ "field1", "$value.field1"],
                [ "field2", "$value.field2"],
                [ "field3", "$value.field3"]
              ],
              [
                [ "amount", { "$multiply": ["$amount.amount",-2.0]}],
                [ "field1", "$amount.field3"],
                [ "field2", "$amount.field2"],
                [ "field3", "$amount.field1"]
              ]
            ]
          }
        }
      },
      {
        $addFields: { tmp2: {"$arrayToObject": "$tmp"}}
      },
      {
        "$replaceWith": "$tmp2"
      }
    ])
    

    MongoPlayground | All in 1 step

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search