Example usage for com.mongodb DBCollection findOne

List of usage examples for com.mongodb DBCollection findOne

Introduction

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

Prototype

@Nullable
public DBObject findOne(final Object id) 

Source Link

Document

Get a single document from collection by '_id'.

Usage

From source file:com.aw.app.action.UserAction.java

/**
 * /*from  w  ww.j a va 2  s  . co  m*/
 * @param token
 * @return 
 */
public int checkAccess(String token) {
    DBCollection collection = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user_token");
    BasicDBObject filter = new BasicDBObject();
    filter.put("access_token", token);
    DBObject record = collection.findOne(filter);
    if (record != null) {
        boolean login = (Boolean) record.get("is_login");
        if (login) {
            return 1;
        } else {
            return 2;
        }
    } else {
        return 3;
    }
}

From source file:com.aw.app.action.UserAction.java

/**
 * //www. j a va 2s .co  m
 * @param token
 * @return 
 */
public long getLoginUserId(String token) {
    DBCollection collection = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user_token");
    BasicDBObject filter = new BasicDBObject();
    filter.put("access_token", token);
    filter.put("is_login", true);
    DBObject record = collection.findOne(filter);
    if (record != null)
        return (Long) record.get("uid");
    else
        return -1;
}

From source file:com.aw.services.AncientWarServiceImpl.java

/**
 * /*from   w w w.j ava 2s.  c o m*/
 * @param key
 * @return
 * @throws JSONException 
 */
@Override
public JSONObject getSystemSettingByKey(String key) throws JSONException {
    DBCollection collection = MongoDbUtil.getCollection("ancient_war", "aw_system_settings");
    DBObject dbObject = new BasicDBObject();
    dbObject.put("key", key);
    DBObject filteredObject = collection.findOne(dbObject);
    JSONObject json = new JSONObject();
    if (filteredObject != null) {
        json.put("status", filteredObject.get("value").toString());
    } else {
        json.put("status", "");
    }
    return json;
}

From source file:com.aw.services.AncientWarServiceImpl.java

/**
 * /*w w  w.  j a v a 2 s. c  om*/
 * @param deviceId
 * @return
 * @throws JSONException 
 */
@Override
public JSONObject login(String deviceId) throws JSONException {
    JSONObject response = new JSONObject();
    if (deviceId != null && !deviceId.equals("")) {
        DBCollection users = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user");
        BasicDBObject filter = new BasicDBObject("selected_market_id", deviceId);
        BasicDBObject user = (BasicDBObject) users.findOne(filter);

        DBCollection devices = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user_devices");
        BasicDBObject deviceQuery = new BasicDBObject("device_id", deviceId);
        BasicDBObject device = (BasicDBObject) devices.findOne(deviceQuery);
        /// System.out.println("Device Id: "+device.get("device_id"));
        long uid = 0;
        if (device != null && device.containsField("uid") && device.get("uid") != null) {
            uid = (Long) device.get("uid");
            user = (BasicDBObject) users.findOne(new User("uid", uid));

            DBCollection troopsDetailsCollection = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName,
                    "aw_user_troops_details");
            UserTroopsDetails userTroopsDetails;
            userTroopsDetails = new UserTroopsDetails("uid", user.get("uid"));
            BasicDBObject details = (BasicDBObject) troopsDetailsCollection.findOne(userTroopsDetails);
            if (details == null) {
                details = new UserTroopsDetails();
                details.put("uid", user.get("uid"));
                details.put("titan_level", 1);
                MongoDbUtil.saveCollection(MongoDbUtil.defaultDBName, "aw_user_troops_details", details);
            }
            user.put("last_visited", new Date().getTime());
            // update user
            BasicDBObject query = new BasicDBObject().append("uid", user.get("uid"));
            BasicDBObject updateUser = new BasicDBObject();
            updateUser.append("$set", new BasicDBObject("last_visited", user.get("last_visited")));
            MongoDbUtil.updateCollection("aw_user", query, updateUser);
            new StrategyAction().bootStrapAction((Long) user.get("uid"));
            return getUserVillage(uid, "login");
        } else {
            JSONObject json = new JSONObject();
            json.put("device_id", deviceId);
            User createdUser = this.createUser(json);
            if (createdUser != null) {
                return this.getUserVillage((Long) createdUser.get("uid"), "login");
            } else {
                response.put("status", false);
                response.put("message", "User is not found and unable to create the user.");
                return response;
            }
        }
    } else if (deviceId == null || deviceId.equals("")) {
        response.put("status", false);
        response.put("message", "Device id is required.");
        return response;
    } else {
        response.put("status", false);
        response.put("message", "No input information provided.");
        return response;
    }
}

From source file:com.bosscs.spark.mongodb.extractor.MongoNativeExtractor.java

