[英]how to use mongodb java-driver Projections.slice
我正在尝试使用 Aggregates.project 在我的文档中对数组进行切片。 我的文件就像
{
"date":"",
"stype_0":[1,2,3,4]
}
我在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))))
最后我得到错误
First argument to $slice must be an array, but is of type: int
我猜这是因为 stype_0 中的第一个元素是 int ,但我真的不知道为什么? 非常感谢!
Slice 有两个版本。 $slice(aggregation)
& $slice(projection)
。 你用错了。
聚合切片函数没有任何内置支持。 下面是一个此类投影的示例。 对所有其他投影字段执行相同操作。
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.