Example usage for com.mongodb BasicDBObject BasicDBObject

List of usage examples for com.mongodb BasicDBObject BasicDBObject

Introduction

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

Prototype

public BasicDBObject(final String key, final Object value) 

Source Link

Document

Creates an object with the given key/value

Usage

From source file:AdminServer.ServiceS.java

private ArrayList getData(DBCollection collection, methodS type, String name) {
    ArrayList data = new ArrayList<>();
    DBCursor cursor;/*  w w w .  ja v  a2 s .  c om*/

    if (type == methodS.ATM) {
        cursor = collection.find();
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            String no = obj.get("SerialNum").toString();
            String ip = obj.get("ipAddr").toString();
            String money = obj.get("money").toString();
            String usr = obj.get("currentUser").toString();
            data.add(new tellerD(no, ip, money, usr));
        }
    } else if (type == methodS.USER) {
        cursor = collection.find(new BasicDBObject("name", name));
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            String usr = obj.get("name").toString();
            boolean ip = (boolean) obj.get("locked");
            data.add(new userD(usr, ip));
        }
    } else if (type == methodS.TRADE) {
        cursor = collection.find(new BasicDBObject("ipAddr", name));
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            String no = obj.get("serial").toString();
            String ip = obj.get("ipAddr").toString();
            String method = obj.get("type").toString();
            String usr = obj.get("name").toString();
            String delta = obj.get("delta").toString();
            String remains = obj.get("remains").toString();
            String date = obj.get("time").toString();
            data.add(new tradeD(no, ip, usr, method, delta, remains, date));
        }
    }
    return data;
}

From source file:aic2013.extractor.MongoDataAccess.java

