vert-x3 / vertx-mongo-client

Mongo Client for Eclipse Vert.x
http://vertx.io
Apache License 2.0
58 stars 98 forks source link

When using vertx-mongo-client to aggregate my MongoDB collection zoo, $skip and $limit are not working. #310

Closed WonderLand33 closed 10 months ago

WonderLand33 commented 10 months ago

Questions

When using vertx-mongo-client to aggregate my MongoDB collection zoo, $skip and $limit are not working.

Version

vertx-mongo-clien: 4.4.4 mongodb: 5.0.0

Steps to reproduce

  1. Insert some data

    db.zoo.insertMany([
    {
        "_id": ObjectId("62844768a10821fde0709281"),
        "breeds": "Shiba Inu",
        "type": "Canidae"
    },
    {
        "_id": ObjectId("62844758a10821fde0709262"),
        "breeds": "Labrador Retriever",
        "type": "Canidae"
    },
    {
        "_id": ObjectId("62844758a10821fde0709264"),
        "breeds": "German Shepherd",
        "type": "Canidae"
    },   
    {
        "_id": ObjectId("62844758a10821fde0709267"),
        "breeds": "turkey",
        "type": "Ovipara"
    }
    ])
  2. Query

    db.getCollection("zoo").aggregate([{
    $match: {}
    }, {
    $group: {
    
        _id: "$type",
        type: {
            $addToSet: "$breeds"
        }
    }
    }, {
    $skip: 0
    }, {
    $limit: 1
    }])

3. Write Java code and run it.

public class AggTest2 {

public static void main(String[] args) throws InterruptedException {

    MongoClient client = MongoClient.createShared(Vertx.vertx(), new JsonObject()
            .put("host", "127.0.0.1")
            .put("port", 27017)
            .put("db_name", "testDB")
    );

    JsonObject query = new JsonObject()
            .put("type", "Canidae");
    JsonObject groupFields = new JsonObject()
            .put("_id", "$type")
            .put("breeds", new JsonObject().put("$addToSet", "$breeds"));

    int skip = 0;
    int limit = 1;
    JsonObject command = new JsonObject()
            .put("aggregate", "zoo")
            .put("pipeline", new JsonArray()
                    .add(new JsonObject().put("$match", query))
                    .add(new JsonObject().put("$group", groupFields))
                    .add(new JsonObject().put("$skip", skip))
                    .add(new JsonObject().put("$limit", limit)))
            .put("cursor", new JsonObject());
    client.runCommand("aggregate", command, res -> {
        if (res.succeeded()) {
            System.out.println(res.result());
        } else {
            res.cause().printStackTrace();
        }
    });

    Thread.sleep(10000L);

}

}


- Output:

{"cursor":{"firstBatch":[{"_id":"Ovipara","breeds":["turkey"]}],"id":0,"ns":"orangeiot.zoo"},"ok":1.0}



- Document link: https://vertx.io/docs/vertx-mongo-client/java/#_running_other_mongodb_commands
WonderLand33 commented 10 months ago

resolved