Example usage for com.mongodb BasicDBObjectBuilder append

List of usage examples for com.mongodb BasicDBObjectBuilder append

Introduction

In this page you can find the example usage for com.mongodb BasicDBObjectBuilder append.

Prototype

public BasicDBObjectBuilder append(final String key, final Object val) 

Source Link

Document

Appends the key/value to the active object

Usage

From source file:org.apache.rya.indexing.geotemporal.mongo.MongoEventStorage.java

License:Apache License

@Override
public Collection<Event> search(final Optional<RyaURI> subject,
        final Optional<Collection<IndexingExpr>> geoFilters,
        final Optional<Collection<IndexingExpr>> temporalFilters) throws EventStorageException {
    requireNonNull(subject);//from   w ww .ja  v  a  2s .co  m

    try {
        final Collection<IndexingExpr> geos = (geoFilters.isPresent() ? geoFilters.get() : new ArrayList<>());
        final Collection<IndexingExpr> tempos = (temporalFilters.isPresent() ? temporalFilters.get()
                : new ArrayList<>());
        final DBObject filterObj = queryAdapter.getFilterQuery(geos, tempos);

        final BasicDBObjectBuilder builder = BasicDBObjectBuilder.start(filterObj.toMap());
        if (subject.isPresent()) {
            builder.append(EventDocumentConverter.SUBJECT, subject.get().getData());
        }
        final MongoCursor<Document> results = mongo.getDatabase(ryaInstanceName).getCollection(COLLECTION_NAME)
                .find(BsonDocument.parse(builder.get().toString())).iterator();

        final List<Event> events = new ArrayList<>();
        while (results.hasNext()) {
            events.add(EVENT_CONVERTER.fromDocument(results.next()));
        }
        return events;
    } catch (final MongoException | DocumentConverterException | GeoTemporalIndexException e) {
        throw new EventStorageException("Could not get the Event.", e);
    }
}

From source file:org.geogit.storage.mongo.MongoGraph.java

License:Open Source License

public Edge addEdge(Object id, Vertex out, Vertex in, String label) {
    if (label == null) {
        throw new IllegalArgumentException("Edge label may not be null");
    }/*from  w ww. j  av a 2  s  .  co m*/

    final BasicDBObjectBuilder builder;
    if (id == null) {
        builder = BasicDBObjectBuilder.start();
    } else {
        builder = BasicDBObjectBuilder.start("_id", id);
    }
    DBObject edge = builder.append("_out", out.getId()).append("_in", in.getId()).append("_label", label).get();
    WriteResult result = collection.save(edge);
    if (result.getLastError().ok()) {
        return new MEdge(edge);
    } else {
        throw new RuntimeException("Storing edge to mongo failed: " + result.getError());
    }
}

From source file:org.mule.modules.morphia.MorphiaConnector.java

License:Open Source License

/**
 * Calculates aggregates values without the need for complex map-reduce operations
 *
 * <p/>//  ww w.j a  v  a2s .  c o m
 * {@sample.xml ../../../doc/mule-module-morphia.xml.sample morphia:aggregate}
 *
 * @param collection collection name
 * @param pipeline list of pipeline operators
 * @param exception The exception that needs to be thrown if there is an error executing the aggregation query
 * @param username the username to use in case authentication is required
 * @param password the password to use in case authentication is required, null
 *                 if no authentication is desired
 * @param host     The host of the Mongo server. If the host is part of a replica set then you can specify all the hosts
 *                 separated by comma.
 * @param port     The port of the Mongo server
 * @param database The database name of the Mongo server
 * @return the aggregation result
 * @throws Exception if there is an exception while aggregating
 */
@Processor
public BasicDBList aggregate(String collection, List<Pipeline> pipeline, @Optional String exception,
        @Optional String username, @Optional @Password String password, @Optional String host,
        @Optional Integer port, @Optional String database) throws Exception {
    if (!pipeline.isEmpty()) {
        Datastore datastore = getDatastore(username, password, database, host, port);
        List<DBObject> dbObjects = new ArrayList<DBObject>();
        for (Pipeline pipelineOperator : pipeline) {
            Object dbObject = JSON.parse(pipelineOperator.toJson());
            if (dbObject == null || !(dbObject instanceof DBObject)) {
                throw new IllegalArgumentException("Illegal pipeline operator '" + pipelineOperator + "'");
            }
            dbObjects.add((DBObject) dbObject);
        }
        BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add("aggregate", collection);
        builder.append("pipeline", dbObjects.toArray());
        CommandResult result = datastore.getDB().command(builder.get());
        if (result.ok()) {
            return (BasicDBList) result.get("result");
        }
        if (exception != null) {
            throw getExceptionFromClassName(exception);
        }
    }
    // Return an empty list
    return new BasicDBList();
}

From source file:org.wso2.security.tools.util.DatabaseUtils.java

License:Open Source License

private static DBObject getWhereClause_1(String methodName, String className) {
    BasicDBObjectBuilder whereBuilder = BasicDBObjectBuilder.start();

    whereBuilder.append("method_name", methodName);
    whereBuilder.append("owner_class", className);
    DBObject where = whereBuilder.get();
    System.out.println(where.toString());
    return where;
}

From source file:usermanager.Main.java

private static DBObject createDBObject(User user) {
    BasicDBObjectBuilder docBuilder = BasicDBObjectBuilder.start();

    docBuilder.append("_id", user.getId());
    docBuilder.append("firstName", user.getFirstName());
    docBuilder.append("lastName", user.getLastName());
    docBuilder.append("email", user.getEmail());

    return docBuilder.get();
}

From source file:usermanager.Main.java

private static DBObject updateDBObject(User user) {
    BasicDBObjectBuilder docBuilder = BasicDBObjectBuilder.start();

    docBuilder.append("firstName", user.getFirstName());
    docBuilder.append("lastName", user.getLastName());
    docBuilder.append("email", user.getEmail());

    return docBuilder.get();
}