Using Java to manipulate mongoDB how to manipulate document embedded collection paging using the aggregate function 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
        }]
    }]
}
  • now you need to page users and learns under users at the same time. Here is the Java code
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);
        }
    }

will report the following error

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)

if you do not use project to aggregate that line of code, users paging is normal, but users.learns paging cannot be implemented.
here are the query results without project aggregation

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 is 3.8.1
how to implement users.learns pagination?
I don"t know much about using mongoDB, for the first time. Please give me more advice

.
May.27,2021

should use the $slice operator of Aggregation. For usage, take a look at the documentation: https://docs.mongodb.com/manu.

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3de7c-2c3c4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3de7c-2c3c4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?