本文介绍了在Morphia的Mongo聚合管道中使用运算符的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现阶段我的聚合管道中有两个文档:
{
我只想使用Java中的Morphia返回一个投影,该投影是ID和数组的大小。在Mongo中,可以使用以下方法完成此操作:
_id: Piers Morgan,
entities:[ Sexism, Charlotte Hawkins, Red地毯]
}
{
_id: Gareth Bale ,
entities:[ Sergio Busquets, Real Madrid CF, EFL Cup, Copa del Rey]
}
{$ project:{count:{$ size: $ entities}}}
在Morphia中,我尝试过:
.project(projection( count,
Projection.expression( $ size, entities))));
返回java.lang.ClassCastException:java.lang.String无法转换为com.mongodb。 DBObject
Morphia中正确的等效表达式是什么?
解决方案您应该使用
projection
而不是expression
这样的.project(projection( count,projection( $ size, entities)))
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?
解决方案You should use
projection
instead ofexpression
like this:.project(projection("count",projection("$size", "entities" )))
这篇关于在Morphia的Mongo聚合管道中使用运算符的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!