|
| 1 | +package docs; |
| 2 | + |
| 3 | +import com.mongodb.client.MongoClient; |
| 4 | +import com.mongodb.client.MongoClients; |
| 5 | +import com.mongodb.client.MongoCollection; |
| 6 | +import com.mongodb.client.MongoDatabase; |
| 7 | + |
| 8 | +import org.bson.Document; |
| 9 | +import org.bson.conversions.Bson; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import com.mongodb.client.model.Filters; |
| 16 | +import com.mongodb.client.model.Sorts; |
| 17 | +import com.mongodb.client.model.Aggregates; |
| 18 | + |
| 19 | +public class Skip { |
| 20 | + |
| 21 | + private final MongoCollection<Document> collection; |
| 22 | + private final MongoClient mongoClient; |
| 23 | + private final MongoDatabase database; |
| 24 | + |
| 25 | + private Skip() { |
| 26 | + final String uri = System.getenv("DRIVER_REF_URI"); |
| 27 | + |
| 28 | + mongoClient = MongoClients.create(uri); |
| 29 | + database = mongoClient.getDatabase("crudOps"); |
| 30 | + collection = database.getCollection("skip"); |
| 31 | + // end declaration |
| 32 | + } |
| 33 | + |
| 34 | + public static void main (String [] args){ |
| 35 | + Skip skip = new Skip(); |
| 36 | + // skip.setupPaintCollection(); |
| 37 | + skip.skipExample(); |
| 38 | + skip.noResultsExample(); |
| 39 | + } |
| 40 | + |
| 41 | + private void skipExample(){ |
| 42 | + // begin skipExample |
| 43 | + import com.mongodb.client.model.Filters; |
| 44 | + import com.mongodb.client.model.Sorts; |
| 45 | + |
| 46 | + // <MongoCollection setup code here> |
| 47 | + |
| 48 | + Bson filter = Filters.empty(); |
| 49 | + collection.find(filter) |
| 50 | + .sort(Sorts.descending("qty")) |
| 51 | + .skip(5) |
| 52 | + .forEach(doc -> System.out.println(doc.toJson())); |
| 53 | + // end skipExample |
| 54 | + } |
| 55 | + |
| 56 | + private void noResultsExample(){ |
| 57 | + // begin noResultsExample |
| 58 | + Bson filter = Filters.empty(); |
| 59 | + collection.find(filter) |
| 60 | + .sort(Sorts.descending("qty")) |
| 61 | + .skip(9) |
| 62 | + .forEach(doc -> System.out.println(doc.toJson())); |
| 63 | + // end noResultsExample |
| 64 | + } |
| 65 | + |
| 66 | + private void skipAggregateExample(){ |
| 67 | + // begin skipAggregateExample |
| 68 | + import com.mongodb.client.model.Filters; |
| 69 | + import com.mongodb.client.model.Sorts; |
| 70 | + import com.mongodb.client.model.Aggregates; |
| 71 | + |
| 72 | + // <MongoCollection setup code here> |
| 73 | + |
| 74 | + Bson filter = Filters.empty(); |
| 75 | + collection.aggregate(Arrays.asList( |
| 76 | + Aggregates.match(filter), |
| 77 | + Aggregates.sort(Sorts.descending("qty")), |
| 78 | + Aggregates.skip(5))) |
| 79 | + .forEach(doc -> System.out.println(doc.toJson())); |
| 80 | + // end skipAggregateExample |
| 81 | + } |
| 82 | + |
| 83 | + private void setupPaintCollection() { |
| 84 | + |
| 85 | + // List<Document> filterdata = new ArrayList<>(); |
| 86 | + |
| 87 | + // Document p1 = new Document("_id", 1).append("color", "red").append("qty", 5); |
| 88 | + // Document p2 = new Document("_id", 2).append("color", "purple").append("qty", 10); |
| 89 | + // Document p3 = new Document("_id", 3).append("color", "blue").append("qty", 9); |
| 90 | + // Document p4 = new Document("_id", 4).append("color", "white").append("qty", 6); |
| 91 | + // Document p5 = new Document("_id", 5).append("color", "yellow").append("qty", 11); |
| 92 | + // Document p6 = new Document("_id", 6).append("color", "pink").append("qty", 3); |
| 93 | + // Document p7 = new Document("_id", 7).append("color", "green").append("qty", 8); |
| 94 | + // Document p8 = new Document("_id", 8).append("color", "orange").append("qty", 7); |
| 95 | + |
| 96 | + // filterdata.add(p1); |
| 97 | + // filterdata.add(p2); |
| 98 | + // filterdata.add(p3); |
| 99 | + // filterdata.add(p4); |
| 100 | + // filterdata.add(p5); |
| 101 | + // filterdata.add(p6); |
| 102 | + // filterdata.add(p7); |
| 103 | + // filterdata.add(p8); |
| 104 | + |
| 105 | + collection.drop(); |
| 106 | + collection.insertMany(Arrays.asList( |
| 107 | + new Document("_id", 1).append("color", "red").append("qty", 5), |
| 108 | + new Document("_id", 2).append("color", "purple").append("qty", 10), |
| 109 | + new Document("_id", 3).append("color", "blue").append("qty", 9), |
| 110 | + new Document("_id", 4).append("color", "white").append("qty", 6), |
| 111 | + new Document("_id", 5).append("color", "yellow").append("qty", 11), |
| 112 | + new Document("_id", 6).append("color", "pink").append("qty", 3), |
| 113 | + new Document("_id", 7).append("color", "green").append("qty", 8), |
| 114 | + new Document("_id", 8).append("color", "orange").append("qty", 7) |
| 115 | + )); |
| 116 | + } |
| 117 | +} |
0 commit comments