Example usage for com.mongodb.client MongoCollection getCodecRegistry

List of usage examples for com.mongodb.client MongoCollection getCodecRegistry

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection getCodecRegistry.

Prototype

CodecRegistry getCodecRegistry();

Source Link

Document

Get the codec registry for the MongoCollection.

Usage

From source file:com.github.cherimojava.data.mongo.entity.EntityInvocationHandler.java

License:Apache License

/**
 * stores the given EntityInvocationHandler represented Entity in the given Collection
 *
 * @param handler/*w w  w  . j  a v a2 s .c  o m*/
 *            EntityInvocationHandler (Entity) to save
 * @param coll
 *            MongoCollection to save entity into
 */
@SuppressWarnings("unchecked")
static <T extends Entity> void save(EntityInvocationHandler handler, MongoCollection<T> coll) {
    for (ParameterProperty cpp : handler.properties.getValidationProperties()) {
        cpp.validate(handler.data.get(cpp.getMongoName()));
    }
    BsonDocumentWrapper wrapper = new BsonDocumentWrapper<>(handler.proxy,
            (org.bson.codecs.Encoder<Entity>) coll.getCodecRegistry().get(handler.properties.getEntityClass()));
    UpdateResult res = coll.updateOne(
            new BsonDocument("_id",
                    BsonDocumentWrapper.asBsonDocument(EntityCodec._obtainId(handler.proxy), idRegistry)),
            new BsonDocument("$set", wrapper), new UpdateOptions());
    if (res.getMatchedCount() == 0) {
        // TODO this seems too nasty, there must be a better way.for now live with it
        coll.insertOne((T) handler.proxy);
    }
    handler.persist();
}

From source file:mongofx.js.api.FindResultIterable.java

License:Open Source License

@JsIgnore
@Override//from ww w .  j  a  va2  s  . c om
public MongoCursor<Document> iterator(int skip, int limit) {
    MongoCollection<Document> collection = getCollection();

    findOptions.skip(skip);
    findOptions.limit(limit);
    if (projection != null) {
        findOptions.projection(projection);
    }
    if (sort != null) {
        findOptions.sort(dbObjectFromMap(sort));
    }
    return new FindIterable(new MongoNamespace(mongoDatabase.getName(), collectionName),
            collection.getCodecRegistry(), //
            collection.getReadPreference(), getExecutor(), findQuery, findOptions).iterator();
}

From source file:mongofx.js.api.FindResultIterable.java

License:Open Source License

public ObjectListPresentation explain() {
    MongoCollection<Document> collection = getCollection();

    FindIterable findIterable = new FindIterable(new MongoNamespace(mongoDatabase.getName(), collectionName),
            collection.getCodecRegistry(), //
            collection.getReadPreference(), getExecutor(), findQuery, findOptions);

    BsonDocument res = findIterable.explainIterator(ExplainVerbosity.QUERY_PLANNER);
    return JsApiUtils.singletonIter(JsApiUtils.convertBsonToDocument(res));
}

From source file:org.radarcns.mongo.util.MongoHelper.java

License:Apache License

/**
 * Finds all documents within a time window belonging to the given subject, source and project.
 * Close the returned iterator after use, for example with a try-with-resources construct.
 *
 * @param collection is the MongoDB that will be queried
 * @param projectName of the project//from   w  ww  .  j a  v a2  s.c o m
 * @param subjectId is the subjectID
 * @param sourceId is the sourceID
 * @param timeFrame the queried timewindow
 * @return a MongoDB cursor containing all documents from the query.
 */
public static MongoCursor<Document> findDocumentsBySource(MongoCollection<Document> collection,
        String projectName, String subjectId, String sourceId, TimeFrame timeFrame) {
    createIndexIfNotAvailable(collection, indexProjectSubjectSourceTimestart);
    Bson querySource = filterSource(projectName, subjectId, sourceId, timeFrame);
    BasicDBObject sortStartTime = new BasicDBObject(KEY + "." + START, ASCENDING);

    if (logger.isDebugEnabled()) {
        BsonDocument findQueryDocument = querySource.toBsonDocument(Document.class,
                collection.getCodecRegistry());
        logger.debug("Filtering query {} and sorting by {}", findQueryDocument, sortStartTime);
    }

    return collection.find(querySource).sort(sortStartTime).iterator();
}