List of usage examples for com.mongodb DBObject removeField
Object removeField(String key);
From source file:org.hibernate.ogm.datastore.mongodb.utils.MongoDBTestHelper.java
License:LGPL
/** * The MongoDB dialect replaces the name of the column identifier, so when the tuple is extracted from the db * we replace the column name of the identifier with the original one. * We are assuming the identifier is not embedded and is a single property. *///from w w w . j a v a2 s. co m private void replaceIdentifierColumnName(DBObject result, EntityKey key) { Object idValue = result.get(MongoDBDialect.ID_FIELDNAME); result.removeField(MongoDBDialect.ID_FIELDNAME); result.put(key.getColumnNames()[0], idValue); }
From source file:org.jongo.spike.projection.JacksonProjection.java
License:Apache License
public Query getProjectionQuery(Class<?> pojoClass) { DBObject query = new BasicDBObject(); putFieldNames(query, getSchemaNode(pojoClass)); query.removeField("_id"); return new ProjectionQuery(query); }
From source file:org.jongo.Update.java
License:Apache License
private void removeIdField(DBObject updateDbo) { DBObject pojoAsDbo = (DBObject) updateDbo.get("$set"); if (pojoAsDbo.containsField("_id")) { // Need to materialize lazy objects which are read only if (pojoAsDbo instanceof LazyBSONObject) { BasicDBObject expanded = new BasicDBObject(); expanded.putAll(pojoAsDbo);// w w w.j a va2 s . c om updateDbo.put("$set", expanded); pojoAsDbo = expanded; } pojoAsDbo.removeField("_id"); } }
From source file:org.mongodb.morphia.mapping.Mapper.java
License:Open Source License
/** * <p> Converts a java object to a mongo-compatible object (possibly a DBObject for complex mappings). Very similar to {@link * Mapper#toDBObject} </p> <p> Used (mainly) by query/update operations </p> *//*from w ww . j a va 2 s . c om*/ Object toMongoObject(final Object javaObj, final boolean includeClassName) { if (javaObj == null) { return null; } Class origClass = javaObj.getClass(); if (origClass.isAnonymousClass() && origClass.getSuperclass().isEnum()) { origClass = origClass.getSuperclass(); } final Object newObj = getConverters().encode(origClass, javaObj); if (newObj == null) { LOG.warning("converted " + javaObj + " to null"); return newObj; } final Class type = newObj.getClass(); final boolean bSameType = origClass.equals(type); //TODO: think about this logic a bit more. //Even if the converter changed it, should it still be processed? if (!bSameType && !(Map.class.isAssignableFrom(type) || Iterable.class.isAssignableFrom(type))) { return newObj; } else { //The converter ran, and produced another type, or it is a list/map boolean isSingleValue = true; boolean isMap = false; Class subType = null; if (type.isArray() || Map.class.isAssignableFrom(type) || Iterable.class.isAssignableFrom(type)) { isSingleValue = false; isMap = implementsInterface(type, Map.class); // subtype of Long[], List<Long> is Long subType = (type.isArray()) ? type.getComponentType() : getParameterizedClass(type, (isMap) ? 1 : 0); } if (isSingleValue && !isPropertyType(type)) { final DBObject dbObj = toDBObject(newObj); if (!includeClassName) { dbObj.removeField(CLASS_NAME_FIELDNAME); } return dbObj; } else if (newObj instanceof DBObject) { return newObj; } else if (isMap) { if (isPropertyType(subType)) { return toDBObject(newObj); } else { final HashMap m = new HashMap(); for (final Map.Entry e : (Iterable<Map.Entry>) ((Map) newObj).entrySet()) { m.put(e.getKey(), toMongoObject(e.getValue(), includeClassName)); } return m; } //Set/List but needs elements converted } else if (!isSingleValue && !isPropertyType(subType)) { final List<Object> values = new BasicDBList(); if (type.isArray()) { for (final Object obj : (Object[]) newObj) { values.add(toMongoObject(obj, includeClassName)); } } else { for (final Object obj : (Iterable) newObj) { values.add(toMongoObject(obj, includeClassName)); } } return values; } else { return newObj; } } }
From source file:org.mongolink.domain.updateStrategy.DiffStrategy.java
License:Open Source License
private void execute(DbObjectDiff.Modifier modifier, DBCollection collection, DBObject diff, DBObject q) { final DBObject modifications = (DBObject) diff.get(modifier.toString()); diff.removeField(modifier.toString()); if (modifications != null) { LOGGER.debug("Updating array : {} modifier: {} values: {}", q, modifier, modifications); collection.update(q, new BasicDBObject(modifier.toString(), modifications)); }// w w w. java2 s . c om }
From source file:org.oncoblocks.centromere.mongodb.ImportUtils.java
License:Apache License
/** * Serializes an object into a string format that can be inserted into a MongoDB collection. * // w w w. j a v a 2s . c o m * @param entity * @return */ public String convertEntityToJson(Object entity) { MongoConverter converter = mongoTemplate.getConverter(); DBObject dbObject = new BasicDBObject(); converter.write(entity, dbObject); if (dbObject.containsField("_id") && dbObject.get("_id") == null) { dbObject.removeField("_id"); } if (dbObject.containsField("_class")) { dbObject.removeField("_class"); } return dbObject.toString(); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoDBUtils.java
License:Apache License
static <T> T replaceInKeys(T object, String target, String replacement) { if (object instanceof DBObject) { DBObject dbObject = (DBObject) object; List<String> keys = new ArrayList<>(); for (String s : dbObject.keySet()) { if (s.contains(target)) { keys.add(s);//from w w w . j av a2 s. c o m } replaceInKeys(dbObject.get(s), target, replacement); } for (String key : keys) { Object value = dbObject.removeField(key); key = key.replace(target, replacement); dbObject.put(key, value); } } else if (object instanceof List) { for (Object o : ((List) object)) { replaceInKeys(o, target, replacement); } } return object; }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
private DBObject createProjection(Query query, QueryOptions options) { DBObject projection = new BasicDBObject(); if (options == null) { options = new QueryOptions(); }/*from www .j a v a 2 s . co m*/ if (options.containsKey("sort")) { if (options.getBoolean("sort")) { options.put("sort", new BasicDBObject(DBObjectToVariantConverter.CHROMOSOME_FIELD, 1) .append(DBObjectToVariantConverter.START_FIELD, 1)); } else { options.remove("sort"); } } List<String> includeList = options.getAsStringList("include"); if (!includeList.isEmpty()) { //Include some for (String s : includeList) { String key = DBObjectToVariantConverter.toShortFieldName(s); if (key != null) { projection.put(key, 1); } else { logger.warn("Unknown include field: {}", s); } } } else { //Include all for (String values : DBObjectToVariantConverter.fieldsMap.values()) { projection.put(values, 1); } if (options.containsKey("exclude")) { // Exclude some List<String> excludeList = options.getAsStringList("exclude"); for (String s : excludeList) { String key = DBObjectToVariantConverter.toShortFieldName(s); if (key != null) { projection.removeField(key); } else { logger.warn("Unknown exclude field: {}", s); } } } } if (query.containsKey(VariantQueryParams.RETURNED_FILES.key()) && projection.containsField(DBObjectToVariantConverter.STUDIES_FIELD)) { List<Integer> files = query.getAsIntegerList(VariantQueryParams.RETURNED_FILES.key()); projection.put(DBObjectToVariantConverter.STUDIES_FIELD, new BasicDBObject("$elemMatch", new BasicDBObject( DBObjectToVariantSourceEntryConverter.FILES_FIELD + "." + DBObjectToVariantSourceEntryConverter.FILEID_FIELD, new BasicDBObject("$in", files)))); } if (query.containsKey(VariantQueryParams.RETURNED_STUDIES.key()) && projection.containsField(DBObjectToVariantConverter.STUDIES_FIELD)) { List<Integer> studiesIds = getStudyIds(query.getAsList(VariantQueryParams.RETURNED_STUDIES.key()), options); // List<Integer> studies = query.getAsIntegerList(VariantQueryParams.RETURNED_STUDIES.key()); if (!studiesIds.isEmpty()) { projection.put(DBObjectToVariantConverter.STUDIES_FIELD, new BasicDBObject("$elemMatch", new BasicDBObject(DBObjectToVariantSourceEntryConverter.STUDYID_FIELD, new BasicDBObject("$in", studiesIds)))); } } logger.debug("Projection: {}", projection); return projection; }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
@Deprecated private DBObject parseProjectionQueryOptions(QueryOptions options) { DBObject projection = new BasicDBObject(); if (options == null) { return projection; }/* w w w. j a v a 2 s . c o m*/ List<String> includeList = options.getAsStringList("include"); if (!includeList.isEmpty()) { //Include some for (String s : includeList) { String key = DBObjectToVariantConverter.toShortFieldName(s); if (key != null) { projection.put(key, 1); } else { logger.warn("Unknown include field: {}", s); } } } else { //Include all for (String values : DBObjectToVariantConverter.fieldsMap.values()) { projection.put(values, 1); } if (options.containsKey("exclude")) { // Exclude some List<String> excludeList = options.getAsStringList("exclude"); for (String s : excludeList) { String key = DBObjectToVariantConverter.toShortFieldName(s); if (key != null) { projection.removeField(key); } else { logger.warn("Unknown exclude field: {}", s); } } } } if (options.containsKey(VariantQueryParams.RETURNED_FILES.key()) && projection.containsField(DBObjectToVariantConverter.STUDIES_FIELD)) { // List<String> files = options.getListAs(FILES, String.class); int file = options.getInt(VariantQueryParams.RETURNED_FILES.key()); projection.put(DBObjectToVariantConverter.STUDIES_FIELD, new BasicDBObject("$elemMatch", new BasicDBObject(DBObjectToVariantSourceEntryConverter.FILES_FIELD + "." + DBObjectToVariantSourceEntryConverter.FILEID_FIELD, file // new BasicDBObject( // "$in", // files // ) ))); } logger.debug("Projection: {}", projection); return projection; }
From source file:org.openspotlight.storage.mongodb.MongoStorageSessionImpl.java
License:Open Source License
private void flushSimpleProperty(final DBObject ref, final Property property) throws Exception { checkNotNull("property", property); DBObject reference;// ww w .j a v a 2 s . c o m String collectionName; if (ref != null) { reference = ref; collectionName = StringKeysSupport.getNodeType((String) reference.get(ID)); } else if (property.getParent() instanceof StorageNode) { reference = createNodeReference((StorageNode) property.getParent()); collectionName = ((StorageNode) property.getParent()).getType(); } else if (property.getParent() instanceof StorageLink) { reference = createLinkReference((StorageLink) property.getParent()); collectionName = ((StorageLink) property.getParent()).getSource().getType(); } else { throw new IllegalStateException(); } String objName = null; Object value = null; if (property.isIndexed()) { ensureIndexed(property.getParent().getPartition(), collectionName, INDEXED, property.getPropertyName(), null); objName = INDEXED; value = ((PropertyImpl) property).getTransientValueAsString(); if (value == null) { value = NULL_VALUE; } } else if (!property.isKey()) { objName = PROPERTIES; value = ((PropertyImpl) property).getTransientValueAsBytes(); } if (objName == null) { return; } DBObject obj = (DBObject) reference.get(objName); if (obj == null) { obj = new BasicDBObject(); reference.put(objName, obj); } if (value instanceof byte[] && isBiggerThan4mb((byte[]) value)) { obj.put(getBigPropertyName(property), true); } else { obj.removeField(getBigPropertyName(property)); obj.put(property.getPropertyName(), value); StorageNode nodeEntry; if (property.getParent() instanceof StorageNode) { nodeEntry = (StorageNode) property.getParent(); } else if (property.getParent() instanceof StorageLink) { nodeEntry = ((StorageLink) property.getParent()).getSource(); } else { throw new IllegalStateException(); } if (FlushMode.AUTO.equals(flushMode)) { getCachedCollection(property.getParent().getPartition(), nodeEntry.getType()).save(reference); } else { final Pair<StorageNode, DBObject> p = newPair(nodeEntry, reference, Pair.PairEqualsMode.K1); if (!transientObjects.get(property.getParent().getPartition()).contains(p)) { transientObjects.put(property.getParent().getPartition(), p); } } } }