What is the correct way to use operators in Mongo

2019-08-25 12:21发布

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条回答
姐就是有狂的资本
2楼-- · 2019-08-25 12:27

You should use projection instead of expression like this:

.project(projection("count",projection("$size", "entities" )))
查看更多
登录 后发表回答