List of usage examples for com.mongodb.client FindIterable projection
FindIterable<TResult> projection(@Nullable Bson projection);
From source file:org.apache.nifi.processors.mongodb.GetMongoRecord.java
License:Apache License
@Override public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { FlowFile input = null;/*from w w w . ja va2s . c o m*/ if (context.hasIncomingConnection()) { input = session.get(); if (input == null && context.hasNonLoopConnection()) { return; } } final String database = context.getProperty(DATABASE_NAME).evaluateAttributeExpressions(input).getValue(); final String collection = context.getProperty(COLLECTION_NAME).evaluateAttributeExpressions(input) .getValue(); final String schemaName = context.getProperty(SCHEMA_NAME).evaluateAttributeExpressions(input).getValue(); final Document query = getQuery(context, session, input); MongoCollection mongoCollection = clientService.getDatabase(database).getCollection(collection); FindIterable<Document> find = mongoCollection.find(query); if (context.getProperty(SORT).isSet()) { find = find .sort(Document.parse(context.getProperty(SORT).evaluateAttributeExpressions(input).getValue())); } if (context.getProperty(PROJECTION).isSet()) { find = find.projection( Document.parse(context.getProperty(PROJECTION).evaluateAttributeExpressions(input).getValue())); } if (context.getProperty(LIMIT).isSet()) { find = find.limit(context.getProperty(LIMIT).evaluateAttributeExpressions(input).asInteger()); } MongoCursor<Document> cursor = find.iterator(); FlowFile output = input != null ? session.create(input) : session.create(); final FlowFile inputPtr = input; try { final Map<String, String> attributes = getAttributes(context, input, query, mongoCollection); try (OutputStream out = session.write(output)) { Map<String, String> attrs = inputPtr != null ? inputPtr.getAttributes() : new HashMap<String, String>() { { put("schema.name", schemaName); } }; RecordSchema schema = writerFactory.getSchema(attrs, null); RecordSetWriter writer = writerFactory.createWriter(getLogger(), schema, out, attrs); long count = 0L; writer.beginRecordSet(); while (cursor.hasNext()) { Document next = cursor.next(); if (next.get("_id") instanceof ObjectId) { next.put("_id", next.get("_id").toString()); } Record record = new MapRecord(schema, next); writer.write(record); count++; } writer.finishRecordSet(); writer.close(); out.close(); attributes.put("record.count", String.valueOf(count)); } catch (SchemaNotFoundException e) { throw new RuntimeException(e); } output = session.putAllAttributes(output, attributes); session.getProvenanceReporter().fetch(output, getURI(context)); session.transfer(output, REL_SUCCESS); if (input != null) { session.transfer(input, REL_ORIGINAL); } } catch (Exception ex) { ex.printStackTrace(); getLogger().error("Error writing record set from Mongo query.", ex); session.remove(output); if (input != null) { session.transfer(input, REL_FAILURE); } } }
From source file:org.helm.rest.MongoDB.java
public JSONObject List(String table, String cols, BsonDocument where, BsonDocument sortby, int page, int countperpage) { if (page < 1) page = 1;//ww w .j av a 2 s . c o m if (countperpage < 1) countperpage = 10; long count; FindIterable iter; MongoCollection coll = db.getCollection(table); if (where == null) { count = coll.count(); iter = coll.find(); } else { count = coll.count(where); iter = coll.find(where); } if (sortby != null) iter = iter.sort(sortby); if (cols != null) { String[] ss = cols.split(","); Document fields = new Document("_id", false); for (int i = 0; i < ss.length; ++i) { fields.append(ss[i].trim().toLowerCase(), true); } iter = iter.projection(fields); } long mod = count % countperpage; long pages = (count - mod) / countperpage + (mod == 0 ? 0 : 1); if (page > 1) iter = iter.skip((page - 1) * countperpage); iter = iter.limit(countperpage); MongoCursor cur = iter.iterator(); JSONObject ret = new JSONObject(); ret.put("page", page); ret.put("pages", pages); ret.put("rows", ResultSet2Json(cur)); cur.close(); return ret; }
From source file:org.helm.rest.MongoDB.java
public ArrayList<JSONObject> ReadAsJson(String table, String cols, BsonDocument where) { FindIterable iter; MongoCollection coll = db.getCollection(table); if (where == null) { iter = coll.find();// w w w .j ava 2 s .c o m } else { iter = coll.find(where); } if (cols != null) { String[] ss = cols.split(","); Document fields = new Document("_id", false); for (int i = 0; i < ss.length; ++i) { fields.append(ss[i].trim().toLowerCase(), true); } iter = iter.projection(fields); } MongoCursor cur = iter.iterator(); if (cur == null || !cur.hasNext()) return null; return ResultSet2Json(cur); }
From source file:org.helm.rest.MongoDB.java
public String ReadAsSDF(String table, String molfilekey) { MongoCollection coll = db.getCollection(table); FindIterable iter = coll.find(); String lb = System.getProperty("line.separator"); molfilekey = molfilekey.toLowerCase(); StringBuilder sb = new StringBuilder(); if (iter == null) { return null; } else {//w w w. ja v a2 s . com Document fields = new Document("_id", false); iter = iter.projection(fields); } MongoCursor cur = iter.iterator(); while (cur.hasNext()) { Document doc = (Document) cur.next(); String m = doc.getString(molfilekey); if (m != null) { // the molfile from toolkit has extra $$$$ line // fix bug: https://github.com/PistoiaHELM/HELMWebEditor/issues/94 int p = m.lastIndexOf("M END") + 6; if (p > 6 && p < m.length() - 1) m = m.substring(0, p); } else { m = lb + " JSDraw203101711402D" + lb + lb + " 0 0 0 0 0 0 0 V2000" + lb + "M END"; } sb.append(m); sb.append(lb); for (String k : doc.keySet()) { if (k.equals(molfilekey) || k.equals("_id")) continue; sb.append("> <"); sb.append(k); sb.append(">"); sb.append(lb); String s = doc.get(k) + ""; sb.append(s == null ? "" : s); sb.append(lb); sb.append(lb); } sb.append("$$$$"); sb.append(lb); } return sb.toString(); }
From source file:org.iu.sead.cloud.ROSearch.java
License:Apache License
private void setROProjection(FindIterable<Document> iter) { iter.projection(new Document("Status", 1).append("Repository", 1).append("Aggregation.Identifier", 1) .append("Aggregation.Creator", 1).append("Aggregation.Title", 1).append("Aggregation.Contact", 1) .append("Aggregation.Abstract", 1) // .append("Aggregation.Creation Date", 1) .append("_id", 0)); }
From source file:org.netbeans.modules.mongodb.ui.QueryWorker.java
License:Open Source License
@Override protected QueryResult createQuery() throws Exception { FindIterable<BsonDocument> query = filter != null ? collection.find(filter) : collection.find(); query = query.projection(projection).sort(sort); long size = filter != null ? collection.count(filter) : collection.count(); return new QueryResult.MongoCursorResult(query.iterator(), this, size); }
From source file:org.seadpdt.impl.PeopleServicesImpl.java
License:Apache License
@GET @Path("/") @Produces(MediaType.APPLICATION_JSON)//from w w w .j av a 2s .c o m public Response getPeopleList() { FindIterable<Document> iter = peopleCollection.find(); iter.projection(getBasicPersonProjection()); MongoCursor<Document> cursor = iter.iterator(); ArrayList<Object> array = new ArrayList<Object>(); while (cursor.hasNext()) { Document next = cursor.next(); array.add(next); } Document peopleDocument = new Document(); peopleDocument.put("persons", array); peopleDocument.put("@context", getPersonContext()); return Response.ok(peopleDocument.toJson()).cacheControl(control).build(); }
From source file:org.seadpdt.impl.PeopleServicesImpl.java
License:Apache License
@GET @Path("/list/") @Produces(MediaType.APPLICATION_JSON)// w w w. jav a 2s . c o m public Response getPeopleListAsArray() { FindIterable<Document> iter = peopleCollection.find(); iter.projection(getBasicPersonProjection()); MongoCursor<Document> cursor = iter.iterator(); JSONArray array = new JSONArray(); while (cursor.hasNext()) { Document next = cursor.next(); next.put("@context", getPersonContext()); array.put(next); } return Response.ok(array.toString()).cacheControl(control).build(); }
From source file:org.seadpdt.impl.PeopleServicesImpl.java
License:Apache License
static Document retrieveProfile(String canonicalID) { Document document = null;//from www.j a va 2s . c o m FindIterable<Document> iter = peopleCollection.find(new Document("@id", canonicalID)); iter.projection(getBasicPersonProjection()); if (iter.first() != null) { document = iter.first(); document.put("@context", getPersonContext()); } return document; }
From source file:org.seadpdt.impl.RepoServicesImpl.java
License:Apache License
@GET @Path("/") @Produces(MediaType.APPLICATION_JSON)//from w w w .j av a2 s . c o m public Response getRepositoryList() { FindIterable<Document> iter = repositoriesCollection.find(); iter.projection(new Document("orgidentifier", 1).append("repositoryURL", 1).append("repositoryName", 1) .append("lastUpdate", 1).append("_id", 0)); MongoCursor<Document> cursor = iter.iterator(); JSONArray array = new JSONArray(); while (cursor.hasNext()) { array.put(new JSONObject(cursor.next().toJson())); } return Response.ok(array.toString()).cacheControl(control).build(); }