public void forAllByUserId(long id, Processor<Status> processor) {
    DB db = mongoClient.getDB("twitterdb");
    DBCollection statusCollection = db.getCollection("statuses");
    DBCursor cursor = statusCollection.find(new BasicDBObject("user.id", id));

    while (cursor.hasNext()) {
        try {/*from   www . j a v  a  2  s  .  co m*/
            processor.process(DataObjectFactory.createStatus(cursor.next().toString()));
        } catch (TwitterException ex) {
            Logger.getLogger(MongoDataAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

/**
 * <p>/*w  w  w  .ja v a  2  s  .c om*/
 * Translates the MongoDB identifier to Mahout/MongoDBDataModel's internal
 * identifier, if required.
 * </p>
 * <p>
 * If MongoDB identifiers are long datatypes, it returns the id.
 * </p>
 * <p>
 * This conversion is needed since Mahout uses the long datatype to feed the
 * recommender, and MongoDB uses 12 bytes to create its identifiers.
 * </p>
 *
 * @param id     MongoDB identifier
 * @param isUser
 * @return String containing the translation of the external MongoDB ID to
 *         internal long ID (mapping).
 * @see #fromLongToId(long)
 * @see <a href="http://www.mongodb.org/display/DOCS/Object%20IDs">
 *      Mongo Object IDs</a>
 */
public String fromIdToLong(String id, boolean isUser) {
    DBObject objectIdLong = collectionMap.findOne(new BasicDBObject("element_id", id));
    if (objectIdLong != null) {
        Map<String, Object> idLong = (Map<String, Object>) objectIdLong.toMap();
        Object value = idLong.get("long_value");
        return value == null ? null : value.toString();
    } else {
        objectIdLong = new BasicDBObject();
        String longValue = Long.toString(idCounter++);
        objectIdLong.put("element_id", id);
        objectIdLong.put("long_value", longValue);
        collectionMap.insert(objectIdLong);
        log.info("Adding Translation {}: {} long_value: {}", isUser ? "User ID" : "Item ID", id, longValue);
        return longValue;
    }
}

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

/**
 * <p>/*from  w w w.  j a  v a  2  s . c  o  m*/
 * Translates the Mahout/MongoDBDataModel's internal identifier to MongoDB
 * identifier, if required.
 * </p>
 * <p>
 * If MongoDB identifiers are long datatypes, it returns the id in String
 * format.
 * </p>
 * <p>
 * This conversion is needed since Mahout uses the long datatype to feed the
 * recommender, and MongoDB uses 12 bytes to create its identifiers.
 * </p>
 *
 * @param id Mahout's internal identifier
 * @return String containing the translation of the internal long ID to
 *         external MongoDB ID (mapping).
 * @see #fromIdToLong(String, boolean)
 * @see <a href="http://www.mongodb.org/display/DOCS/Object%20IDs">
 *      Mongo Object IDs</a>
 */
public String fromLongToId(long id) {
    DBObject objectIdLong = collectionMap.findOne(new BasicDBObject("long_value", Long.toString(id)));
    Map<String, Object> idLong = (Map<String, Object>) objectIdLong.toMap();
    Object value = idLong.get("element_id");
    return value == null ? null : value.toString();
}

From source file:alto.plugin.webradio.recommender.MongoDBDataModel.java

License:Apache License

/**
 * <p>//  w w  w .  j  ava 2  s  .c om
 * Checks if an ID is currently in the model.
 * </p>
 *
 * @param ID user or item ID
 * @return true: if ID is into the model; false: if it's not.
 */
public boolean isIDInModel(String ID) {
    DBObject objectIdLong = collectionMap.findOne(new BasicDBObject("element_id", ID));
    return objectIdLong != null;
}

From source file:anuncius.singleton.AnunciusDAO.java

public List<Document> search(String query) {
    Document result = new Document();
    result.put("deleted", false);
    MongoCollection collection = this.mongo.getCollection(ADVERTISEMENT_COLLECTION_NAME);

    BasicDBObject document = new BasicDBObject("$text", new BasicDBObject("$search", query));
    Collection data = collection.find(document).into(new ArrayList<>());
    return new ArrayList(data);
}

From source file:anuncius.singleton.MongoHandler.java

public List<Document> getList(String collectionName, int limit, int sort, Document result, String sortParam) {
    MongoCollection<Document> collection = mainDatabase.getCollection(collectionName);
    if (collection != null) {
        FindIterable<Document> iterable;
        if (result == null) {
            result = new Document();
        }/* www .  ja v  a2  s .co  m*/
        iterable = collection.find(result);
        if (limit > 0) {
            iterable = iterable.limit(limit);
        }
        if (sortParam != null) {
            iterable = iterable.sort(new BasicDBObject(sortParam, sort));
        }
        return iterable.into(new ArrayList<>());
    }
    return new ArrayList<>();
}

From source file:app.data.local.CollectionBindingRepositoryImpl.java

License:Apache License

@Override
public void removeFavor(long colId, String uid) {
    Query query = new Query();
    query.addCriteria(/*  ww  w  .ja  v a2s .com*/
            Criteria.where("collectionId").is(colId).and("favors").elemMatch(Criteria.where("uid").is(uid)));

    Update update = new Update();
    //update.unset("dataList.$");
    //mMongoTemplate.upsert(query, update, Favor.class);
    // http://stackoverflow.com/questions/35600557/mongodb-how-using-spring-cryteria-remove-element-from-nested-object-array
    // http://ufasoli.blogspot.fr/2012/09/mongodb-spring-data-remove-elements.html?view=sidebar
    update.pull("favors", new BasicDBObject("uid", uid));
    mMongoTemplate.updateMulti(query, update, CollectionBinding.class);
}

From source file:applango.common.services.DB.mongo.mongoDB.java

public static void updateLicensePrice(DBCollection appInfoConnection, String licenseType, int newPrice) {
    System.out.println("Updating license price of " + licenseType + " to " + newPrice);
    String jsonCustomer = "{$and : [{'licenseType' : '" + licenseType
            + "'}, {'customerId': 'automationCustomer'} ]}";
    DBObject dbObjectRecordQuery = (DBObject) JSON.parse(jsonCustomer);

    BasicDBObject set = new BasicDBObject("$set", new BasicDBObject("price", newPrice));
    appInfoConnection.update(dbObjectRecordQuery, set);
    System.out.println("The new price for " + licenseType + " is " + newPrice);

}

From source file:applango.common.services.DB.mongo.mongoDB.java

public static void updateCustomerAppRankWeightSet(DBCollection appInfoConnection, String weightName,
        boolean defaultWeightSet) {

    System.out.println("Updating " + weightName + " to " + defaultWeightSet);
    String jsonCustomer = "{$and : [{'weightName' : '" + weightName
            + "'}, {'customerId': 'automationCustomer'} ]}";
    DBObject dbObjectRecordQuery = (DBObject) JSON.parse(jsonCustomer);

    BasicDBObject set = new BasicDBObject("$set", new BasicDBObject("defaultWeightSet", defaultWeightSet));
    appInfoConnection.update(dbObjectRecordQuery, set);
    System.out.println("CustomerAppRankWeightSet for " + weightName + " is " + defaultWeightSet);

}