License:Apache License

/**
 * Is sharded collection.//from  w ww  . j a  va  2s  .c o m
 *
 * @param collection the collection
 * @return the boolean
 */
private boolean isShardedCollection(DBCollection collection) {

    DB config = collection.getDB().getMongo().getDB("config");
    DBCollection configCollections = config.getCollection("collections");

    DBObject dbObject = configCollections
            .findOne(new BasicDBObject(MONGO_DEFAULT_ID, collection.getFullName()));
    return dbObject != null;
}

From source file:com.chdi.kundenverwaltung.KundenVerwaltungsLogic.java

public Customer findById(String id) {

    DBCollection table = db.getCollection("user");

    BasicDBObject searchQuery2 = new BasicDBObject().append("ID", id);

    DBObject user = table.findOne(searchQuery2);

    return new Customer(user.get("ID").toString(), user.get("CompanyName").toString(),
            user.get("Name").toString(), user.get("Adress").toString());

}

From source file:com.chdi.kundenverwaltung.KundenVerwaltungsLogic.java

public boolean deleteCustomer(String id) {

    DBCollection table = db.getCollection("user");

    BasicDBObject searchQuery2 = new BasicDBObject().append("ID", id);

    DBObject user = table.findOne(searchQuery2);

    table.remove(user);/*from ww  w  . j  ava  2 s .  com*/

    DBObject usObject = table.findOne(searchQuery2);

    if (usObject == null) {

        return true;
    } else {
        return false;
    }

}

From source file:com.cloudbees.gasp.model.MongoConnection.java

License:Apache License

public String newLocation(GeoLocation location) {

    DBCollection locations = getCollection();

    // Search by "name" for existing record
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("name", location.getName());

    // Upsert from GaspLocation object
    DBObject updateExpression = new BasicDBObject();
    updateExpression.put("name", location.getName());
    updateExpression.put("formattedAddress", location.getFormattedAddress());

    // Include Location lat/lng data
    BasicDBObject locObj = new BasicDBObject();
    locObj.put("lng", location.getLocation().getLng());
    locObj.put("lat", location.getLocation().getLat());
    updateExpression.put("location", locObj);

    locations.update(searchQuery, updateExpression, true, false);
    String upsertId = locations.findOne(searchQuery).get("_id").toString();

    if (logger.isDebugEnabled())
        logger.debug("addLocation(): " + upsertId);

    // Return the _id field for the upserted record
    return upsertId;
}

From source file:com.continuent.tungsten.replicator.applier.MongoApplier.java

License:Open Source License

/**
 * {@inheritDoc}/*from   w  ww  .  j  av a 2  s  . com*/
 * 
 * @see com.continuent.tungsten.replicator.applier.RawApplier#getLastEvent()
 */
@Override
public ReplDBMSHeader getLastEvent() throws ReplicatorException, InterruptedException {
    // Connect to the schema and collection.
    DB db = m.getDB(serviceSchema);
    DBCollection trepCommitSeqno = db.getCollection("trep_commit_seqno");

    // Construct query.
    DBObject query = new BasicDBObject();
    query.put("task_id", taskId);

    // Find matching trep_commit_seqno value.
    DBObject doc = trepCommitSeqno.findOne(query);

    // Return a constructed header or null, depending on whether we found
    // anything.
    if (doc == null) {
        if (logger.isDebugEnabled())
            logger.debug("trep_commit_seqno is empty: taskId=" + taskId);
        return null;
    } else {
        if (logger.isDebugEnabled())
            logger.debug("trep_commit_seqno entry found: doc=" + doc);

        long seqno = (Long) doc.get("seqno");
        // Cast to integer in MongoDB.
        int fragno = (Integer) doc.get("fragno");
        boolean lastFrag = (Boolean) doc.get("last_frag");
        String sourceId = (String) doc.get("source_id");
        long epochNumber = (Long) doc.get("epoch_number");
        String eventId = (String) doc.get("event_id");
        String shardId = (String) doc.get("shard_id");
        long extractTimestamp = (Long) doc.get("extract_timestamp");
        ReplDBMSHeaderData header = new ReplDBMSHeaderData(seqno, (short) fragno, lastFrag, sourceId,
                epochNumber, eventId, shardId, new Timestamp(extractTimestamp), 0);
        return header;
    }
}

From source file:com.edduarte.argus.document.DocumentCollection.java

License:Apache License

private Document getInternal(String documentUrl) {
    DBCollection collection = documentsDB.getCollection(collectionName);
    BasicDBObject mongoDocument = (BasicDBObject) collection
            .findOne(new BasicDBObject(Document.URL, documentUrl));
    return mongoDocument != null ? new Document(occurrencesDB, mongoDocument) : null;
}