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:Publisher.java

public static void mongoQueryTest2(DB db, List<TripUpdate> tripUpdates) {
    DBCollection coll = db.getCollection("testCollection");
    DBObject d = coll.findOne(new BasicDBObject("_id", tripUpdates.get(0).tripId));
    DBObject a = coll.findOne(new BasicDBObject("_id", tripUpdates.get(0).tripId + "-0"));
    System.out.println("********* MONGO OUTPUT *********");
    System.out.println("Trip: " + d.get("_id").toString());
    System.out.println("Stop delay: " + a.get("arrivalDelay").toString());
    System.out.println("**********************************\n");
}

From source file:Publisher.java

public static void mongoQueryTest(DB db, List<VehiclePositionTest> positions) {
    DBCollection coll = db.getCollection("testCollection");
    DBObject d = coll.findOne(new BasicDBObject("_id", positions.get(0).getVehicleId()));
    System.out.println("********* MONGO OUTPUT *********");
    System.out.println("Vehicle: " + d.get("_id").toString());
    System.out.println("Stop: " + d.get("stopId").toString());
    System.out.println("Trip: " + d.get("tripId").toString());
    System.out.println("Agency: " + d.get("agencyId").toString());
    System.out.println("Time of record: " + d.get("timeOfRecord").toString());
    System.out.println("Time of update: " + d.get("timeOfLocationUpdate").toString());
    System.out.println("Speed: " + d.get("speed").toString());
    System.out.println("Latitude: " + d.get("currentLocationLat").toString());
    System.out.println("Longitude: " + d.get("currentLocationLon").toString());
    System.out.println("Odometer: " + d.get("odometer").toString());
    System.out.println("Orientation: " + d.get("currentOrientation").toString());
    System.out.println("Congestion level: " + d.get("congestionLevel").toString());
    System.out.println("Status: " + d.get("status").toString());
    System.out.println("Label: " + d.get("label").toString());
    System.out.println("License plate: " + d.get("licensePlate").toString());
    System.out.println("**********************************\n");
}

From source file:Credits.java

String SetCredits(String name, int credits) {

    DBCollection coll = db.getCollection("User");
    BasicDBObject newDocument = new BasicDBObject().append("$inc",
            new BasicDBObject().append("Credits", credits));
    coll.update(new BasicDBObject().append("Username", name), newDocument);

    DBObject db = coll.findOne(new BasicDBObject().append("Username", name));
    String s = db.toString();//from  www. ja va  2s .c o m
    String[] a = s.split(",");
    try {

        String b = a[a.length - 1];
        String[] c = b.split(":");
        System.out.println(c[1]);
        return c[1];
    } catch (Exception e) {
        return "0";
    }

}

From source file:act.server.MongoDB.java

License:Open Source License

private static DBObject findOneDoc(DBCollection c, String id_key, Object id) {
    BasicDBObject query = new BasicDBObject();
    query.put(id_key, id);/*www  . j av  a2 s.  c  o  m*/
    DBObject res = c.findOne(query);
    return res;
}

From source file:bhl.pages.database.MongoConnection.java

License:Open Source License

/**
 * Fetch a resource from the server via a given field value
 * @param collName the collection or database name
 * @param value the value of the field//w  w w.j a v a 2 s. co  m
 * @param field the field name
 * @return the response as a string or null if not found
 */
@Override
public String getFromDbByIntField(String collName, int value, String field) throws DbException {
    try {
        connect();
        DBCollection coll = getCollectionFromName(collName);
        DBObject query = new BasicDBObject(field, value);
        DBObject obj = coll.findOne(query);
        if (obj != null)
            return obj.toString();
        else
            return null;
    } catch (Exception e) {
        throw new DbException(e);
    }
}

From source file:bhl.pages.database.MongoConnection.java

License:Open Source License

/**
 * Fetch a resource from the server via a given field value
 * @param collName the collection or database name
 * @param value the value of the field/*from  w  ww .  j av  a  2  s .  c o m*/
 * @param field the field name
 * @return the response as a string or null if not found
 */
@Override
public String getFromDbByField(String collName, String value, String field) throws DbException {
    try {
        connect();
        DBCollection coll = getCollectionFromName(collName);
        DBObject query;
        if (field.equals(JSONKeys._ID)) {
            ObjectId objId = new ObjectId(value);
            query = new BasicDBObject(field, objId);
        } else
            query = new BasicDBObject(field, value);
        DBObject obj = coll.findOne(query);
        if (obj != null)
            return obj.toString();
        else
            return null;
    } catch (Exception e) {
        throw new DbException(e);
    }
}

From source file:br.bireme.scl.BrokenLinks.java

License:Open Source License

