Example usage for com.mongodb BasicDBObjectBuilder start

List of usage examples for com.mongodb BasicDBObjectBuilder start

Introduction

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

Prototype

public static BasicDBObjectBuilder start(final String key, final Object val) 

Source Link

Document

Creates a builder initialized with the given key/value.

Usage

From source file:com.grallandco.mongodb.beersample.BreweryServlet.java

License:Open Source License

/**
 * Handle the /breweries/search action./*from ww w  .  ja  v  a2s .  c om*/
 *
 * @param request the HTTP request object.
 * @param response the HTTP response object.
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
private void handleSearch(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    String name = request.getParameter("value").toLowerCase();

    DBObject query = QueryBuilder.start().put("name")
            .regex(java.util.regex.Pattern.compile(name, Pattern.CASE_INSENSITIVE)).get();

    DBCursor cursor = db.getCollection("brewery").find(query).sort(BasicDBObjectBuilder.start("name", 1).get())
            .limit(20);
    ArrayList<HashMap<String, String>> breweries = new ArrayList<HashMap<String, String>>();
    while (cursor.hasNext()) {
        DBObject row = cursor.next();
        HashMap<String, String> brewery = new HashMap<String, String>();
        brewery.put("id", (String) row.get("_id"));
        brewery.put("name", (String) row.get("name"));
        breweries.add(brewery);
    }

    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    out.print(gson.toJson(breweries));
    out.flush();
}

From source file:com.groupon.jenkins.mongo.CauseActionConverter.java

License:Open Source License

@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    if (value == null)
        return null;
    CauseAction action = (CauseAction) value;
    List causes = new BasicDBList();

    for (Object obj : action.getCauses()) {
        causes.add(getMapper().toDBObject(obj));
    }/*from  w  ww  . j a  va2s  .c  o  m*/
    return BasicDBObjectBuilder.start("causes", causes).add("className", CauseAction.class.getName()).get();
}

From source file:com.icoin.trading.tradeengine.query.tradeexecuted.repositories.TradeExecutedQueryRepositoryImpl.java

License:Apache License

