Java Code Examples for com.mongodb.client.model.Sorts#ascending()

The following examples show how to use com.mongodb.client.model.Sorts#ascending() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GetSortBsonVisitor.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Builds singleton List (with always 1 entry) of a Bson document used for sorting based on the passed
 * {@code sortDirection} and {@code fieldName}.
 *
 * @param sortDirection the {@link SortDirection} to apply.
 * @param sortKey the sorting key.
 * @return the singleton List of a Bson sort document.
 */
private static List<Bson> getSortBson(final SortDirection sortDirection, final String sortKey) {
    requireNonNull(sortDirection);

    final Bson sort;
    switch (sortDirection) {
        case ASC:
            sort = Sorts.ascending(sortKey);
            break;
        case DESC:
            sort = Sorts.descending(sortKey);
            break;
        default:
            throw new IllegalStateException("Unknown SortDirection=" + sortDirection);
    }

    return Collections.singletonList(sort);
}
 
Example 2
Source File: InsertMongoDB.java    From Java-Data-Analysis with MIT License 5 votes vote down vote up
public static void printCollection(MongoCollection collection) {
    Bson bson = Sorts.ascending("fname");
    FindIterable<Document> docs = collection.find().sort(bson);
    int num = 0;
    for (Document doc : docs) {
        String name = doc.getString("fname");
        String relation = doc.getString("relation");
        System.out.printf("%4d. %s, %s%n", ++num, name, relation);
    }
}
 
Example 3
Source File: PrintMongDB.java    From Java-Data-Analysis with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    MongoClient client = new MongoClient("localhost", 27017);
    MongoDatabase friends = client.getDatabase("friends");
    MongoCollection relatives = friends.getCollection("relatives");
    
    Bson bson = Sorts.ascending("fname");
    FindIterable<Document> docs = relatives.find().sort(bson);
    int num = 0;
    for (Document doc : docs) {
        String name = doc.getString("fname");
        String relation = doc.getString("relation");
        System.out.printf("%4d. %s, %s%n", ++num, name, relation);
    }
}
 
Example 4
Source File: PrintMongoDB.java    From Java-Data-Analysis with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    MongoClient client = new MongoClient("localhost", 27017);
    MongoDatabase friends = client.getDatabase("friends");
    MongoCollection relatives = friends.getCollection("relatives");
    
    Bson bson = Sorts.ascending("fname");
    FindIterable<Document> docs = relatives.find().sort(bson);
    int num = 0;
    for (Document doc : docs) {
        String name = doc.getString("fname");
        String relation = doc.getString("relation");
        System.out.printf("%4d. %s, %s%n", ++num, name, relation);
    }
}
 
Example 5
Source File: MongoThingsSearchPersistence.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Source<Metadata, NotUsed> sudoStreamMetadata(final EntityId lowerBound) {
    final Bson notDeletedFilter = Filters.exists(FIELD_DELETE_AT, false);
    final Bson filter = lowerBound.isDummy()
            ? notDeletedFilter
            : Filters.and(notDeletedFilter, Filters.gt(FIELD_ID, lowerBound.toString()));
    final Bson relevantFieldsProjection =
            Projections.include(FIELD_ID, FIELD_REVISION, FIELD_POLICY_ID, FIELD_POLICY_REVISION,
                    FIELD_PATH_MODIFIED);
    final Bson sortById = Sorts.ascending(FIELD_ID);
    final Publisher<Document> publisher = collection.find(filter)
            .projection(relevantFieldsProjection)
            .sort(sortById);
    return Source.fromPublisher(publisher).map(MongoThingsSearchPersistence::readAsMetadata);
}
 
Example 6
Source File: AggregationQuery.java    From immutables with Apache License 2.0 -70 votes vote down vote up
@Override
public void process(Consumer<Bson> consumer) {
  final Function<Collation, Bson> toSortFn = col -> {
    final String name = naming.get(col.path());
    return col.direction().isAscending() ? Sorts.ascending(name) : Sorts.descending(name);

  };

  BsonDocument sort = new BsonDocument();
  for (Collation collation: query.collations()) {
    sort.putAll(toSortFn.apply(collation).toBsonDocument(BsonDocument.class, codecRegistry));
  }

  if (!sort.isEmpty()) {
    consumer.accept(Aggregates.sort(sort));
  }
}