0

I have the following database:

  { stream :{ "name": "name1",
       "error1": 1,
       "error2": 1,
       "error3": 1 }
  }

,

{ stream : {"name": "name1",
       "error1": 2,
       "error2": 1,
       "error3": 1 }
  }

,
{ stream : {"name": "name2",
       "error1": 1,
       "error2": 1,
       "error3": 1 }
  }

I would like to group it by name and sum every time some different combination of errors.

this is what I did in mongo, I need to create the following query dynamically in java

 db.collection.aggregate([{$group: {_id: "$stream.name",error1: {$sum:"$stream.error1"   },error2: {$sum: "$stream.error2" }}  ]) 

the thing is that every time I need different combinations of the errors:error1 with error2, only error 1 etc..

this is what I did: (the arguments in the "if" are some boolean values that I am getting)

  List<String>  totalError = new ArrayList<String>();   
  BasicDBObject group = new BasicDBObject( "$group", new BasicDBObject("_id","$stream.name" ));


    if (error1)
    {

        group.append("error1",new BasicDBObject ("$sum", "$stream.error1"  ));

    }

    if (error2)
    {
        group.append("error2",new BasicDBObject ("$sum", "$stream.error2"  ));

    }

    if (error3)
    {
        group.append("error3",new BasicDBObject ("$sum", "$stream.error3"  ));

    }

the problem is that I am getting:

    { "$group" : { "_id" : "$stream.name"} , "error1" : { "$sum: "$stream.error1"} , "error2" : { "$sum" : "$stream.error2"}

},

instead of:

      { "$group" : { "_id" : "$stream.name", "error1" : { "$sum: "$stream.error1"} , "error2" : { "$sum" : "$stream.error2"}}

if I knew what error combination I need I could use append in the constructor of group dbobject.. but I don't know the combination and I need to use the "ifs"

1
  • please look at it now.. I checked my query in mongo Commented Jan 31, 2018 at 12:44

1 Answer 1

1

Try

BasicDBObject fields = new BasicDBObject("_id","$stream.name" );
 if (error1)
    fields.append("error1",new BasicDBObject ("$sum","$stream.error1"));
 if (error2)
    fields.append("error2",new BasicDBObject ("$sum","$stream.error2"));
 if (error3)
    fields.append("error3",new BasicDBObject ("$sum","$stream.error3"));
BasicDBObject group = new BasicDBObject( "$group", fields);

You should use helper functions when possible.

List<BsonField> fieldAccumulators = new ArrayList<>();
if (error1)
    fieldAccumulators.add(Accumulators.sum("error1","$stream.error1"));
if (error2)
    fieldAccumulators.add(Accumulators.sum("error2","$stream.error2"));
if (error3)
   fieldAccumulators.add(Accumulators.sum("error3","$stream.error3"));
collection.aggregate(Arrays.asList(Aggregates.group("$stream.name", fieldAccumulators)));
Sign up to request clarification or add additional context in comments.

1 Comment

you are welcome. I've added alternative too to use helper functions to create group stage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.