@Override
public List<OpenHighLowCloseVolume> ohlc(String orderBookIdentifier, Date start, Date end, Pageable pageable) {
    hasLength(orderBookIdentifier);/*from www  .  j a  v a2 s . c o  m*/
    notNull(pageable);

    DBObject command = BasicDBObjectBuilder.start("aggregate", TRADE_EXECUTED_ENTRY_COLLECTION).get();

    final DBObject criteriaObject = Criteria.where("orderBookIdentifier").is(orderBookIdentifier)
            .and("tradeTime").gte(start).lt(end).getCriteriaObject();
    DBObject match = BasicDBObjectBuilder.start("$match", criteriaObject).get();

    DBObject projection = BasicDBObjectBuilder
            .start("year", BasicDBObjectBuilder.start("$year", "$tradeTime").get())
            .append("month", BasicDBObjectBuilder.start("$month", "$tradeTime").get())
            .append("day", BasicDBObjectBuilder.start("$dayOfMonth", "$tradeTime").get())
            .append("hour", BasicDBObjectBuilder.start("$hour", "$tradeTime").get())
            .append("minute", BasicDBObjectBuilder.start("$minute", "$tradeTime").get())
            .append("tradedPrice", 1).append("tradedAmount", 1).append("_id", 0).get();

    DBObject project = BasicDBObjectBuilder.start("$project", projection).get();

    //{ "aggregate" : "tradeExecutedEntry" ,
    // "pipeline" : [ { "$match" : { "orderBookIdentifier" : "f830f7e3-9f99-4688-92e7-6dbafc7220a8" ,
    // "tradeTime" : { "$gte" : { "$date" : "2007-12-12T04:12:12.120Z"} ,
    // "$lt" : { "$date" : "2012-12-12T04:12:04.120Z"}}}} ,
    // { "$project" : { "tradeTime" : 1 , "tradedPrice.amount" : 1 , "tradedAmount.amount" : 1 , "year" :
    // { "$year" : [ "$tradeTime"]} , "month" : { "$month" : [ "$tradeTime"]} , "week" : { "$week" : [ "$tradeTime"]}}} ,
    // { "$group" : { "_id" : "$year" , "open" : { "$first" : "$tradedPrice"} , "high" : { "$max" : "$tradedPrice"} ,
    // "low" : { "$min" : "$tradedPrice"} , "close" : { "$last" : "$tradedPrice"} , "volume" : { "$sum" : "$tradedAmount"}}} ,
    // { "$skip" : 0} , { "$limit" : 100}]}

    //        {"$project": {
    //            "year":       {"$year": "$dt"},
    //            "month":      {"$month": "$dt"},
    //            "day":        {"$dayOfMonth": "$dt"},
    //            "hour":       {"$hour": "$dt"},
    //            "minute":     {"$minute": "$dt"},
    //            "second":     {"$second": "$dt"},
    //            "dt": 1,
    //                    "p": 1 }},
    //        {"_id" : {"year": "$year", "month": "$month", "day": "$day", "hour": "$hour", "minute": "$minute" },
    //            "open":  {"$first": "$p"},
    //            "high":  {"$max": "$p"},
    //            "low":   {"$min": "$p"},
    //            "close": {"$last": "$p"} }} ] )

    //        02:41:22.649 [main] DEBUG c.i.t.t.q.t.r.TradeExecutedQueryRepositoryImpl - aggregation { "aggregate" : "tradeExecutedEntry" , "pipeline" : [ { "$match" : { "orderBookIdentifier" : "c623022b-9baa-437a-a70f-b59adead3ecf" , "tradeTime" : { "$gte" : { "$date" : "2007-12-12T04:12:12.120Z"} , "$lt" : { "$date" : "2012-12-12T04:12:04.120Z"}}}} , { "$project" : { "year" : { "$year" : "$tradeTime"} , "month" : { "$month" : "$tradeTime"} , "day" : { "$dayOfMonth" : "$tradeTime"} , "hour" : { "$hour" : "$tradeTime"} , "minute" : { "$minute" : "$tradeTime"} , "tradedPrice" : 1 , "tradedAmount" : 1 , "_id" : 0}} , { "$group" : { "_id" : { "year" : "$year" , "priceCcy" : "$tradedPrice.currency" , "amountCcy" : "$tradedAmount.currency"} , "open" : { "$first" : "$tradedPrice.amount"} , "high" : { "$max" : "$tradedPrice.amount"} , "low" : { "$min" : "$tradedPrice.amount"} , "close" : { "$last" : "$tradedPrice.amount"} , "volume" : { "$sum" : "$tradedAmount.amount"}}}]} found :[ { "_id" : { "year" : 2012 , "priceCcy" : "CNY" , "amountCcy" : "BTC"} , "open" : 10500 , "high" : 10500 , "low" : 10500 , "close" : 10500 , "volume" : 11550000000} , { "_id" : { "year" : 2010 , "priceCcy" : "CNY" , "amountCcy" : "BTC"} , "open" : 10500 , "high" : 10500 , "low" : 10500 , "close" : 10500 , "volume" : 2100000000} , { "_id" : { "year" : 2011 , "priceCcy" : "CNY" , "amountCcy" : "BTC"} , "open" : 10500 , "high" : 10500 , "low" : 10500 , "close" : 10500 , "volume" : 1050000000}]
    //        02:46:45.023 [main] DEBUG c.i.t.t.q.t.r.TradeExecutedQueryRepositoryImpl - aggregation { "aggregate" : "tradeExecutedEntry" , "pipeline" : [ { "$match" : { "orderBookIdentifier" : "04527652-b53b-47bf-967d-2001fbe18c13" , "tradeTime" : { "$gte" : { "$date" : "2007-12-12T04:12:12.120Z"} , "$lt" : { "$date" : "2012-12-12T04:12:04.120Z"}}}} , { "$project" : { "year" : { "$year" : "$tradeTime"} , "month" : { "$month" : "$tradeTime"} , "day" : { "$dayOfMonth" : "$tradeTime"} , "hour" : { "$hour" : "$tradeTime"} , "minute" : { "$minute" : "$tradeTime"} , "tradedPrice" : 1 , "tradedAmount" : 1 , "_id" : 0}} , { "$group" : { "_id" : { "year" : "$year" , "priceCcy" : "$tradedPrice.currency" , "amountCcy" : "$tradedAmount.currency"} , "open" : { "$first" : "$tradedPrice.amount"} , "high" : { "$max" : "$tradedPrice.amount"} , "low" : { "$min" : "$tradedPrice.amount"} , "close" : { "$last" : "$tradedPrice.amount"} , "volume" : { "$sum" : "$tradedAmount.amount"}}}]} found :[ { "_id" : { "year" : 2012 , "priceCcy" : "CNY" , "amountCcy" : "BTC"} , "open" : 10500 , "high" : 10500 , "low" : 10500 , "close" : 10500 , "volume" : 11550000000} , { "_id" : { "year" : 2010 , "priceCcy" : "CNY" , "amountCcy" : "BTC"} , "open" : 10500 , "high" : 10500 , "low" : 10500 , "close" : 10500 , "volume" : 2100000000} , { "_id" : { "year" : 2011 , "priceCcy" : "CNY" , "amountCcy" : "BTC"} , "open" : 10500 , "high" : 10500 , "low" : 10500 , "close" : 10500 , "volume" : 1050000000}]

    final DBObject groupId = BasicDBObjectBuilder.start("year", "$year")
            //                .append("month", "$month")
            //                .append("day", "$dayOfMonth")
            //                .append("hour", "$hour")
            //                .append("minute", "$minute")
            .append("priceCcy", "$tradedPrice.currency").append("amountCcy", "$tradedAmount.currency").get();
    DBObject groupOp = BasicDBObjectBuilder.start("_id", groupId)
            .append("open", BasicDBObjectBuilder.start("$first", "$tradedPrice.amount").get())
            .append("high", BasicDBObjectBuilder.start("$max", "$tradedPrice.amount").get())
            .append("low", BasicDBObjectBuilder.start("$min", "$tradedPrice.amount").get())
            .append("close", BasicDBObjectBuilder.start("$last", "$tradedPrice.amount").get())
            .append("volume", BasicDBObjectBuilder.start("$sum", "$tradedAmount.amount").get()).get();

    DBObject group = BasicDBObjectBuilder.start("$group", groupOp).get();

    final BasicDBList pipeline = new BasicDBList();
    pipeline.add(match);
    pipeline.add(project);
    pipeline.add(group);
    command.put("pipeline", pipeline);

    CommandResult commandResult = mongoTemplate.executeCommand(command);
    handleCommandError(commandResult, command);

    // map results
    @SuppressWarnings("unchecked")
    Iterable<DBObject> resultSet = (Iterable<DBObject>) commandResult.get("result");
    List<OpenHighLowCloseVolume> mappedResults = Lists.newLinkedList();

    if (logger.isDebugEnabled()) {
        logger.debug("aggregation {} found :{}", command, resultSet);
    }

    System.err.println(Long.MAX_VALUE / 100000000);
    return null;
}

