[英]how to use mongodb java-driver Projections.slice
I am trying to use Aggregates.project to slice the array in my documents.我正在尝试使用 Aggregates.project 在我的文档中对数组进行切片。 My documents is like我的文件就像
{
"date":"",
"stype_0":[1,2,3,4]
}
in the mongochef looks like在 mongochef 看起来像
and my code in java is :我在java中的代码是:
Aggregates.project(Projections.fields(
Projections.slice("stype_0", pst-1, pen-pst),Projections.slice("stype_1", pst-1, pen-pst),
Projections.slice("stype_2", pst-1, pen-pst),Projections.slice("stype_3", pst-1, pen-pst))))
finally i get error最后我得到错误
First argument to $slice must be an array, but is of type: int
I guess that is because the first element in stype_0 is int , but I really do not know why?我猜这是因为 stype_0 中的第一个元素是 int ,但我真的不知道为什么? Thanks a lot!非常感谢!
Slice has two versions. Slice 有两个版本。 $slice(aggregation)
& $slice(projection)
. $slice(aggregation)
& $slice(projection)
。 You are using the wrong one.你用错了。
Aggregate slice function doesn't have any built-in support.聚合切片函数没有任何内置支持。 Below is an example for one such projection.下面是一个此类投影的示例。 Do the same for all the other projection fields.对所有其他投影字段执行相同操作。
List stype_0 = Arrays.asList("$stype_0", 1, 1);
Bson project = Aggregates.project(Projections.fields(new Document("stype_0", new Document("$slice", stype_0))));
AggregateIterable<Document> iterable = dbCollection.aggregate(Arrays.asList(project));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.