List of usage examples for com.mongodb DBCollection findOne
@Nullable public DBObject findOne(final Object id)
From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java
License:Open Source License
@Override public HashMap<String, String> getInitialStats() { HashMap<String, String> stats = new HashMap<String, String>(); com.mongodb.DB db = null;// ww w. jav a 2 s .c om try { db = mongo.getDB(database); db.requestStart(); // get the number of users DBCollection collection = db.getCollection("users"); DBCursor users = collection.find(); int usercnt = users.count(); users.close(); stats.put("usercount", Integer.toString(usercnt)); // find user offset DBObject m = new BasicDBObject().append("_id", 1); DBCursor minUser = collection.find(m).limit(1); int offset = 0; if (minUser.hasNext()) offset = (Integer) minUser.next().toMap().get("_id"); minUser.close(); // get the number of friends per user DBObject q = new BasicDBObject().append("_id", offset); DBObject queryResult = null; queryResult = collection.findOne(q); String x = queryResult.get("ConfFriends").toString(); int frndCount = 0; if (x.equals("") || (!x.equals("") && (x.substring(2, x.length() - 1)).equals(""))) frndCount = 0; else { x = x.substring(2, x.length() - 1); frndCount = x.split(",").length; } stats.put("avgfriendsperuser", Integer.toString(frndCount)); x = queryResult.get("PendFriends").toString(); int pendCount = 0; if (x.equals("") || (!x.equals("") && (x.substring(2, x.length() - 1)).equals(""))) pendCount = 0; else { x = x.substring(2, x.length() - 1); pendCount = x.split(",").length; } stats.put("avgpendingperuser", Integer.toString(pendCount)); // find number of resources for the user DBCollection resCollection = db.getCollection("resources"); DBObject res = new BasicDBObject().append("creatorid", Integer.toString(offset)); DBCursor resQueryResult = null; resQueryResult = resCollection.find(res); int resCount = resQueryResult.count(); resQueryResult.close(); stats.put("resourcesperuser", Integer.toString(resCount)); } catch (Exception e) { e.printStackTrace(System.out); } finally { if (db != null) db.requestDone(); } return stats; }
From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java
License:Open Source License
@Override public int queryPendingFriendshipIds(int profileId, Vector<Integer> pendingFrnds) { int retVal = 0; com.mongodb.DB db = null;/*from w w w . ja va 2s. c o m*/ try { db = mongo.getDB(database); db.requestStart(); DBCollection collection = db.getCollection("users"); DBObject q = new BasicDBObject().append("_id", profileId); DBObject queryResult = null; queryResult = collection.findOne(q); String x = queryResult.get("PendFriends").toString(); if (!x.equals("")) { x = x.substring(2, x.length() - 1); if (!x.equals("")) { String friendIds[] = x.split(","); for (int j = 0; j < friendIds.length; j++) pendingFrnds.add(Integer.parseInt(friendIds[j].trim())); } } } catch (Exception e) { e.printStackTrace(System.out); retVal = -1; } return retVal; }
From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java
License:Open Source License
@Override public int queryConfirmedFriendshipIds(int profileId, Vector<Integer> confFrnds) { int retVal = 0; com.mongodb.DB db = null;//from w ww. ja va 2 s . c o m try { db = mongo.getDB(database); db.requestStart(); DBCollection collection = db.getCollection("users"); DBObject q = new BasicDBObject().append("_id", profileId); DBObject queryResult = null; queryResult = collection.findOne(q); String x = queryResult.get("ConfFriends").toString(); if (!x.equals("")) { x = x.substring(2, x.length() - 1); if (!x.equals("")) { String friendIds[] = x.split(","); for (int j = 0; j < friendIds.length; j++) confFrnds.add(Integer.parseInt(friendIds[j].trim())); } } } catch (Exception e) { e.printStackTrace(System.out); retVal = -1; } return retVal; }
From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java
License:Open Source License
@Override public boolean schemaCreated() { // query for some user and see if it exists com.mongodb.DB db = null;// w w w . j ava 2 s. c o m try { db = mongo.getDB(database); db.requestStart(); DBCollection collection = db.getCollection("users"); DBObject q = new BasicDBObject().append("_id", 0); DBObject queryResult = null; queryResult = collection.findOne(q); db.requestDone(); if (queryResult != null) { return true; } } catch (Exception ex) { db.requestDone(); ex.printStackTrace(); return false; } return false; }
From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java
License:Open Source License
@Override public void reconstructSchema() { com.mongodb.DB db = null;//w ww . j a v a2 s .c o m try { db = mongo.getDB(database); db.requestStart(); // getting the number of users DBCollection collection = db.getCollection("users"); int numUsers = (int) collection.getCount(); // for every user set their conf Friends and pend Friends to null for (int i = 0; i < numUsers; i++) { DBObject r = new BasicDBObject().append("_id", i); DBObject queryResult = collection.findOne(r); BasicDBObject updateCommand = new BasicDBObject(); updateCommand.put("$set", new BasicDBObject("ConfFriends", new ArrayList<Integer>())); WriteResult res = collection.update(r, updateCommand, false, false, writeConcern); updateCommand = new BasicDBObject(); updateCommand.put("$set", new BasicDBObject("PendFriends", new ArrayList<Integer>())); res = collection.update(r, updateCommand, false, false, writeConcern); } if (manipulationArray) { collection = db.getCollection("resources"); int numResources = (int) collection.getCount(); // for every user set their conf Friends and pend Friends to null for (int i = 0; i < numUsers; i++) { DBObject r = new BasicDBObject().append("_id", i); DBObject queryResult = collection.findOne(r); BasicDBObject updateCommand = new BasicDBObject(); updateCommand.put("$set", new BasicDBObject("Manipulations", new ArrayList<Integer>())); WriteResult res = collection.update(r, updateCommand, false, false, writeConcern); } } else { collection = db.getCollection("manipulation"); //TODO: test this if (Boolean.parseBoolean( props.getProperty(MONGODB_SHARDING_PROPERTY, MONGODB_SHARDING_PROPERTY_DEFAULT)) == true) collection.drop(); //this doesnt work with shards else { DBCursor DBCur = collection.find(); while (DBCur.hasNext()) collection.remove(DBCur.next()); } } db.requestDone(); } catch (Exception ex) { db.requestDone(); ex.printStackTrace(); } }
From source file:mx.edu.cide.justiciacotidiana.v1.mongo.MongoInterface.java
License:Open Source License
/** * Recupera un elemento de la coleccin mediante una referencia. * @param collectionName Nombre de la coleccin donde existe el elemento. * @param query Objeto para referencia de bsqueda. * @return BasicDBObject con los datos del elemento encontrado, si existe. *//*from w w w .j ava 2 s . co m*/ public DBObject findOne(String collectionName, BasicDBObject query) { DBCollection tCol = mongoDB.getCollection(collectionName); DBObject ret = tCol.findOne(query); return ret; }
From source file:mx.org.cedn.avisosconagua.mongo.MongoInterface.java
License:Open Source License
/** * Flags te advice as succesfully generated (completed) * @param adviceID ID for the current advice * @param previous ID for the previous advice (if any) * @param title title of the advice/* ww w .j a v a 2s . co m*/ * @param type type of the advice. One of pacdp|atldp|pacht|atlht. * @param date issue date of the advice */ public void setGenerated(String adviceID, String previous, String title, String type, String date) { DBCollection col = mongoDB.getCollection(GENERATED_COL); String isodate = Utils.getOrderDate(date); BasicDBObject nuevo = new BasicDBObject(INTERNAL_FORM_ID, adviceID).append(GENERATED_TITLE, title) .append("previousIssue", previous).append("generationTime", Utils.sdf.format(new Date())) .append("adviceType", type).append("issueDate", isodate); BasicDBObject query = new BasicDBObject(INTERNAL_FORM_ID, adviceID); BasicDBObject old = (BasicDBObject) col.findOne(query); if (null == old) { col.insert(nuevo); } else { col.update(old, nuevo); } }
From source file:mx.org.cedn.avisosconagua.mongo.MongoInterface.java
License:Open Source License
/** * Gets the chain of tracking advices (from newest to oldest) * @param currentId ID for the current advice * @return list of previous advices starting from the current advice *//*from w ww . ja va 2 s.c om*/ public ArrayList<Statistics> getAdviceChain(String currentId) { String searchId = currentId; DBCollection col = mongoDB.getCollection(CAPTURA_COL); Deque<String> deque = new ArrayDeque<>(); while (searchId != null && !searchId.trim().equals("")) { deque.push(searchId); BasicDBObject current = (BasicDBObject) col.findOne(new BasicDBObject(INTERNAL_FORM_ID, searchId)); if (null != current) { current = (BasicDBObject) current.get("precapture"); } if (null != current) { searchId = current.getString("previousIssue"); } else { searchId = null; } } ArrayList<Statistics> ret = new ArrayList<>(); while (!deque.isEmpty()) { String curr = deque.pop(); BasicDBObject lobj = (BasicDBObject) col.findOne(new BasicDBObject(INTERNAL_FORM_ID, curr)); if (null != lobj) { ret.add(new Statistics(lobj)); } } return ret; }
From source file:mx.org.cedn.avisosconagua.mongo.UpdateIssueDate.java
License:Open Source License
public static void main(String[] arg) throws Exception { MongoClientURI mongoClientURI = new MongoClientURI(System.getenv("MONGOHQ_URL")); MongoClient mongoClient = new MongoClient(mongoClientURI); DB mongoDB = mongoClient.getDB(mongoClientURI.getDatabase()); String GENERATED_COL = "GeneratedFiles"; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm"); SimpleDateFormat isoformater = new SimpleDateFormat("YYYY-MM-dd HH:mm"); if (null != mongoClientURI.getUsername()) { mongoDB.authenticate(mongoClientURI.getUsername(), mongoClientURI.getPassword()); }//from w w w .jav a 2s.co m DBCollection col = mongoDB.getCollection(GENERATED_COL); DBCursor cursor = col.find(); for (DBObject obj : cursor) { String date = (String) obj.get("issueDate"); Date fec = null; try { fec = sdf.parse(date); } catch (ParseException npe) { } if (null != fec) { date = isoformater.format(fec); DBObject act = col.findOne(obj); obj.put("issueDate", date); col.update(act, obj); } } }
From source file:net.autosauler.ballance.server.model.AbstractStructuredData.java
License:Apache License
/** * Find last number.//from w ww . j av a 2 s . co m * * @return the long */ protected Long findLastNumber() { Long last = 0L; DB db = Database.get(getDomain()); if (db != null) { Database.retain(); DBCollection coll = db.getCollection(getTableName()); BasicDBObject w = new BasicDBObject(); w.put(fieldname_domain, getDomain()); addFindLastNumberParams(w); BasicDBObject query = new BasicDBObject(); query.put("$query", w); query.put("$orderby", new BasicDBObject(fieldname_number, -1)); DBObject doc = null; try { doc = coll.findOne(query); } catch (com.mongodb.MongoException e) { last = 1L; } if (doc != null) { last = (Long) doc.get(fieldname_number) + 1L; } Database.release(); } if (last.equals(0L)) { last = 1L; } return last; }