mongo java aggregate_使用Java操作mongoDB使用聚合函数Aggregate,如何操作文档内嵌集合分页?...

{

"_id" : {

"$oid" : "5b8e1d485d30a6499820cfc5"

},

"id" : 5,

"lessonCount" : 5,

"userCount" : 6,

"users" : [{

"phone" : "1301111000",

"liveCount" : 0,

"learns" : [{

"time" : 0,

"id" : 1

}, {

"time" : 0,

"id" : 2

}, {

"time" : 0,

"id" : 3

}, {

"time" : 0,

"id" : 4

}, {

"time" : 0,

"id" : 5

}]

}, {

"phone" : "1301111001",

"liveCount" : 1,

"learns" : [{

"time" : 0,

"id" : 1

}, {

"time" : 1,

"id" : 2

}, {

"time" : 2,

"id" : 3

}, {

"time" : 3,

"id" : 4

}, {

"time" : 4,

"id" : 5

}]

}, {

"phone" : "1301111002",

"liveCount" : 2,

"learns" : [{

"time" : 0,

"id" : 1

}, {

"time" : 2,

"id" : 2

}, {

"time" : 4,

"id" : 3

}, {

"time" : 6,

"id" : 4

}, {

"time" : 8,

"id" : 5

}]

}, {

"phone" : "1301111003",

"liveCount" : 3,

"learns" : [{

"time" : 0,

"id" : 1

}, {

"time" : 3,

"id" : 2

}, {

"time" : 6,

"id" : 3

}, {

"time" : 9,

"id" : 4

}, {

"time" : 12,

"id" : 5

}]

}, {

"phone" : "1301111004",

"liveCount" : 4,

"learns" : [{

"time" : 0,

"id" : 1

}, {

"time" : 4,

"id" : 2

}, {

"time" : 8,

"id" : 3

}, {

"time" : 12,

"id" : 4

}, {

"time" : 16,

"id" : 5

}]

}, {

"phone" : "1301111005",

"liveCount" : 5,

"learns" : [{

"time" : 0,

"id" : 1

}, {

"time" : 5,

"id" : 2

}, {

"time" : 10,

"id" : 3

}, {

"time" : 15,

"id" : 4

}, {

"time" : 20,

"id" : 5

}]

}]

}

现在需要将 users 分页,并且同时将 users 下的 learns 分页,以下是Java代码

public void queryAggregates() {

List<Bson> bsons = new ArrayList<>();

Bson id = Aggregates.match(Filters.eq("id", 5));

Bson match = Aggregates.match(Filters.eq("users.learns.id", 3));

Bson unwind = Aggregates.unwind("$users");

Bson match2 = Aggregates.match(Filters.eq("users.learns.id", 3));

Bson sort = Aggregates.sort(Sorts.descending("users.learns.time"));

Bson limit = Aggregates.limit(5);

Bson project = Aggregates.project(Projections.slice("users.learns", 2,3));

bsons.add(id);

bsons.add(match);

bsons.add(unwind);

bsons.add(match2);

bsons.add(sort);

bsons.add(limit);

bsons.add(project);

AggregateIterable<Document> aggregate = c.aggregate(bsons);

for (Document document : aggregate) {

Object users = document.get("users");

System.out.println(users);

}

}

会报以下错误

com.mongodb.MongoCommandException: Command failed with error 28724 (Location28724): 'First argument to $slice must be an array, but is of type: int' on server 127.0.0.1:27017. The full response is { "ok" : 0.0, "errmsg" : "First argument to $slice must be an array, but is of type: int", "code" : 28724, "codeName" : "Location28724" }

at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:179)

at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:293)

at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)

at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99)

at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:444)

at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72)

at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:200)

at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269)

at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131)

at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:123)

at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:242)

at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:233)

at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:136)

at com.mongodb.operation.AggregateOperationImpl$1.call(AggregateOperationImpl.java:193)

at com.mongodb.operation.AggregateOperationImpl$1.call(AggregateOperationImpl.java:189)

at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:457)

at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:401)

at com.mongodb.operation.AggregateOperationImpl.execute(AggregateOperationImpl.java:189)

at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:294)

at com.mongodb.operation.AggregateOperation.execute(AggregateOperation.java:41)

at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:179)

at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132)

at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:86)

at com.lyn.mongo.TestMongo.queryAggregates(TestMongo.java:93)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

若不使用 project 聚合那行代码,users分页正常,但是无法实现 users.learns分页,

以下是不使用 project 聚合的查询结果

Document{{phone=1301111005, liveCount=5, learns=[Document{{time=0, id=1}}, Document{{time=5, id=2}}, Document{{time=10, id=3}}, Document{{time=15, id=4}}, Document{{time=20, id=5}}]}}

Document{{phone=1301111004, liveCount=4, learns=[Document{{time=0, id=1}}, Document{{time=4, id=2}}, Document{{time=8, id=3}}, Document{{time=12, id=4}}, Document{{time=16, id=5}}]}}

Document{{phone=1301111003, liveCount=3, learns=[Document{{time=0, id=1}}, Document{{time=3, id=2}}, Document{{time=6, id=3}}, Document{{time=9, id=4}}, Document{{time=12, id=5}}]}}

Document{{phone=1301111002, liveCount=2, learns=[Document{{time=0, id=1}}, Document{{time=2, id=2}}, Document{{time=4, id=3}}, Document{{time=6, id=4}}, Document{{time=8, id=5}}]}}

Document{{phone=1301111001, liveCount=1, learns=[Document{{time=0, id=1}}, Document{{time=1, id=2}}, Document{{time=2, id=3}}, Document{{time=3, id=4}}, Document{{time=4, id=5}}]}}

Java api 是 3.8.1

请问如何实现 users.learns 分页?

第一次使用mongoDB,不是太懂,请多指教

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值