List of usage examples for com.mongodb DBObject removeField
Object removeField(String key);
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Merges in overriding values from a collection of properties into a map * //from w w w. ja v a 2 s . c om * @param propsMap * the properties to update; the map is updated in place * @param overridingPropObjs * the properties that taken precedence */ private static void mergeProperties(Map<String, DBObject> propsMap, DBCursor overridingPropObjs) { // Keep track of properties that were not overridden Set<String> unmerged = new HashSet<String>(propsMap.keySet()); while (overridingPropObjs != null && overridingPropObjs.hasNext()) { DBObject overridePropObj = overridingPropObjs.next(); String key = (String) overridePropObj.get(FIELD_NAME); unmerged.remove(key); MongoTestDAO.mergeProperty(propsMap, overridePropObj); } // All the untouched properties need to have 'value' moved to 'default' for (String key : unmerged) { DBObject sourceObj = propsMap.get(key); String sourceObjValue = (String) sourceObj.get(FIELD_VALUE); if (sourceObjValue == null) { // There is no value. So the default remains the same. continue; } // Copy the object DBObject targetObj = copyDBObject(sourceObj); // Set the new default targetObj.put(FIELD_DEFAULT, sourceObjValue); // Remove the value targetObj.removeField(FIELD_VALUE); // Reset the version number targetObj.put(FIELD_VERSION, VERSION_ZERO); // Replace the object propsMap.put(key, targetObj); } // Done }
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Merges in overriding value from an object into a map * /*from w w w. ja va 2 s.c o m*/ * @param propsMap * the properties to update; the map is updated in place * @param overridePropObj * the property that taken precedence */ private static void mergeProperty(Map<String, DBObject> propsMap, DBObject overridePropObj) { String key = (String) overridePropObj.get(FIELD_NAME); Integer overrideVersion = (Integer) overridePropObj.get(FIELD_VERSION); String overrideValue = (String) overridePropObj.get(FIELD_VALUE); String overrideOrigin = (String) overridePropObj.get(FIELD_ORIGIN); DBObject propObj = propsMap.get(key); if (propObj == null) { // The property is not (or is no longer) relevant to the test return; } // Copy the property to a new instance DBObject newPropObj = copyDBObject(propObj); // If the new property already has a value, then that becomes the // default String newPropValue = (String) newPropObj.get(FIELD_VALUE); if (newPropValue != null) { newPropObj.put(FIELD_DEFAULT, newPropValue); newPropObj.removeField(FIELD_VALUE); } // Now overwrite with the overriding values newPropObj.put(FIELD_VERSION, overrideVersion); newPropObj.put(FIELD_ORIGIN, overrideOrigin); newPropObj.put(FIELD_VALUE, overrideValue); // Put that into the map propsMap.put(key, newPropObj); // Done }
From source file:org.apache.hadoop.contrib.mongoreduce.MongoStreamRecordReader.java
License:Apache License
public boolean next(Text key, Text value) throws IOException { boolean hadMore = cursor.hasNext(); if (hadMore) { DBObject v = cursor.next(); Object objID = v.get("_id"); try {/*from w w w . j av a 2 s . c om*/ if (objID.getClass().getName().equals(Class.forName("org.bson.types.ObjectId").getName())) key.set(((ObjectId) v.get("_id")).toString().getBytes("UTF-8")); else key.set(objID.toString().getBytes("UTF-8")); } catch (ClassNotFoundException e) { throw new IOException(e); } // remove object ID from DBObject v.removeField("_id"); value.set(v.toString().getBytes("UTF-8")); resultsRead++; } return hadMore; }
From source file:org.apache.rya.mongodb.aggregation.AggregationPipelineQueryNode.java
License:Apache License
/** * Given a StatementPattern, generate an object representing the arguments * to a "$match" command that will find matching triples. * @param sp The StatementPattern to search for * @param path If given, specify the field that should be matched against * the statement pattern, using an ordered list of field names for a nested * field. E.g. to match records { "x": { "y": <statement pattern } }, pass * "x" followed by "y".//w w w . j a v a 2s . c o m * @return The argument of a "$match" query */ private static BasicDBObject getMatchExpression(final StatementPattern sp, final String... path) { final Var subjVar = sp.getSubjectVar(); final Var predVar = sp.getPredicateVar(); final Var objVar = sp.getObjectVar(); final Var contextVar = sp.getContextVar(); RyaIRI s = null; RyaIRI p = null; RyaType o = null; RyaIRI c = null; if (subjVar != null && subjVar.getValue() instanceof Resource) { s = RdfToRyaConversions.convertResource((Resource) subjVar.getValue()); } if (predVar != null && predVar.getValue() instanceof IRI) { p = RdfToRyaConversions.convertIRI((IRI) predVar.getValue()); } if (objVar != null && objVar.getValue() != null) { o = RdfToRyaConversions.convertValue(objVar.getValue()); } if (contextVar != null && contextVar.getValue() instanceof IRI) { c = RdfToRyaConversions.convertIRI((IRI) contextVar.getValue()); } final RyaStatement rs = new RyaStatement(s, p, o, c); final DBObject obj = strategy.getQuery(rs); // Add path prefix, if given if (path.length > 0) { final StringBuilder sb = new StringBuilder(); for (final String str : path) { sb.append(str).append("."); } final String prefix = sb.toString(); final Set<String> originalKeys = new HashSet<>(obj.keySet()); originalKeys.forEach(key -> { final Object value = obj.removeField(key); obj.put(prefix + key, value); }); } return (BasicDBObject) obj; }
From source file:org.baldeapi.v1.persistence.GenericDAO.java
License:Apache License
public DBObject insertOrUpdate(DBObject object) { DBObject query = new BasicDBObject("_id", object.get("_id")); object.removeField("_id"); collection.update(query, object, true, false); return object; }
From source file:org.elasticsearch.river.mongodb.util.MongoDBHelper.java
License:Apache License
public static DBObject applyExcludeFields(DBObject bsonObject, Set<String> excludeFields) { if (excludeFields == null) { return bsonObject; }/*from w w w . ja va2 s. c o m*/ DBObject filteredObject = bsonObject; for (String field : excludeFields) { if (field.contains(".")) { String rootObject = field.substring(0, field.indexOf(".")); String childObject = field.substring(field.indexOf(".") + 1); if (filteredObject.containsField(rootObject)) { Object object = filteredObject.get(rootObject); if (object instanceof DBObject) { DBObject object2 = (DBObject) object; object2 = applyExcludeFields(object2, new HashSet<String>(Arrays.asList(childObject))); } } } else { if (filteredObject.containsField(field)) { filteredObject.removeField(field); } } } return filteredObject; }
From source file:org.fracturedatlas.athena.apa.impl.MongoApaAdapter.java
License:Open Source License
@Override public void deleteTicketProp(TicketProp prop) { if (prop.getTicket() != null) { DBObject recordDoc = getRecordDocument(new BasicDBObject(), prop.getTicket().getType(), ObjectId.massageToObjectId(prop.getTicket().getId())); String fieldName = prop.getPropField().getName(); if (recordDoc != null) { DBObject propsObj = (DBObject) recordDoc.get("props"); if (propsObj.containsField(fieldName)) { propsObj.removeField(fieldName); }/*from www.j a v a 2 s .c o m*/ recordDoc.put("props", propsObj); db.getCollection(prop.getTicket().getType()).save(recordDoc); } } }
From source file:org.hibernate.ogm.datastore.mongodb.dialect.impl.MongoHelpers.java
License:LGPL
/** * Remove a column from the DBObject//from w w w . j ava 2 s .c om * * @param entity the {@link DBObject} with the column * @param column the column to remove */ public static void resetValue(DBObject entity, String column) { // fast path for non-embedded case if (!column.contains(".")) { entity.removeField(column); } else { String[] path = DOT_SEPARATOR_PATTERN.split(column); Object field = entity; int size = path.length; for (int index = 0; index < size; index++) { String node = path[index]; DBObject parent = (DBObject) field; field = parent.get(node); if (field == null && index < size - 1) { //TODO clean up the hierarchy of empty containers // no way to reach the leaf, nothing to do return; } if (index == size - 1) { parent.removeField(node); } } } }
From source file:org.hibernate.ogm.datastore.mongodb.index.impl.MongoDBIndexSpec.java
License:LGPL
/** * Prepare the options by adding additional information to them. *//*from www . j av a 2 s .co m*/ private DBObject prepareOptions(DBObject options, String indexName, boolean unique) { options.put("name", indexName); if (unique) { options.put("unique", true); // MongoDB only allows one null value per unique index which is not in line with what we usually consider // as the definition of a unique constraint. Thus, we mark the index as sparse to only index values // defined and avoid this issue. We do this only if a partialFilterExpression has not been defined // as partialFilterExpression and sparse are exclusive. if (!options.containsField("partialFilterExpression")) { options.put("sparse", true); } } if (Boolean.TRUE.equals(options.get("text"))) { // text is an option we take into account to mark an index as a full text index as we cannot put "text" as // the order like MongoDB as ORM explicitely checks that the order is either asc or desc. // we remove the option from the DBObject so that we don't pass it to MongoDB isTextIndex = true; options.removeField("text"); } return options; }
From source file:org.hibernate.ogm.datastore.mongodb.MongoDBDialect.java
License:LGPL
@Override public void removeAssociation(AssociationKey key, AssociationContext associationContext) { AssociationStorageStrategy storageStrategy = getAssociationStorageStrategy(key, associationContext); WriteConcern writeConcern = getWriteConcern(associationContext); if (storageStrategy == AssociationStorageStrategy.IN_ENTITY) { DBObject entity = this.prepareIdObject(key.getEntityKey()); if (entity != null) { BasicDBObject updater = new BasicDBObject(); addSubQuery("$unset", updater, key.getMetadata().getCollectionRole(), Integer.valueOf(1)); DBObject dbObject = getEmbeddingEntity(key, associationContext); if (dbObject != null) { dbObject.removeField(key.getMetadata().getCollectionRole()); getCollection(key.getEntityKey()).update(entity, updater, true, false, writeConcern); }/* w ww . java 2 s.co m*/ } } else { DBCollection collection = getAssociationCollection(key, storageStrategy); DBObject query = associationKeyToObject(key, storageStrategy); int nAffected = collection.remove(query, writeConcern).getN(); log.removedAssociation(nAffected); } }