List of usage examples for com.mongodb DBObject keySet
Set<String> keySet();
From source file:org.springframework.data.mongodb.core.aggregation.ConditionalOperator.java
License:Apache License
private List<Object> getClauses(AggregationOperationContext context, String key, Object predicate) { List<Object> clauses = new ArrayList<Object>(); if (predicate instanceof List) { List<Object> args = new ArrayList<Object>(); for (Object clause : (List<?>) predicate) { if (clause instanceof DBObject) { args.addAll(getClauses(context, (DBObject) clause)); }//from w w w . j a v a 2s .c o m } clauses.add(new BasicDBObject(key, args)); } else if (predicate instanceof DBObject) { DBObject nested = (DBObject) predicate; for (String s : nested.keySet()) { if (!isKeyword(s)) { continue; } List<Object> args = new ArrayList<Object>(); args.add("$" + key); args.add(nested.get(s)); clauses.add(new BasicDBObject(s, args)); } } else if (!isKeyword(key)) { List<Object> args = new ArrayList<Object>(); args.add("$" + key); args.add(predicate); clauses.add(new BasicDBObject("$eq", args)); } return clauses; }
From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java
License:Apache License
/** * Reads the given {@link DBObject} into a {@link Map}. will recursively resolve nested {@link Map}s as well. * /*w ww . j a v a 2s . co m*/ * @param type the {@link Map} {@link TypeInformation} to be used to unmarshall this {@link DBObject}. * @param dbObject must not be {@literal null} * @param path must not be {@literal null} * @return */ @SuppressWarnings("unchecked") protected Map<Object, Object> readMap(TypeInformation<?> type, DBObject dbObject, ObjectPath path) { Assert.notNull(dbObject, "DBObject must not be null!"); Assert.notNull(path, "Object path must not be null!"); Class<?> mapType = typeMapper.readType(dbObject, type).getType(); TypeInformation<?> keyType = type.getComponentType(); Class<?> rawKeyType = keyType == null ? null : keyType.getType(); TypeInformation<?> valueType = type.getMapValueType(); Class<?> rawValueType = valueType == null ? null : valueType.getType(); Map<Object, Object> map = CollectionFactory.createMap(mapType, rawKeyType, dbObject.keySet().size()); Map<String, Object> sourceMap = dbObject.toMap(); for (Entry<String, Object> entry : sourceMap.entrySet()) { if (typeMapper.isTypeKey(entry.getKey())) { continue; } Object key = potentiallyUnescapeMapKey(entry.getKey()); if (rawKeyType != null) { key = conversionService.convert(key, rawKeyType); } Object value = entry.getValue(); if (value instanceof DBObject) { map.put(key, read(valueType, (DBObject) value, path)); } else if (value instanceof DBRef) { map.put(key, DBRef.class.equals(rawValueType) ? value : readAndConvertDBRef((DBRef) value, valueType, ObjectPath.ROOT, rawValueType)); } else { Class<?> valueClass = valueType == null ? null : valueType.getType(); map.put(key, getPotentiallyConvertedSimpleRead(value, valueClass)); } } return map; }
From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java
License:Apache License
/** * Removes the type information from the entire conversion result. * /* w w w . j a va2 s .c o m*/ * @param object * @param recursively whether to apply the removal recursively * @return */ private Object removeTypeInfo(Object object, boolean recursively) { if (!(object instanceof DBObject)) { return object; } DBObject dbObject = (DBObject) object; String keyToRemove = null; for (String key : dbObject.keySet()) { if (recursively) { Object value = dbObject.get(key); if (value instanceof BasicDBList) { for (Object element : (BasicDBList) value) { removeTypeInfo(element, recursively); } } else { removeTypeInfo(value, recursively); } } if (typeMapper.isTypeKey(key)) { keyToRemove = key; if (!recursively) { break; } } } if (keyToRemove != null) { dbObject.removeField(keyToRemove); } return dbObject; }
From source file:org.springframework.data.mongodb.core.convert.MongoExampleMapper.java
License:Apache License
private static DBObject orConcatenate(DBObject source) { List<DBObject> foo = new ArrayList<DBObject>(source.keySet().size()); for (String key : source.keySet()) { foo.add(new BasicDBObject(key, source.get(key))); }/* w w w .ja va2 s .c o m*/ return new BasicDBObject("$or", foo); }
From source file:org.springframework.data.mongodb.core.convert.QueryMapper.java
License:Apache License
/** * Replaces the property keys used in the given {@link DBObject} with the appropriate keys by using the * {@link PersistentEntity} metadata./*from w ww. j av a 2 s. c o m*/ * * @param query must not be {@literal null}. * @param entity can be {@literal null}. * @return */ @SuppressWarnings("deprecation") public DBObject getMappedObject(DBObject query, MongoPersistentEntity<?> entity) { if (isNestedKeyword(query)) { return getMappedKeyword(new Keyword(query), entity); } DBObject result = new BasicDBObject(); for (String key : query.keySet()) { // TODO: remove one once QueryMapper can work with Query instances directly if (Query.isRestrictedTypeKey(key)) { @SuppressWarnings("unchecked") Set<Class<?>> restrictedTypes = (Set<Class<?>>) query.get(key); this.converter.getTypeMapper().writeTypeRestrictions(result, restrictedTypes); continue; } if (isKeyword(key)) { result.putAll(getMappedKeyword(new Keyword(query, key), entity)); continue; } try { Field field = createPropertyField(entity, key, mappingContext); Entry<String, Object> entry = getMappedObjectForField(field, query.get(key)); result.put(entry.getKey(), entry.getValue()); } catch (InvalidPersistentPropertyPath invalidPathException) { // in case the object has not already been mapped if (!(query.get(key) instanceof DBObject)) { throw invalidPathException; } result.put(key, query.get(key)); } } return result; }
From source file:org.springframework.data.mongodb.core.convert.QueryMapper.java
License:Apache License
/** * Maps fields to retrieve to the {@link MongoPersistentEntity}s properties. <br /> * Also onverts and potentially adds missing property {@code $meta} representation. * //from ww w . j av a 2 s . c o m * @param fieldsObject * @param entity * @return * @since 1.6 */ public DBObject getMappedFields(DBObject fieldsObject, MongoPersistentEntity<?> entity) { DBObject mappedFields = fieldsObject != null ? getMappedObject(fieldsObject, entity) : new BasicDBObject(); mapMetaAttributes(mappedFields, entity, MetaMapping.FORCE); return mappedFields.keySet().isEmpty() ? null : mappedFields; }
From source file:org.springframework.data.mongodb.core.convert.QueryMapper.java
License:Apache License
/** * Converts the given source assuming it's actually an association to another object. * //from w w w . j a v a2s .c o m * @param source * @param property * @return */ protected Object convertAssociation(Object source, MongoPersistentProperty property) { if (property == null || source == null || source instanceof DBObject) { return source; } if (source instanceof DBRef) { DBRef ref = (DBRef) source; return new DBRef(ref.getCollectionName(), convertId(ref.getId())); } if (source instanceof Iterable) { BasicDBList result = new BasicDBList(); for (Object element : (Iterable<?>) source) { result.add(createDbRefFor(element, property)); } return result; } if (property.isMap()) { BasicDBObject result = new BasicDBObject(); DBObject dbObject = (DBObject) source; for (String key : dbObject.keySet()) { result.put(key, createDbRefFor(dbObject.get(key), property)); } return result; } return createDbRefFor(source, property); }
From source file:org.springframework.data.mongodb.core.DefaultIndexOperations.java
License:Apache License
public List<IndexInfo> getIndexInfo() { return mongoOperations.execute(collectionName, new CollectionCallback<List<IndexInfo>>() { public List<IndexInfo> doInCollection(DBCollection collection) throws MongoException, DataAccessException { List<DBObject> dbObjectList = collection.getIndexInfo(); return getIndexData(dbObjectList); }//w w w . j a va 2 s . co m private List<IndexInfo> getIndexData(List<DBObject> dbObjectList) { List<IndexInfo> indexInfoList = new ArrayList<IndexInfo>(); for (DBObject ix : dbObjectList) { DBObject keyDbObject = (DBObject) ix.get("key"); int numberOfElements = keyDbObject.keySet().size(); List<IndexField> indexFields = new ArrayList<IndexField>(numberOfElements); for (String key : keyDbObject.keySet()) { Object value = keyDbObject.get(key); if (TWO_D_IDENTIFIERS.contains(value)) { indexFields.add(IndexField.geo(key)); } else if ("text".equals(value)) { DBObject weights = (DBObject) ix.get("weights"); for (String fieldName : weights.keySet()) { indexFields.add(IndexField.text(fieldName, Float.valueOf(weights.get(fieldName).toString()))); } } else { Double keyValue = new Double(value.toString()); if (ONE.equals(keyValue)) { indexFields.add(IndexField.create(key, ASC)); } else if (MINUS_ONE.equals(keyValue)) { indexFields.add(IndexField.create(key, DESC)); } } } String name = ix.get("name").toString(); boolean unique = ix.containsField("unique") ? (Boolean) ix.get("unique") : false; boolean dropDuplicates = ix.containsField("dropDups") ? (Boolean) ix.get("dropDups") : false; boolean sparse = ix.containsField("sparse") ? (Boolean) ix.get("sparse") : false; String language = ix.containsField("default_language") ? (String) ix.get("default_language") : ""; indexInfoList.add(new IndexInfo(indexFields, name, unique, dropDuplicates, sparse, language)); } return indexInfoList; } }); }
From source file:org.springframework.data.mongodb.core.index.IndexInfo.java
License:Apache License
/** * Creates new {@link IndexInfo} parsing required properties from the given {@literal sourceDocument}. * * @param sourceDocument// ww w .j av a 2s . co m * @return * @since 1.10 */ public static IndexInfo indexInfoOf(DBObject sourceDocument) { DBObject keyDbObject = (DBObject) sourceDocument.get("key"); int numberOfElements = keyDbObject.keySet().size(); List<IndexField> indexFields = new ArrayList<IndexField>(numberOfElements); for (String key : keyDbObject.keySet()) { Object value = keyDbObject.get(key); if (TWO_D_IDENTIFIERS.contains(value)) { indexFields.add(IndexField.geo(key)); } else if ("text".equals(value)) { DBObject weights = (DBObject) sourceDocument.get("weights"); for (String fieldName : weights.keySet()) { indexFields.add(IndexField.text(fieldName, Float.valueOf(weights.get(fieldName).toString()))); } } else { Double keyValue = new Double(value.toString()); if (ONE.equals(keyValue)) { indexFields.add(IndexField.create(key, ASC)); } else if (MINUS_ONE.equals(keyValue)) { indexFields.add(IndexField.create(key, DESC)); } } } String name = sourceDocument.get("name").toString(); boolean unique = sourceDocument.containsField("unique") ? (Boolean) sourceDocument.get("unique") : false; boolean dropDuplicates = sourceDocument.containsField("dropDups") ? (Boolean) sourceDocument.get("dropDups") : false; boolean sparse = sourceDocument.containsField("sparse") ? (Boolean) sourceDocument.get("sparse") : false; String language = sourceDocument.containsField("default_language") ? (String) sourceDocument.get("default_language") : ""; String partialFilter = sourceDocument.containsField("partialFilterExpression") ? sourceDocument.get("partialFilterExpression").toString() : ""; IndexInfo info = new IndexInfo(indexFields, name, unique, dropDuplicates, sparse, language); info.partialFilterExpression = partialFilter; return info; }
From source file:org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.java
License:Apache License
private DBObject resolveCompoundIndexKeyFromStringDefinition(String dotPath, String keyDefinitionString) { if (!StringUtils.hasText(dotPath) && !StringUtils.hasText(keyDefinitionString)) { throw new InvalidDataAccessApiUsageException("Cannot create index on root level for empty keys."); }//w w w .j a va 2 s . c om if (!StringUtils.hasText(keyDefinitionString)) { return new BasicDBObject(dotPath, 1); } DBObject dbo = (DBObject) JSON.parse(keyDefinitionString); if (!StringUtils.hasText(dotPath)) { return dbo; } BasicDBObjectBuilder dboBuilder = new BasicDBObjectBuilder(); for (String key : dbo.keySet()) { dboBuilder.add(dotPath + "." + key, dbo.get(key)); } return dboBuilder.get(); }