我正在尝试使用 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));
关于java - 如何使用 mongodb java-driver Projections.slice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40564791/