private static int getIsisIdField(final String mstName, final DBCollection coll) throws IOException {
    assert mstName != null;
    assert coll != null;

    final BasicDBObject query = new BasicDBObject(MST_FIELD, mstName);
    final BasicDBObject doc = (BasicDBObject) coll.findOne(query);

    if (doc == null) {
        throw new IOException(
                "Missing fields: collection[" + coll.getName() + "] or master name[" + mstName + "]");
    }/*from ww  w .j a  v a2  s.co  m*/

    return doc.getInt(ID_TAG_FIELD);
}

From source file:br.bireme.scl.BrokenLinks.java

License:Open Source License

private static int getIsisUrlFields(final String mstName, final DBCollection coll) throws IOException {
    assert mstName != null;
    assert coll != null;

    final BasicDBObject query = new BasicDBObject(MST_FIELD, mstName);
    final BasicDBObject doc = (BasicDBObject) coll.findOne(query);

    if (doc == null) {
        throw new IOException(
                "Missing fields: collection[" + coll.getName() + "] or master name[" + mstName + "]");
    }//  w  w w .  java2  s . c  om

    return doc.getInt(URL_TAG_FIELD);
}

From source file:br.bireme.scl.BrokenLinks.java

License:Open Source License

private static boolean saveRecord(final String mstName, final int id, final String url, final String err,
        final int urlTag, final List<Integer> ccsFlds, final Master mst, final DBCollection coll,
        final DBCollection hcoll, final Map<Integer, Map<String, Integer>> occMap)
        throws BrumaException, IOException {
    assert mstName != null;
    assert id > 0;
    assert url != null;
    assert urlTag > 0;
    assert err != null;
    assert ccsFlds != null;
    assert mst != null;
    assert coll != null;
    assert hcoll != null;
    assert occMap != null;

    final Record rec = mst.getRecord(id);
    if (!rec.isActive()) {
        //throw new BrumaException("not active record mfn=" + id);
        System.err.println("WARNING: record[" + id + "] is not active. " + "Ignoring it!");
        return false;
    }/*from   w ww  . ja va 2  s .  c  o  m*/

    final List<Field> urls = rec.getFieldList(urlTag);
    final Date now = new Date();
    Date date;

    Map<String, Integer> fldMap = occMap.get(id);
    if (fldMap == null) {
        fldMap = new HashMap<String, Integer>();
        occMap.put(id, fldMap);
    }

    final int occ = nextOcc(url, urls, fldMap);
    if (occ == -1) {
        System.err.println("url[" + url + "] not found. mfn=" + id);
        //throw new IOException("url[" + url + "] not found. mfn=" + id);                
        return false;
    }

    final BasicDBObject query = new BasicDBObject(ID_FIELD, id + "_" + occ);
    final boolean ret;
    if (fixedRecently(hcoll, query, now, 60)) {
        ret = false;
    } else {
        final BasicDBObject obj = (BasicDBObject) coll.findOne(query);
        if (obj == null) {
            date = now;
        } else {
            date = obj.getDate(LAST_UPDATE_FIELD);
            if (date == null) {
                date = obj.getDate(DATE_FIELD);
            } else {
                final WriteResult wr = coll.remove(obj, WriteConcern.ACKNOWLEDGED);
                if (!wr.getCachedLastError().ok()) {
                    //TODO
                }
                date = obj.getDate(DATE_FIELD);
            }
        }
        final String url_d = EncDecUrl.decodeUrl(url);
        final String url_d_l = (url_d.length() >= 900) ? url_d.substring(0, 900) + "..." : url_d;
        final String url_l = (url.length() > 900) ? url.substring(0, 900) + "..." : url;
        final BasicDBObject doc = new BasicDBObject();
        doc.put(DATE_FIELD, date);
        doc.put(LAST_UPDATE_FIELD, now);
        doc.put(MST_FIELD, mstName);
        doc.put(ID_FIELD, id + "_" + occ);
        doc.put(BROKEN_URL_FIELD, url_l);
        doc.put(PRETTY_BROKEN_URL_FIELD, url_d_l);
        doc.put(MSG_FIELD, err);
        doc.put(CENTER_FIELD, getCCS(rec, ccsFlds));

        final WriteResult wres = coll.save(doc, WriteConcern.ACKNOWLEDGED);
        ret = wres.getCachedLastError().ok();
    }
    return ret;
}

From source file:br.bireme.scl.BrokenLinks.java

License:Open Source License

private static boolean fixedRecently(final DBCollection hcoll, final BasicDBObject query, final Date now,
        final int days) {
    assert hcoll != null;
    assert query != null;
    assert now != null;
    assert days >= 0;

    final boolean ret;
    final BasicDBObject obj = (BasicDBObject) hcoll.findOne(query);
    if (obj == null) {
        ret = false;/*from w ww  .  j  a v  a  2 s.c o m*/
    } else {
        final Date date = obj.getDate(LAST_UPDATE_FIELD);
        if (date == null) {
            ret = false;
        } else {
            ret = (now.getTime() - date.getTime() <= (days * MILISECONDS_IN_A_DAY));
        }
    }
    return ret;
}