List of usage examples for com.mongodb DBCollection findOne
@Nullable public DBObject findOne(final Object id)
From source file:com.intuit.tweetstest.TweetsController.java
@RequestMapping(value = "/feed", method = RequestMethod.GET) protected String fetchFeeds(HttpServletRequest request, HttpServletResponse response) throws ParseException, ServletException, IOException, Exception { JSONObject result = new JSONObject(); String user = request.getParameter("user"); if (user != null && !user.isEmpty()) { String countStr = request.getParameter("count"); int count; // If count parameter is not sent in request, set it to 100 if (countStr != null && !countStr.isEmpty()) { count = Integer.parseInt(countStr); } else {/* w w w . ja v a2 s .c o m*/ count = 100; } // The access to the MongoDB should itself be a part of a separate // package with all the DB APIs wrapped around and published to the users. // Not doing it here to save some time. MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("tweetsdb"); DBCollection usersCollection = db.getCollection("userscollection"); // Not checking if the user is a valid user or not here. That's a // separate code flow on itself. DBObject query = new BasicDBObject("user", user); // Here I am retrieving the list of users the current user is following DBObject userDocument = usersCollection.findOne(query); if (userDocument == null) { result.put(user, "No such user"); result.put("isSuccess", false); sendResponse(response, result); return null; } JSONObject json = (JSONObject) new JSONParser().parse(userDocument.toString()); JSONArray following = (JSONArray) json.get("following"); // A sample of the following array looks like ["user4", "user3"] List<String> followingList = getFollowingList(following); // Once the following list is retrieved, the tweets of those users are // read from the db. JSONArray tweetsArray = retrieveTweetsFromDB(db, followingList, count); result.put("tweets", tweetsArray); result.put("isSuccess", true); } else { System.out.println("Missing user parameter in the request. Returning error"); result.put("Missing parameter", "user"); result.put("isSuccess", false); } sendResponse(response, result); return null; }
From source file:com.jopss.apostas.db.migration.CadastroPerfil.java
@ChangeSet(order = "001", id = "002.001", author = "apostas") public void criarPerfil(DB db) { DBCollection perfil = db.getCollection("perfil"); DBCollection permissao = db.getCollection("permissao"); BasicDBObject filtro = new BasicDBObject(); filtro.put("papel", new BasicDBObject("$in", Arrays.asList("ROLE_GERAL", "ROLE_ADMIN"))); List listaPermissoes = new ArrayList(); DBObject ob1 = permissao.findOne(new BasicDBObject("papel", "ROLE_ADMIN")); listaPermissoes.add(new DBRef("permissao", ob1.get("_id"))); DBObject ob2 = permissao.findOne(new BasicDBObject("papel", "ROLE_GERAL")); listaPermissoes.add(new DBRef("permissao", ob2.get("_id"))); BasicDBObject admin = new BasicDBObject(); admin.append("dataatualizacao", null); admin.append("datacriacao", new Date()); admin.append("nome", "Perfil Administrador"); admin.append("descricao", "Perfil com permisso geral."); admin.append("permissoes", listaPermissoes); perfil.insert(admin);// w w w. j av a2s. c o m BasicDBObject visitante = new BasicDBObject(); visitante.append("dataatualizacao", null); visitante.append("datacriacao", new Date()); visitante.append("nome", "Perfil Visitante"); visitante.append("descricao", "Perfil sem permisso para gerenciar uma aposta."); perfil.insert(visitante); }
From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoRowLocalServiceImpl.java
License:Open Source License
protected ExpandoRow getRow(ExpandoTable expandoTable, long classPK) { DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable); DBObject expandoRowDBObject = dbCollection.findOne(new BasicDBObject("classPK", classPK)); return toExpandoRow(expandoRowDBObject, expandoTable); }
From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java
License:Open Source License
@Override public ExpandoValue addValue(long classNameId, long tableId, long columnId, long classPK, String data) throws PortalException { ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(tableId); ExpandoColumn expandoColumn = ExpandoColumnLocalServiceUtil.getColumn(columnId); ExpandoValue expandoValue = ExpandoValueUtil.create(0); expandoValue.setCompanyId(expandoTable.getCompanyId()); expandoValue.setTableId(tableId);//from w w w . j a va 2 s . c om expandoValue.setColumnId(columnId); expandoValue.setRowId(classPK); expandoValue.setClassNameId(classNameId); expandoValue.setClassPK(classPK); expandoValue.setData(data); DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable); DBObject queryDBObject = new BasicDBObject(); queryDBObject.put("companyId", expandoTable.getCompanyId()); queryDBObject.put("tableId", tableId); queryDBObject.put("rowId", classPK); queryDBObject.put("classNameId", classNameId); queryDBObject.put("classPK", classPK); BasicDBObject expandoValueDBObject = (BasicDBObject) dbCollection.findOne(queryDBObject); if (expandoValueDBObject != null) { expandoValue.setValueId(expandoValueDBObject.getLong("valueId")); DBObject operatorDBObject = new BasicDBObject(); DBObject updateExpandoValueDBObject = new BasicDBObject(expandoColumn.getName(), getData(expandoColumn, expandoValue)); operatorDBObject.put(MongoOperator.SET, updateExpandoValueDBObject); dbCollection.update(expandoValueDBObject, operatorDBObject); } else { long valueId = CounterLocalServiceUtil.increment(); expandoValue.setValueId(valueId); queryDBObject.put("valueId", valueId); queryDBObject.put(expandoColumn.getName(), getData(expandoColumn, expandoValue)); dbCollection.insert(queryDBObject); } return expandoValue; }
From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java
License:Open Source License
@Override public void addValues(long classNameId, long tableId, List<ExpandoColumn> expandoColumns, long classPK, Map<String, String> data) throws PortalException { ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(tableId); ExpandoValue expandoValue = ExpandoValueUtil.create(0); expandoValue.setCompanyId(expandoTable.getCompanyId()); expandoValue.setTableId(tableId);//from w w w . ja va 2 s . c om expandoValue.setRowId(classPK); expandoValue.setClassNameId(classNameId); expandoValue.setClassPK(classPK); DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable); DBObject queryDBObject = new BasicDBObject(); queryDBObject.put("companyId", expandoTable.getCompanyId()); queryDBObject.put("tableId", tableId); queryDBObject.put("rowId", classPK); queryDBObject.put("classNameId", classNameId); queryDBObject.put("classPK", classPK); BasicDBObject expandoValueDBObject = (BasicDBObject) dbCollection.findOne(queryDBObject); if (expandoValueDBObject != null) { expandoValue.setValueId(expandoValueDBObject.getLong("valueId")); DBObject operatorDBObject = new BasicDBObject(); DBObject updateExpandoValueDBObject = new BasicDBObject(); updateExpandoValueDBObject(updateExpandoValueDBObject, expandoColumns, data, expandoValue); operatorDBObject.put(MongoOperator.SET, updateExpandoValueDBObject); dbCollection.update(expandoValueDBObject, operatorDBObject); } else { long valueId = CounterLocalServiceUtil.increment(); expandoValue.setValueId(valueId); queryDBObject.put("valueId", valueId); updateExpandoValueDBObject(queryDBObject, expandoColumns, data, expandoValue); dbCollection.insert(queryDBObject); } }
From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java
License:Open Source License
@Override public List<ExpandoValue> getRowValues(long companyId, long classNameId, String tableName, long classPK, int start, int end) { try {//from ww w.j ava 2 s . co m ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(companyId, classNameId, tableName); DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable); BasicDBObject queryDBObject = new BasicDBObject(); queryDBObject.put("companyId", expandoTable.getCompanyId()); queryDBObject.put("tableId", expandoTable.getTableId()); queryDBObject.put("rowId", classPK); queryDBObject.put("classNameId", expandoTable.getClassNameId()); queryDBObject.put("classPK", classPK); BasicDBObject expandoValueDBObject = (BasicDBObject) dbCollection.findOne(queryDBObject); if (expandoValueDBObject == null) { expandoValueDBObject = queryDBObject; } List<ExpandoColumn> expandoColumns = ExpandoColumnLocalServiceUtil .getColumns(expandoTable.getTableId()); if ((start != QueryUtil.ALL_POS) && (end != QueryUtil.ALL_POS)) { expandoColumns = expandoColumns.subList(start, end); } List<ExpandoValue> expandoValues = new ArrayList<ExpandoValue>(); for (ExpandoColumn expandoColumn : expandoColumns) { ExpandoValue expandoValue = toExpandoValue(expandoValueDBObject, expandoColumn); expandoValues.add(expandoValue); } return expandoValues; } catch (PortalException pe) { throw new SystemException(pe); } }
From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java
License:Open Source License
@Override public ExpandoValue getValue(long companyId, long classNameId, String tableName, String columnName, long classPK) { try {/*from www . jav a2 s. c o m*/ ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(companyId, classNameId, tableName); ExpandoColumn expandoColumn = ExpandoColumnLocalServiceUtil.getColumn(expandoTable.getTableId(), columnName); DBCollection dbCollection = MongoDBUtil.getCollection(expandoTable); DBObject queryDBObject = new BasicDBObject(); queryDBObject.put("companyId", expandoTable.getCompanyId()); queryDBObject.put("tableId", expandoTable.getTableId()); queryDBObject.put("rowId", classPK); queryDBObject.put("classNameId", expandoTable.getClassNameId()); queryDBObject.put("classPK", classPK); BasicDBObject expandoValueDBObject = (BasicDBObject) dbCollection.findOne(queryDBObject); if (expandoValueDBObject == null) { return null; } return toExpandoValue(expandoValueDBObject, expandoColumn); } catch (NoSuchColumnException nsce) { return null; } catch (NoSuchTableException nste) { return null; } catch (PortalException pe) { throw new SystemException(pe); } }
From source file:com.mycompany.bean.PlytaService.java
public Plyta findPlytaByTytul(String tytul) throws Exception { DBCollection collection = getConnection("PlytyDB", "plyty"); BasicDBObject query = new BasicDBObject(); query.append("tytul", tytul); DBObject o = collection.findOne(query); Plyta p = new Plyta(); p.setTytul((String) o.get("tytul")); p.setAutor((List<String>) o.get("autor")); p.setLiczbaUtworow((Integer) o.get("liczbaUtworow")); p.setWytwornia((List<String>) o.get("wytwornia")); p.setRokWydania((Integer) o.get("rokWydania")); p.setProducent((String) o.get("producent")); p.setGatunek((List<String>) o.get("gatunek")); p.setDlugosc((String) o.get("dlugosc")); p.setSingle((List<String>) o.get("single")); p.setNagrody((List<String>) o.get("nagrody")); p.setRodzajAlbumu((String) o.get("rodzajAlbumu")); p.setUtwory((List<Utwor>) o.get("utwory")); return p;/*from w w w.j av a 2 s . c o m*/ }
From source file:com.mythesis.userbehaviouranalysis.ProfileAnalysis.java
License:Apache License
/** * a method that stores the query that has been suggested by the user * @param crawlerOutputPath SWebRank output directory used to check if a relevant query already exists * @param profile the query's relevant profile * @param query the given query//from ww w . j a va 2s.com */ public void storeQuery(String crawlerOutputPath, String profile, String query) { System.out.println(crawlerOutputPath); System.out.println(profile); System.out.println(query); //Find output paths File root = new File(crawlerOutputPath); File[] contents = root.listFiles(); List<String> sWebRanklevels = new ArrayList<>(); for (File f : contents) { if (f.getAbsolutePath().contains("level")) sWebRanklevels.add(f.getAbsolutePath()); } //Find all query paths List<String> queries = new ArrayList<>(); for (String s : sWebRanklevels) { File level = new File(s); File[] queriesFiles = level.listFiles(); for (File f : queriesFiles) { if (!f.getAbsolutePath().contains("txt")) { String str = f.getAbsolutePath(); queries.add(str.substring(str.lastIndexOf("\\") + 1).replace("-query", "").replace("+", " ")); } } } //check if a relevant query already exists - I use Jaro-Winkler distance query = query.trim().replaceAll(" +", " "); for (String q : queries) { JaroWinklerDistance jwd = new JaroWinklerDistance(); double distance = jwd.getDistance(q, query); if (distance > 0.9) { // threshold = 0.9 return; } } Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("profileAnalysis"); DBCollection DBqueries = db.getCollection("newQueries"); BasicDBObject searchQuery = new BasicDBObject(); searchQuery.put("profile", profile); DBObject document = DBqueries.findOne(searchQuery); boolean flag = false; //check if a relevant query exists in the database - I use Jaro-Winkler distance if (document != null) { flag = true; BasicDBList storedQueries = (BasicDBList) document.get("queries"); for (Object quer : storedQueries) { JaroWinklerDistance jwd = new JaroWinklerDistance(); double distance = jwd.getDistance((String) quer, query); if (distance > 0.9) { // threshold = 0.9 return; } } } //if document already exists add the new query if (flag) { DBqueries.update(searchQuery, new BasicDBObject("$push", new BasicDBObject("queries", query))); } else { //otherwise create a new document BasicDBList dbl = new BasicDBList(); dbl.add(query); BasicDBObject entry = new BasicDBObject("profile", profile).append("queries", dbl); DBqueries.insert(entry); } }
From source file:com.redhat.lightblue.metadata.mongo.MetadataCache.java
License:Open Source License
/** * Load the cache version from the db./*from w w w . j a va 2s .c o m*/ */ private synchronized Long loadCacheVersion(DBCollection collection) { BasicDBObject query = new BasicDBObject("_id", "collectionVersion"); DBObject obj = collection.findOne(query); if (obj == null) { updateCollectionVersion(collection); obj = collection.findOne(query); if (obj == null) { // Leave it uninitialized LOGGER.error("Cannot initialize metadata cache"); } } if (obj != null) { return (Long) obj.get("collectionVersion"); } else { return null; } }