问题
I have two documents at this stage in my aggregation pipeline which are:
{
"_id" : "Piers Morgan",
"entities" : ["Sexism", "Charlotte Hawkins","Red carpet"]
}
{
"_id" : "Gareth Bale",
"entities" : ["Sergio Busquets", "Real Madrid C.F.", "EFL Cup", "Copa del Rey"]
}
I wish simply to return a projection which is the id and the size of the array, using Morphia in Java. In Mongo this can be done using:
{ $project: { count : {$size : "$entities"} } }
In Morphia I have attempted:
.project(projection("count",
Projection.expression("$size", "entities")));
which returns java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject
What is the correct equivalent expression in Morphia?
回答1:
You should use projection
instead of expression
like this:
.project(projection("count",projection("$size", "entities" )))
来源:https://stackoverflow.com/questions/45214057/what-is-the-correct-way-to-use-operators-in-mongo-aggregation-pipelines-with-mor