Hay una clara diferencia entre "no disponible" y "ningún método auxiliar implementado" , y ese es el caso real aquí. Solo porque no hay un "ayudante" para implementar el $stdDevSamp
o $stdDevPop
operadores, no significa que no se puedan usar, siempre y cuando se conecte a una instancia de MongoDB 3.2, por supuesto.
Todo lo que realmente necesita es una clase personalizada que soporte la AggregationOperation
interfaz, que permitirá la construcción usando DBObject
:
public class CustomAggregationOperation implements AggregationOperation {
private DBObject operation;
public CustomAggregationOperation (DBObject operation) {
this.operation = operation;
}
@Override
public DBObject toDBObject(AggregationOperationContext context) {
return context.getMappedObject(operation);
}
}
Luego puede usar esa clase en la construcción de canalización de agregación de esta manera:
Aggregation aggregation = newAggregation(
new CustomAggregationOperation(
new BasicDBObject("$sample", new BasicDBObject("size",100))
),
new CustomAggregationOperation(
new BasicDBObject(
"$group",
new BasicDBObject("_id",null)
.append("ageStdDev",new BasicDBObject("$stdDevSamp","$age"))
)
)
);
Y ese es el equivalente del ejemplo de documentación :
db.users.aggregate(
[
{ "$sample": { "size": 100 } },
{ "$group": { "_id": null, "ageStdDev": { "$stdDevSamp": "$age" } } }
]
)
Como interfaz para AggregationOperation
la clase se mezcla fácilmente con los ayudantes implementados:
Aggregation aggregation = newAggregation(
// Using the match helper for the `$match` stage
match(
Criteria.where("age").gte(20).lte(50)
),
// Mixed in with custom classes for the others
new CustomAggregationOperation(
new BasicDBObject("$sample", new BasicDBObject("size",100))
),
new CustomAggregationOperation(
new BasicDBObject(
"$group",
new BasicDBObject("_id",null)
.append("ageStdDev",new BasicDBObject("$stdDevSamp","$age"))
)
)
);
Por lo tanto, aún puede usar funciones incluso si no hay un "ayudante integrado" para resolver la construcción del objeto BSON por usted. Simplemente haga la construcción usted mismo.