From source file:com.linuxbox.util.lockservice.mongodb.MongoLockService.java

License:Open Source License

/**
 * Request sole access to a lock. Returns true if sole access is granted,
 * false otherwise.//from   w  w  w.j  a va  2 s.co m
 * 
 * @param identifier
 * @return
 */
@Override
public void lock(String identifier, Object notation) throws LockAcquisitionException {
    try {
        final DBObject controlRecord = BasicDBObjectBuilder.start(LOCK_IDENTIFIER_KEY, identifier)
                .add(LOCK_TIMESTAMP_KEY, new Date()).add(LOCK_NOTE_KEY, notation).get();
        lockCollection.insert(controlRecord);
        return;
    } catch (MongoException e) {
        // because the index for identifier is unique, trying to create
        // another record for the same file will generate an exception
        // that we catch here
        throw new LockAcquisitionException(identifier, e);
    }
}

From source file:com.mingo.mongo.aggregation.AggregationPipelineBuilder.java

License:Apache License

/**
 * Add unwind operator.//from   w  w w  .  j a  v a 2s .  c om
 *
 * @param field field
 * @return {@link AggregationPipelineBuilder}
 */
public AggregationPipelineBuilder unwind(String field) {
    operators.add(BasicDBObjectBuilder.start(UNWIND.getMongoName(), field).get());
    return this;
}

From source file:com.mingo.mongo.aggregation.AggregationPipelineBuilder.java

License:Apache License

/**
 * Builds operator.//  ww w . j a  v a 2s .co  m
 *
 * @param aggregationOperator {@link AggregationOperators}
 * @param field               a key-value map that can be saved to the database
 * @return {@link AggregationPipelineBuilder}
 */
private AggregationPipelineBuilder buildOperator(AggregationOperators aggregationOperator, DBObject field) {
    operators.add(BasicDBObjectBuilder.start(aggregationOperator.getMongoName(), field).get());
    return this;
}

From source file:com.novemberain.quartz.mongodb.MongoDBJobStore.java

License:Open Source License

private DBObject updateThatSetsTriggerStateTo(String state) {
    return BasicDBObjectBuilder.start("$set", new BasicDBObject(TRIGGER_STATE, state)).get();
}

From source file:com.smbtec.xo.mongodb.impl.MongoDbRepositoryImpl.java

License:Apache License

@Override
public <T> T findOne(Class<T> type, Object id) {
    if (Objects.isNull(id)) {
        throw new IllegalArgumentException("Id must not be null");
    }//  w ww . j  a  va2  s .  c  om
    EntityTypeMetadata<DocumentMetadata> entityMetadata = checkEntityMetadata(type);
    String collectionName = entityMetadata.getDatastoreMetadata().getDiscriminator();
    DBObject result = database.getCollection(collectionName)
            .findOne(BasicDBObjectBuilder.start(MONGODB_ID, id).get());
    if (result == null) {
        return null;
    } else {
        MongoDbDocument document = new MongoDbDocument(result, collectionName);
        return (T) xoSession.fromDatastore(document);
    }
}

From source file:com.tadamski.glassfish.mongo.realm.api.MongoRealmApi.java

License:Open Source License

public DBObject getUserByLogin(String login) {
    DBObject queryByLogin = BasicDBObjectBuilder.start(getProperty(LOGIN_PROPERTY), login).get();
    return mongoRealm.getMongoCollection().findOne(queryByLogin, excludePasswordAndSaltProperties());
}

From source file:com.tadamski.glassfish.mongo.realm.api.MongoRealmApi.java

License:Open Source License

public void deleteUser(ObjectId userId) {
    mongoRealm.getMongoCollection().remove(BasicDBObjectBuilder.start("_id", userId).get());
}