Closed WonderLand33 closed 10 months ago
When using vertx-mongo-client to aggregate my MongoDB collection zoo, $skip and $limit are not working.
vertx-mongo-client
zoo
$skip
$limit
vertx-mongo-clien: 4.4.4 mongodb: 5.0.0
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" } ])
Query
db.getCollection("zoo").aggregate([{ $match: {} }, { $group: { _id: "$type", type: { $addToSet: "$breeds" } } }, { $skip: 0 }, { $limit: 1 }])
{ "_id": "Canidae", "type": [ "German Shepherd", "Shiba Inu", "Labrador Retriever" ] }
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
resolved
Questions
When using
vertx-mongo-client
to aggregate my MongoDB collectionzoo
,$skip
and$limit
are not working.Version
vertx-mongo-clien: 4.4.4 mongodb: 5.0.0
Steps to reproduce
Insert some data
Query
public class AggTest2 {
}
{"cursor":{"firstBatch":[{"_id":"Ovipara","breeds":["turkey"]}],"id":0,"ns":"orangeiot.zoo"},"ok":1.0}