What is the correct way to use operators in Mongo aggregation pipelines with Morphia

我们两清 提交于 2019-12-12 05:29:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!