コード例 #1
0
  /** @see DATAMONGO-1552 */
  @Test
  public void shouldExposeDefaultCountField() {

    BucketOperation operation = bucket("field");

    assertThat(operation.getFields().exposesSingleFieldOnly(), is(true));
    assertThat(operation.getFields().getField("count"), is(notNullValue()));
  }
コード例 #2
0
  /** @see DATAMONGO-1552 */
  @Test
  public void shouldRenderBucketOutputOperators() {

    BucketOperation operation =
        Aggregation.bucket("field") //
            .andOutputCount()
            .as("titles");

    DBObject dbObject = operation.toDBObject(Aggregation.DEFAULT_CONTEXT);
    assertThat(extractOutput(dbObject), is(JSON.parse("{ titles : { $sum: 1 } }")));
  }
コード例 #3
0
  /** @see DATAMONGO-1552 */
  @Test
  public void shouldRenderAddToSetOperator() {

    BucketOperation operation =
        bucket("field") //
            .andOutput("title")
            .addToSet()
            .as("titles");

    DBObject dbObject = operation.toDBObject(Aggregation.DEFAULT_CONTEXT);
    assertThat(extractOutput(dbObject), is(JSON.parse("{ titles : { $addToSet: \"$title\" } }")));
  }
コード例 #4
0
  /** @see DATAMONGO-1552 */
  @Test
  public void shouldRenderMinOperator() {

    BucketOperation operation =
        bucket("field") //
            .andOutput("score")
            .min()
            .as("min_score");

    DBObject dbObject = operation.toDBObject(Aggregation.DEFAULT_CONTEXT);
    assertThat(extractOutput(dbObject), is(JSON.parse("{ min_score : { $min: \"$score\" } }")));
  }
コード例 #5
0
  /** @see DATAMONGO-1552 */
  @Test
  public void shouldRenderSumWithValueOperator() {

    BucketOperation operation =
        bucket("field") //
            .andOutput("score")
            .sum(4)
            .as("cummulated_score");

    DBObject dbObject = operation.toDBObject(Aggregation.DEFAULT_CONTEXT);
    assertThat(extractOutput(dbObject), is(JSON.parse("{ cummulated_score : { $sum: 4 } }")));
  }
コード例 #6
0
  /** @see DATAMONGO-1552 */
  @Test
  public void shouldRenderSumWithOwnOutputExpression() {

    BucketOperation operation =
        bucket("field") //
            .andOutputExpression("netPrice + tax")
            .apply("$multiply", 5)
            .as("total");

    DBObject dbObject = operation.toDBObject(Aggregation.DEFAULT_CONTEXT);
    assertThat(
        extractOutput(dbObject),
        is(JSON.parse("{ total : { $multiply: [ {$add : [\"$netPrice\", \"$tax\"]}, 5] } }")));
  }
コード例 #7
0
  /** @see DATAMONGO-1552 */
  @Test
  public void shouldRenderBucketOutputExpressions() {

    BucketOperation operation =
        Aggregation.bucket("field") //
            .andOutputExpression("(netPrice + surCharge) * taxrate * [0]", 2)
            .as("grossSalesPrice") //
            .andOutput("title")
            .push()
            .as("titles");

    DBObject dbObject = operation.toDBObject(Aggregation.DEFAULT_CONTEXT);
    assertThat(
        extractOutput(dbObject),
        is(
            JSON.parse(
                "{ \"grossSalesPrice\" : { \"$multiply\" : [ { \"$add\" : [ \"$netPrice\" , \"$surCharge\"]} , \"$taxrate\" , 2]} , \"titles\" : { $push: \"$title\" } }}")));
  }