List of usage examples for com.mongodb DBObject keySet
Set<String> keySet();
From source file:eu.delving.services.core.impl.RecordImpl.java
License:EUPL
@Override public Map<String, Integer> getFingerprint() { Map<String, Integer> fingerprint = new TreeMap<String, Integer>(); DBObject hash = getHash(); for (String key : hash.keySet()) { String path = (String) hash.get(key); int count = collection.find(mob(String.format("%s.%s", HASH, key), mob("$exists", true))).count(); fingerprint.put(path, count);// www .j av a 2s . c om } return fingerprint; }
From source file:ezbake.data.mongo.EzMongoHandler.java
License:Apache License
/** * Create MongoDB indexes for the given fields in some collection. * * @param collectionName The Name of the collection the document belongs to * @param jsonKeys The Mongo index keys//from w ww . ja v a2s . com * @param jsonOptions Optional options for the particular fields being indexed. * @param security A valid EzSecurity token * @throws TException If authentication fails * @throws EzMongoBaseException If some other error occurs */ @Override public void createIndex(String collectionName, String jsonKeys, String jsonOptions, EzSecurityToken security) throws TException, EzMongoBaseException { try { HashMap<String, String> auditParamsMap = new HashMap<>(); auditParamsMap.put("action", "createIndex"); auditParamsMap.put("collectionName", collectionName); auditParamsMap.put("jsonKeys", jsonKeys); auditParamsMap.put("jsonOptions", jsonOptions); auditLog(security, AuditEventType.FileObjectCreate, auditParamsMap); TokenUtils.validateSecurityToken(security, this.getConfigurationProperties()); if (StringUtils.isEmpty(collectionName)) { throw new EzMongoBaseException("collectionName is required."); } final String finalCollectionName = getCollectionName(collectionName); final DBObject indexKeys = (DBObject) JSON.parse(jsonKeys); // The 'options' object is optional if (StringUtils.isEmpty(jsonOptions)) { db.getCollection(finalCollectionName).createIndex(indexKeys); } else { final DBObject indexOptions = (DBObject) JSON.parse(jsonOptions); // check if "ns" and "name" fields are not in the jsonOptions - then need to put them in. // otherwise, mongodb throws this error: // "Cannot authorize inserting into system.indexes documents without a string-typed \"ns\" field." String ns = (String) indexOptions.get("ns"); if (ns == null) { ns = dbName + "." + finalCollectionName; indexOptions.put("ns", ns); appLog.info("putting index's ns as : {}", ns); } String name = (String) indexOptions.get("name"); if (name == null) { name = ""; final Set<String> keySet = indexKeys.keySet(); for (final String key : keySet) { final Object keyValue = indexKeys.get(key); if (name.length() > 0) { name += "_"; } name += key + "_" + keyValue.toString(); } indexOptions.put("name", name); appLog.info("putting index's name as : {}", name); } db.getCollection(finalCollectionName).createIndex(indexKeys, indexOptions); } appLog.info("created index with keys: {}, options: {}", jsonKeys, jsonOptions); } catch (final Exception e) { throw enrichException("createIndex", e); } }
From source file:ezbake.data.mongo.HandlerForDriverFindCalls.java
License:Apache License
protected QueryResultIterator convertFindForDriver(String collectionName, DBObject jsonQuery, DBObject projection, String jsonSort, int skip, int limit, int batchSize, ReadPreference readPref, EzSecurityToken token, String operationType) throws Exception { appLog.info("convertFindForDriver() query: " + jsonQuery); AggregationOptions opts = null;/*w ww . j a va 2 s . c o m*/ if (batchSize > 0) { opts = AggregationOptions.builder().outputMode(AggregationOptions.OutputMode.CURSOR) .batchSize(batchSize).build(); } else { opts = AggregationOptions.builder().build(); } Object distinct = jsonQuery.get("distinct"); Object key = null; if (distinct != null) { key = jsonQuery.get("key"); Object q = jsonQuery.get("query"); if (q != null) { jsonQuery = (DBObject) q; } } jsonQuery = checkForQueryComment(jsonQuery); jsonQuery = checkForshowDiskLoc(jsonQuery); Object returnKey = jsonQuery.get("$returnKey"); if (returnKey != null) { Object q = jsonQuery.get("$query"); if (q != null) { jsonQuery = (DBObject) q; } } Object snapshot = jsonQuery.get("$snapshot"); if (snapshot != null) { Object ob = jsonQuery.get("$orderby"); if (ob != null) { throw new MongoException("Do not use $snapshot with cursor.hint() and cursor.sort() methods"); } Object hint = jsonQuery.get("$hint"); if (hint != null) { throw new MongoException("Do not use $snapshot with cursor.hint() and cursor.sort() methods"); } Object q = jsonQuery.get("$query"); if (q != null) { jsonQuery = (DBObject) q; } } Object explain = jsonQuery.get("$explain"); if (explain != null) { Object q = jsonQuery.get("$query"); if (q != null) { jsonQuery = (DBObject) q; } } Object orderby = jsonQuery.get("$orderby"); if (orderby != null) { Object q = jsonQuery.get("$query"); if (q != null) { jsonQuery = (DBObject) q; } jsonSort = orderby.toString(); } Object maxScan = jsonQuery.get("$maxScan"); if (maxScan != null) { Object q = jsonQuery.get("$query"); if (q != null) { jsonQuery = (DBObject) q; } limit = (Integer) maxScan; } Object min = jsonQuery.get("$min"); if (min != null) { Object q = jsonQuery.get("$query"); if (q != null) { jsonQuery = (DBObject) q; } } Object max = jsonQuery.get("$max"); if (max != null) { Object q = jsonQuery.get("$query"); if (q != null) { jsonQuery = (DBObject) q; } } QueryResultIterator qri = null; DBObject query = null; if (jsonQuery != null && jsonQuery.keySet().size() > 0) { query = new BasicDBObject("$match", jsonQuery); } DBObject[] additionalOps = parent_handler.handler.getMongoFindHelper().getFindAggregationCommandsArray(skip, limit, (projection != null && projection.keySet().size() > 0) ? projection.toString() : "", jsonSort, token, operationType); List<DBObject> pipeline = new ArrayList<DBObject>(); if (query != null) { pipeline.add(query); } Collections.addAll(pipeline, additionalOps); appLog.info("convertFindForDriver() final pipeline query: " + pipeline); Cursor cursor = null; if (distinct != null) { qri = handleDistinctCall(jsonQuery, readPref, token, opts, distinct, key, pipeline); } else if (max != null && min != null) { // TODO can max AND min be possible? investigate... } else if (max != null) { qri = handleMaxCall(collectionName, max, jsonQuery, readPref, token, opts, pipeline); } else if (min != null) { qri = handleMinCall(collectionName, min, jsonQuery, readPref, token, opts, pipeline); } else { cursor = parent_handler.handler.db.getCollection(collectionName).aggregate(pipeline, opts, readPref); if (cursor instanceof QueryResultIterator) { qri = (QueryResultIterator) cursor; } else { appLog.info("UNKNOWN CURSOR RETURNED: {}", cursor.toString()); throw new Exception("Find converted to Aggregate pipeline did not return a QueryResultIterator: " + cursor.toString()); } } return qri; }
From source file:fr.cnes.sitools.dataset.database.mongodb.RequestMongoDB.java
License:Open Source License
@Override public String getFilterClause(List<Predicat> predicats, List<Column> columns) { BasicDBObject whereClause = new BasicDBObject(); // loop over the predicats // boolean first = true; // String result = "{"; ///*w w w . j a v a 2 s. c o m*/ // Map<String, List<Predicat>> orderedPredicats = orderPredicat(predicats); // for (Predicat predicat : predicats) { // // String filter = getFilter(predicat); // if (filter != null && !"".equals(filter)) { // // DBObject objPredicat = (DBObject) JSON.parse(filter); // // if (objPredicat != null) { // if (first) { // whereClause.append("$and", new ArrayList<DBObject>()); // } // else { // result += ","; // } // first = false; // // result += filter; // // // ((List<DBObject>) whereClause.get("$and")).add(objPredicat); // // // if (whereClause.containsField(key)) { // // // If the key already exists append the value to the existing key // // // DBObject obj = new BasicDBObject(); // // // obj.put("$and", objPredicat.get(key)); // // // // if (!whereClause.containsField("$and")) { // // whereClause.append("$and", new ArrayList<DBObject>()); // // DBObject pred = (DBObject) whereClause.get(key); // // whereClause.remove(key); // // ((List<DBObject>) whereClause.get("$and")).add(pred); // // // // } // // ((List<DBObject>) whereClause.get("$and")).add(objPredicat); // // // // // ((DBObject) whereClause.get(key)).putAll(obj); // // } // // else { // // // if the key doesn't exists just append the predicat to the whereClause // // whereClause.append(key, objPredicat.get(key)); // // } // // } // // } // } for (Predicat predicat : predicats) { String filter = getFilter(predicat); if (filter != null && !"".equals(filter)) { DBObject objPredicat = (DBObject) JSON.parse(filter); if (objPredicat != null) { Set<String> keys = objPredicat.keySet(); for (String key : keys) { if (whereClause.containsField(key)) { ((DBObject) whereClause.get(key)).putAll((DBObject) objPredicat.get(key)); } else { whereClause.append(key, objPredicat.get(key)); } } } } } return whereClause.toString(); }
From source file:fr.cnes.sitools.datasource.mongodb.business.SitoolsMongoDBDataSource.java
License:Open Source License
/** * Get the list of fields of a collection * /*from www. ja va2s . c om*/ * @param collectionName * the name of the mongoDB collection * @return name of related columns of the table TODO evolution return List<Column> */ // TODO public List<String> getMetadata(String collectionName) { List<String> columnNameList = null; DB mongoDatabase = null; mongoDatabase = getDatabase(); columnNameList = new ArrayList<String>(); DBCollection collection = mongoDatabase.getCollection(collectionName); DBObject dbObject = collection.findOne(); columnNameList.addAll(dbObject.keySet()); return columnNameList; }
From source file:fr.cnes.sitools.datasource.mongodb.dbexplorer.MongoDBExplorerResource.java
License:Open Source License
/** * Trace informations for a sitoolsDataSource * //from w w w. j a v a2s . c o m * @param object * The DBObject to trace * @param messages * ArrayList<String> messages */ private void traceObjects(DBObject object, List<String> messages) { for (Object key : object.keySet()) { messages.add(key + ": " + object.get(key.toString())); } }
From source file:gr.forth.ics.icardea.pid.PrimKV.java
public static iCARDEA_Patient create_from_DBObject(DBObject o) { iCARDEA_Patient tr = new iCARDEA_Patient(); for (String k : o.keySet()) { if ("name".equals(k)) tr.name.fill_from_DBObject((DBObject) o.get(k)); else if ("mothers_name".equals(k)) tr.mothers_name.fill_from_DBObject((DBObject) o.get(k)); else if ("addr".equals(k)) tr.addr.fill_from_DBObject((DBObject) o.get(k)); else if ("date_of_birth".equals(k)) tr.date_of_birth = (String) o.get(k); else if ("sex".equals(k)) tr.sex = (String) o.get(k); else if ("ssn".equals(k)) tr.ssn = (String) o.get(k); else if ("tel_home".equals(k)) tr.tel_home = (String) o.get(k); else if ("tel_work".equals(k)) tr.tel_work = (String) o.get(k); else if ("drivers_lic".equals(k)) tr.drivers_lic = (String) o.get(k); else if ("accnum".equals(k)) tr.accnum = (String) o.get(k); else if ("ids".equals(k)) { for (DBObject i : (List<DBObject>) o.get(k)) { String ns = (String) (i.containsField("namespace") ? i.get("namespace") : null); String id = (String) (i.containsField("id") ? i.get("id") : null); tr.ids.add(new ID(ns, id)); }//from w w w. j a va 2 s .c o m } else if (k.startsWith(ID_PREFIX)) { String ns = k.substring(ID_PREFIX.length()); String id = (String) o.get(k); tr.ids.add(new ID(ns, id)); } } return tr; }
From source file:io.liveoak.mongo.gridfs.GridFSResource.java
License:Open Source License
public void readFileInfo(PropertySink sink) { DBObject dbobj = fileInfo().dbObject(); for (String key : dbobj.keySet()) { if (getFiltered().contains(key)) { continue; }//w w w . j a v a2 s. c o m Object val = dbobj.get(key); if (val instanceof Date) { val = ((Date) val).getTime(); } else if (val instanceof ObjectId) { val = val.toString(); } if ("uploadDate".equals(key)) { key = "createDate"; } if (val != null) { sink.accept(key, val); } } }
From source file:io.liveoak.mongo.MongoBaseObjectResource.java
License:Open Source License
@Override public Map<String, ?> properties(RequestContext ctx) throws Exception { // TODO: only read properties specified in the return fields and not everything Map<String, Object> result = new HashMap<>(); ReturnFields returnFields = ctx.returnFields(); DBObject dbObject = getDBObject(); if (dbObject == null) { throw new ResourceProcessingException("Could not find object with ID: " + this.id()); }//ww w.ja v a 2 s . co m Set<String> keys = dbObject.keySet(); for (String key : keys) { if (!key.equals(MONGO_ID_FIELD) && !key.equals(LiveOak.ID)) { Object value = getDBObject().get(key); if (value instanceof BasicDBObject) { value = new MongoEmbeddedObjectResource(this, (DBObject) value); } else if (value instanceof BasicDBList) { value = getResourceCollection(value); } else if (value instanceof DBRef) { value = getResource((DBRef) value, returnFields.child(key).isEmpty()); } if (supportedObject(value)) { result.put(key, value); } else { log.warn("Unsupported Property type " + value.getClass() + " cannot encode."); } } } return result; }
From source file:io.liveoak.security.policy.acl.impl.AclPolicy.java
License:Open Source License
private ResourceState createACE(String createdResourceURI, SecurityContext securityContext, AutoRuleConfig autoRuleConfig) { DBObject dbObject = new BasicDBObject(); dbObject.put(ACE_REALM, securityContext.getRealm()); dbObject.put(ACE_USER_ID, securityContext.getSubject()); dbObject.put(ACE_RESOURCE_PATH, createdResourceURI); dbObject.put(ACE_ACTIONS, autoRuleConfig.getAutoAddedOwnerPermissions().toArray()); dbObject.put(ACE_PERMITTED, true);/*from w w w. ja v a 2 s . co m*/ this.aclCollection.insert(dbObject); log.debug("Created ACE: " + dbObject); ResourceState createdState = new DefaultResourceState(); for (String key : dbObject.keySet()) { createdState.putProperty(key, dbObject.get(key)); } return createdState; }