List of usage examples for com.mongodb QueryBuilder start
public static QueryBuilder start()
From source file:com.bosscs.spark.mongodb.config.MongoDeepJobConfig.java
License:Apache License
/** * Filter query.//from ww w .ja v a 2 s . co m * * @param filters the filters * @return the mongo deep job config */ public MongoDeepJobConfig<T> filterQuery(Filter[] filters) { if (filters.length > 0) { List<BasicDBObject> list = new ArrayList<>(); QueryBuilder queryBuilder = QueryBuilder.start(); for (int i = 0; i < filters.length; i++) { BasicDBObject bsonObject = new BasicDBObject(); Filter filter = filters[i]; if (filter.getFilterType().equals(FilterType.EQ)) { bsonObject.put(filter.getField(), filter.getValue()); } else { bsonObject.put(filter.getField(), new BasicDBObject( "$".concat(filter.getFilterType().getFilterTypeId().toLowerCase()), filter.getValue())); } list.add(bsonObject); } queryBuilder.and(list.toArray(new BasicDBObject[list.size()]).toString()); filterQuery(queryBuilder); } return this; }
From source file:com.bosscs.spark.mongodb.reader.MongoReader.java
License:Apache License
/** * Generate filter query.//from w w w .ja v a 2 s . c o m * * @param partition the partition * @return the dB object */ private DBObject generateFilterQuery(MongoPartition partition) { if (mongoDeepJobConfig.getQuery() != null) { QueryBuilder queryBuilder = QueryBuilder.start(); queryBuilder.and(createQueryPartition(partition).toString() + mongoDeepJobConfig.getQuery()); LOG.debug("mongodb query " + queryBuilder.get()); return queryBuilder.get(); } return createQueryPartition(partition); }
From source file:com.callidusrobotics.droptables.model.DocumentDao.java
License:Open Source License
/** * Finds documents containing the desired key,value pair. * * @param collectionName/*from ww w . ja v a 2 s . c om*/ * The collection to search * @param key * The document key to select on * @param value * The desired value * @return A list of matching documents, never null */ public List<DBObject> getDocumentsByField(String collectionName, String key, String value) { DBCollection collection = getCollection(collectionName); DBObject query = QueryBuilder.start().and(key).is(value).get(); DBCursor cursor = collection.find(query); return getDocuments(cursor); }
From source file:com.gigaspaces.persistency.MongoSpaceDataSource.java
License:Open Source License
@Override public DataIterator<Object> getDataIteratorByIds(DataSourceIdsQuery idsQuery) { if (logger.isDebugEnabled()) logger.debug("MongoSpaceDataSource.getDataIteratorByIds(" + idsQuery + ")"); DBObject[] ors = new DBObject[idsQuery.getIds().length]; for (int i = 0; i < ors.length; i++) ors[i] = BasicDBObjectBuilder.start().add(Constants.ID_PROPERTY, idsQuery.getIds()[i]).get(); DBObject document = QueryBuilder.start().or(ors).get(); DBCollection mongoCollection = mongoClient.getCollection(idsQuery.getTypeDescriptor().getTypeName()); DBCursor results = mongoCollection.find(document); return new DefaultMongoDataIterator(results, idsQuery.getTypeDescriptor()); }
From source file:com.gigaspaces.persistency.parser.SQL2MongoBaseVisitor.java
License:Open Source License
public T visitOr(SQL2MongoParser.OrContext ctx) { T r = visitChildren(ctx);/* ww w . ja v a 2 s. c o m*/ if (IsLogic(ctx, "OR")) { QueryBuilder q = QueryBuilder.start(); for (DBObject at : ands) q.or(at); Set<String> keys = atom.get().keySet(); for (String key : keys) { q.or(new BasicDBObject(key, atom.get().get(key))); } ands.clear(); atom = QueryBuilder.start(); ors.add(q.get()); } return r; }
From source file:com.gigaspaces.persistency.parser.SQL2MongoBaseVisitor.java
License:Open Source License
public T visitAnd(SQL2MongoParser.AndContext ctx) { T r = visitChildren(ctx);//from w w w. ja va 2 s .c o m if (IsLogic(ctx, "AND")) { ands.add(atom.get()); atom = QueryBuilder.start(); } return r; }
From source file:com.grallandco.mongodb.beersample.BeerServlet.java
License:Open Source License
/** * Handle the /beers/search action./*from w w w . ja v a 2s .c o m*/ * * @param request the HTTP request object. * @param response the HTTP response object. * @throws java.io.IOException * @throws javax.servlet.ServletException */ private void handleSearch(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String name = request.getParameter("value").toLowerCase(); DBObject query = QueryBuilder.start().put("name") .regex(java.util.regex.Pattern.compile(name, Pattern.CASE_INSENSITIVE)).get(); DBCursor cursor = db.getCollection("beer").find(query).sort(BasicDBObjectBuilder.start("name", 1).get()) .limit(20); ArrayList<HashMap<String, String>> beers = new ArrayList<HashMap<String, String>>(); while (cursor.hasNext()) { DBObject row = cursor.next(); HashMap<String, String> beer = new HashMap<String, String>(); beer.put("id", (String) row.get("_id")); beer.put("name", (String) row.get("name")); beer.put("brewery", (String) row.get("brewery_id")); beers.add(beer); } response.setContentType("application/json"); PrintWriter out = response.getWriter(); out.print(gson.toJson(beers)); out.flush(); }
From source file:com.grallandco.mongodb.beersample.BreweryServlet.java
License:Open Source License
/** * Handle the /breweries/search action./*from ww w. jav a 2 s. c om*/ * * @param request the HTTP request object. * @param response the HTTP response object. * @throws java.io.IOException * @throws javax.servlet.ServletException */ private void handleSearch(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String name = request.getParameter("value").toLowerCase(); DBObject query = QueryBuilder.start().put("name") .regex(java.util.regex.Pattern.compile(name, Pattern.CASE_INSENSITIVE)).get(); DBCursor cursor = db.getCollection("brewery").find(query).sort(BasicDBObjectBuilder.start("name", 1).get()) .limit(20); ArrayList<HashMap<String, String>> breweries = new ArrayList<HashMap<String, String>>(); while (cursor.hasNext()) { DBObject row = cursor.next(); HashMap<String, String> brewery = new HashMap<String, String>(); brewery.put("id", (String) row.get("_id")); brewery.put("name", (String) row.get("name")); breweries.add(brewery); } response.setContentType("application/json"); PrintWriter out = response.getWriter(); out.print(gson.toJson(breweries)); out.flush(); }
From source file:com.hw3dot2.BlogPostDAO.java
License:Apache License
/** * Return a list of DBObjects, each one a post from the posts collection * * @param limit amount of returned posts * @return/* www .jav a 2 s .com*/ */ public List<DBObject> findByDateDescending(int limit) { List<DBObject> posts = new ArrayList<DBObject>(); DBObject query = QueryBuilder.start().get(); DBCursor cursor = postsCollection.find(query).sort(new BasicDBObject("date", -1)).limit(10); while (cursor.hasNext()) { posts.add(cursor.next()); } // return posts; }
From source file:com.linuxbox.enkive.audit.mongodb.MongoAuditService.java
License:Open Source License
/** * If there is a username, then we hint to use that index, as we expect * there to be many users compared to event codes. Thus it is likely that * generally filtering by users will reduce the result set by the greatest * amount. Of course some event codes are more prevalent than others, so * this won't always be the case./* ww w . jav a2 s . co m*/ * * @param startTime * all results must occur ON or AFTER this timestamp * @param endTime * all results must occur BEFORE this timestamp * @throws AuditServiceException */ @Override public List<AuditEntry> search(Integer eventCode, String userIdentifier, Date startTime, Date endTime) throws AuditServiceException { final QueryBuilder qb = QueryBuilder.start(); String indexHint = TIMESTAMP_INDEX; if (eventCode != null) { qb.put(CODE_FIELD).is(eventCode); indexHint = CODE_TIMESTAMP_INDEX; } if (userIdentifier != null) { qb.put(USERNAME_FIELD).is(userIdentifier); indexHint = CODE_TIMESTAMP_INDEX; } if (startTime != null) { final int epochTime = (int) (startTime.getTime() / 1000); final BSONTimestamp timestamp = new BSONTimestamp(epochTime, 0); qb.put(TIMESTAMP_FIELD).greaterThanEquals(timestamp); } if (endTime != null) { final int epochTime = (int) (Math.ceil(endTime.getTime() / 1000.0)); final BSONTimestamp timestamp = new BSONTimestamp(epochTime, 0); qb.put(TIMESTAMP_FIELD).lessThan(timestamp); } final DBCursor cursor = auditCollection.find(qb.get()).sort(DATE_BACKWARD_SORT).hint(indexHint); return dbCursortoAuditEntryList(cursor); }