Example usage for com.mongodb.client.model Indexes descending

List of usage examples for com.mongodb.client.model Indexes descending

Introduction

In this page you can find the example usage for com.mongodb.client.model Indexes descending.

Prototype

public static Bson descending(final List<String> fieldNames) 

Source Link

Document

Create an index key for an ascending index on the given fields.

Usage

From source file:com.redhat.jielicious.storage.mongo.handler.MongoStorageHandler.java

License:Open Source License

private void putAll(String body, SecurityContext context, String systemId, String agentId, String jvmId,
        final String namespace, AsyncResponse asyncResponse, final String collectionSuffix) {
    if (!namespaceSet.contains(namespace)) {
        namespaceSet.add(namespace);//from  w w w  .  j  av  a  2 s .  c om
    }

    if (!indexSet.contains(namespace + collectionSuffix)) {
        ThermostatMongoStorage.getDatabase().getCollection(namespace + collectionSuffix)
                .createIndex(Indexes.descending("systemId"), new IndexOptions().background(true));
        ThermostatMongoStorage.getDatabase().getCollection(namespace + collectionSuffix)
                .createIndex(Indexes.descending("agentId"), new IndexOptions().background(true));
        ThermostatMongoStorage.getDatabase().getCollection(namespace + collectionSuffix)
                .createIndex(Indexes.descending("jvmId"), new IndexOptions().background(true));
        indexSet.add(namespace + collectionSuffix);
    }

    try {
        BasicDBList inputList = (BasicDBList) JSON.parse(body);

        final List<Document> items = new ArrayList<>();
        for (Object item : inputList) {
            items.add(Document.parse(new DocumentBuilder(item.toString())
                    .addTags(context.getUserPrincipal().getName()).addId("systemId", systemId)
                    .addId("agentId", agentId).addId("jvmId", jvmId).build()));
        }

        TimedRequest<Boolean> timedRequest = new TimedRequest<>();

        Boolean response = timedRequest.run(new TimedRequest.TimedRunnable<Boolean>() {
            @Override
            public Boolean run() {
                try {
                    ThermostatMongoStorage.getDatabase().getCollection(namespace + collectionSuffix)
                            .insertMany(items);
                } catch (Exception e) {
                    return Boolean.FALSE;
                }
                return Boolean.TRUE;
            }
        });

        asyncResponse.resume(Response.status(Response.Status.OK).entity("PUT: " + response.toString()).build());
    } catch (Exception e) {
        asyncResponse.resume(Response.status(Response.Status.BAD_REQUEST).build());
    }
}

From source file:io.lumeer.storage.mongodb.dao.organization.MongoPaymentDao.java

License:Open Source License

@Override
public void createPaymentRepository(final Organization organization) {
    database.createCollection(databaseCollectionName(organization));

    MongoCollection<Document> groupCollection = database.getCollection(databaseCollectionName(organization));
    groupCollection.createIndex(Indexes.ascending(PaymentCodec.PAYMENT_ID), new IndexOptions().unique(true));
    groupCollection.createIndex(Indexes.descending(PaymentCodec.DATE), new IndexOptions().unique(true));
    groupCollection.createIndex(Indexes.descending(PaymentCodec.START), new IndexOptions().unique(true));
    groupCollection.createIndex(Indexes.descending(PaymentCodec.VALID_UNTIL), new IndexOptions().unique(true));
}

From source file:okra.index.IndexCreator.java

License:Open Source License

public static <T extends OkraItem> void ensureIndexes(final Okra<T> okra, final MongoClient mongo,
        final String database, final String collection) {
    okra.getIndexDefs().stream().map(indexDef -> {
        final boolean ascending = indexDef.getOrdering() == null || indexDef.getOrdering().equals(Ordering.ASC);

        final Bson ordering = ascending ? Indexes.ascending(indexDef.getAttrs())
                : Indexes.descending(indexDef.getAttrs());

        return mongo.getDatabase(database).getCollection(collection).createIndex(ordering);
    }).forEach(indexName -> LOGGER.info("Done. Index name: {}", indexName));
}