List of usage examples for com.mongodb DBObject putAll
void putAll(BSONObject o);
From source file:org.pentaho.di.trans.steps.mongodbupdate.MongoDbUpdate.java
License:Open Source License
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { Object[] row = getRow();/*from w ww . ja va 2s.c o m*/ if (row == null) { setOutputDone(); return false; } if (first) { first = false; data.fieldNumber = meta.getFieldNames().length; data.fieldIndexes = new int[data.fieldNumber]; for (int i = 0; i < data.fieldNumber; i++) { data.fieldIndexes[i] = getInputRowMeta().indexOfValue(meta.getFieldNames()[i]); if (data.fieldIndexes[i] < 0) { logError(BaseMessages.getString(PKG, "MongoDbUpdate.Log.CanNotFindField", meta.getFieldNames()[i])); throw new KettleException(BaseMessages.getString(PKG, "MongoDbUpdate.Log.CanNotFindField", meta.getFieldNames()[i])); } } } String firstPrefix; String secondPrefix; boolean noContainer = false; if (meta.getContainerObjectName() == null) meta.setContinerObjectName(""); if (meta.getContainerObjectName().length() == 0) { firstPrefix = ""; secondPrefix = ""; meta.setArrayFlag(false); noContainer = true; } else { firstPrefix = meta.getContainerObjectName() + "."; secondPrefix = meta.getContainerObjectName() + (meta.getArrayFlag() ? ".$." : "."); } DBObject globalQueryObj = new BasicDBObject(); globalQueryObj.putAll(data.primaryQuery); DBObject queryObj = new BasicDBObject(); DBObject updateInnerObj = new BasicDBObject(); DBObject rowImageObj = new BasicDBObject(); for (int i = 0; i < data.fieldNumber; i++) { String fieldvalue = getInputRowMeta().getString(row, data.fieldIndexes[i]); switch (meta.getTrimType()) { case 0: break; case 1: fieldvalue = Const.ltrim(fieldvalue); break; case 2: fieldvalue = Const.rtrim(fieldvalue); break; case 3: fieldvalue = Const.trim(fieldvalue); break; } if (meta.getKeyFieldFlags()[i]) { queryObj.put(firstPrefix + meta.getJSONNames()[i], fieldvalue); } else { updateInnerObj.put(secondPrefix + meta.getJSONNames()[i], fieldvalue); } rowImageObj.put(meta.getJSONNames()[i], fieldvalue); } globalQueryObj.putAll(queryObj); boolean insert = false; if (meta.getInsertIfNotPresentFlag() && (data.collection.count(globalQueryObj) == 0)) { insert = true; } if (insert) { if (noContainer) { data.collection.insert(rowImageObj); if (getLogLevel().isDebug()) { logDebug(BaseMessages.getString(PKG, "MongoDbUpdate.Log.DocumentInsert", JSON.serialize(rowImageObj))); } } else if (meta.getArrayFlag()) { DBObject updateObj = new BasicDBObject(); updateObj.put("$push", new BasicDBObject(meta.getContainerObjectName(), rowImageObj)); data.collection.updateMulti(data.primaryQuery, updateObj); if (getLogLevel().isDebug()) { logDebug(BaseMessages.getString(PKG, "MongoDbUpdate.Log.DocumentPushed", JSON.serialize(data.primaryQuery), JSON.serialize(updateObj))); } } else { DBObject updateObj = new BasicDBObject(); updateObj.put("$set", new BasicDBObject(meta.getContainerObjectName(), rowImageObj)); data.collection.updateMulti(data.primaryQuery, updateObj); if (getLogLevel().isDebug()) { logDebug(BaseMessages.getString(PKG, "MongoDbUpdate.Log.InsertIntoDocument"), JSON.serialize(data.primaryQuery), JSON.serialize(updateObj)); } } } else { DBObject updateObj = new BasicDBObject(); updateObj.put("$set", updateInnerObj); data.collection.updateMulti(globalQueryObj, updateObj); if (getLogLevel().isDebug()) { logDebug(BaseMessages.getString(PKG, "MongoDbUpdate.Log.DocumentUpdated", JSON.serialize(globalQueryObj), JSON.serialize(updateObj))); } } putRow(getInputRowMeta(), row); return true; }
From source file:org.restheart.hal.metadata.singletons.RequestPropsInjecterTransformer.java
License:Open Source License
/** * * @param exchange/*from w w w .j av a 2 s. c o m*/ * @param context * @param contentToTransform * @param args properties to add */ @Override public void tranform(final HttpServerExchange exchange, final RequestContext context, DBObject contentToTransform, final DBObject args) { BasicDBObject injected = new BasicDBObject(); if (args instanceof BasicDBObject) { HashMap<String, Object> properties = getPropsValues(exchange, context); String firstKey = args.keySet().iterator().next(); Object _toinject = args.get(firstKey); if (_toinject instanceof BasicDBList) { BasicDBList toinject = (BasicDBList) _toinject; toinject.forEach(_el -> { if (_el instanceof String) { String el = (String) _el; Object value = properties.get(el); if (value != null) { injected.put(el, value); } else { context.addWarning("property in the args list does not have a value: " + _el); } } else { context.addWarning("property in the args list is not a string: " + _el); } }); contentToTransform.put(firstKey, injected); } else { context.addWarning( "transformer wrong definition: args must be an object with a array containing the names of the properties to inject. got " + JsonUtils.serialize(args)); } } else if (args instanceof BasicDBList) { HashMap<String, Object> properties = getPropsValues(exchange, context); BasicDBList toinject = (BasicDBList) args; toinject.forEach(_el -> { if (_el instanceof String) { String el = (String) _el; Object value = properties.get(el); if (value != null) { injected.put(el, value); } else { context.addWarning("property in the args list does not have a value: " + _el); } } else { context.addWarning("property in the args list is not a string: " + _el); } }); contentToTransform.putAll((BSONObject) injected); } else { context.addWarning( "transformer wrong definition: args must be an object with a array containing the names of the properties to inject. got " + JsonUtils.serialize(args)); } }
From source file:org.restheart.hal.metadata.singletons.RequetPropsInjecterTransformer.java
License:Open Source License
/** * * @param exchange//from w w w. j av a 2 s . com * @param context * @param contentToTransform * @param args properties to add */ @Override public void tranform(final HttpServerExchange exchange, final RequestContext context, DBObject contentToTransform, final DBObject args) { BasicDBObject injected = new BasicDBObject(); if (args instanceof BasicDBObject) { HashMap<String, String> properties = getPropsValues(exchange, context); String firstKey = args.keySet().iterator().next(); Object _toinject = args.get(firstKey); if (_toinject instanceof BasicDBList) { BasicDBList toinject = (BasicDBList) _toinject; toinject.forEach(_el -> { if (_el instanceof String) { String el = (String) _el; String value = properties.get(el); injected.put(el, value); } else { context.addWarning("property in the args list is not a string: " + _el); } }); contentToTransform.put(firstKey, injected); } else { context.addWarning( "transformer wrong definition: args must be an object with a array containing the names of the properties to inject. got " + JsonUtils.serialize(args)); } } else if (args instanceof BasicDBList) { HashMap<String, String> properties = getPropsValues(exchange, context); BasicDBList toinject = (BasicDBList) args; toinject.forEach(_el -> { if (_el instanceof String) { String el = (String) _el; String value = properties.get(el); injected.put(el, value); } else { context.addWarning("property in the args list is not a string: " + _el); } }); contentToTransform.putAll((BSONObject) injected); } else { context.addWarning( "transformer wrong definition: args must be an object with a array containing the names of the properties to inject. got " + JsonUtils.serialize(args)); } }
From source file:org.restheart.handlers.schema.JsonSchemaTransformer.java
License:Open Source License
public static void escapeSchema(DBObject schema) { DBObject escaped = (DBObject) JsonUtils.escapeKeys(schema, false); List<String> keys = Lists.newArrayList(schema.keySet().iterator()); keys.stream().forEach(f -> schema.removeField(f)); schema.putAll(escaped); }
From source file:org.restheart.handlers.schema.JsonSchemaTransformer.java
License:Open Source License
public static void unescapeSchema(DBObject schema) { DBObject unescaped = (DBObject) JsonUtils.unescapeKeys(schema); List<String> keys = Lists.newArrayList(schema.keySet().iterator()); keys.stream().forEach(f -> schema.removeField(f)); schema.putAll(unescaped); }
From source file:org.slc.sli.aggregation.mapreduce.io.MongoAggWriter.java
License:Apache License
@Override public void write(EmittableKey key, BSONWritable value) throws IOException { DBObject k = new BasicDBObject(); k.putAll(key.toBSON()); DBObject v = new BasicDBObject(); v.put("$set", value); output.findAndModify(k, v);/*from www .j a v a 2 s. c o m*/ }
From source file:org.slc.sli.dal.convert.ContainerDocumentAccessor.java
License:Apache License
protected boolean updateContainerDoc(final Query query, Map<String, Object> newValues, String collectionName, String type) {/* ww w . j ava 2 s . c o m*/ final ContainerDocument containerDocument = containerDocumentHolder.getContainerDocument(type); if (containerDocument.isContainerSubdoc()) { Update update = new Update(); for (Map.Entry<String, Object> patch : newValues.entrySet()) { update.set("body." + patch.getKey(), patch.getValue()); } return getLocation(type).doUpdate(query, update); } //empty attendanceEvent(or other) array DBObject emptyArray = new BasicDBObject(); emptyArray.put("body." + containerDocument.getFieldToPersist(), new ArrayList()); DBObject setEmptyArray = new BasicDBObject("$set", emptyArray); mongoTemplate.getCollection(collectionName).update(query.getQueryObject(), setEmptyArray, false, false, WriteConcern.SAFE); TenantContext.setIsSystemCall(false); final String fieldToPersist = containerDocument.getFieldToPersist(); DBObject entityDetails = new BasicDBObject(); for (Map.Entry<String, Object> newValue : newValues.entrySet()) { if (!newValue.getKey().equals(containerDocument.getFieldToPersist())) { entityDetails.put("body." + newValue.getKey(), newValue.getValue()); } } DBObject set = new BasicDBObject("$set", entityDetails); DBObject docToPersist = null; if (newValues.containsKey(containerDocument.getFieldToPersist())) { docToPersist = BasicDBObjectBuilder.start().push("$pushAll") .add("body." + fieldToPersist, newValues.get(fieldToPersist)).get(); } else { docToPersist = new BasicDBObject(); } docToPersist.putAll(set); return mongoTemplate.getCollection(collectionName) .update(query.getQueryObject(), docToPersist, true, false, WriteConcern.SAFE).getLastError().ok(); }
From source file:org.slc.sli.dal.convert.ContainerDocumentHelper.java
License:Apache License
public static DBObject buildDocumentToPersist(final ContainerDocumentHolder containerDocumentHolder, final Entity entity, final UUIDGeneratorStrategy generatorStrategy, final INaturalKeyExtractor naturalKeyExtractor) { final ContainerDocument containerDocument = containerDocumentHolder.getContainerDocument(entity.getType()); DBObject entityDetails = new BasicDBObject(); final Map<String, Object> entityBody = entity.getBody(); for (final String key : containerDocument.getParentNaturalKeys()) { entityDetails.put("body." + key, entityBody.get(key)); }/*w w w .j a v a2 s. c o m*/ DBObject docToPersist = null; if (containerDocument.isContainerSubdoc()) { final Map<String, Object> containerSubDoc = new HashMap<String, Object>(); String key = createParentKey(entity, containerDocumentHolder, generatorStrategy) + getContainerDocId(entity, generatorStrategy, naturalKeyExtractor); containerSubDoc.put("_id", key); containerSubDoc.put("type", entity.getType()); containerSubDoc.put("body", entityBody); containerSubDoc.put("metaData", entity.getMetaData()); final List<Map<String, Object>> containerSubDocList = new ArrayList<Map<String, Object>>(); containerSubDocList.add(containerSubDoc); docToPersist = BasicDBObjectBuilder.start().get(); } else { if (entity.getMetaData() != null && !entity.getMetaData().isEmpty()) { entityDetails.put("metaData", entity.getMetaData()); } entityDetails.put("type", entity.getType()); String fieldToPersist = containerDocument.getFieldToPersist(); if (entityBody.containsKey(fieldToPersist)) { docToPersist = BasicDBObjectBuilder.start().push("$pushAll") .add("body." + fieldToPersist, entityBody.get(fieldToPersist)).get(); } } DBObject set = new BasicDBObject("$set", entityDetails); docToPersist.putAll(set); return docToPersist; }
From source file:org.springframework.data.document.mongodb.convert.MappingMongoConverter.java
License:Apache License
/** * Internal write conversion method which should be used for nested invocations. * /*from w w w. ja va2 s . c om*/ * @param obj * @param dbo */ @SuppressWarnings("unchecked") protected void writeInternal(final Object obj, final DBObject dbo) { if (null == obj) { return; } Class<?> customTarget = getCustomTarget(obj.getClass(), DBObject.class); if (customTarget != null) { DBObject result = conversionService.convert(obj, DBObject.class); dbo.putAll(result); return; } if (Map.class.isAssignableFrom(obj.getClass())) { writeMapInternal((Map<Object, Object>) obj, dbo); return; } MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(obj.getClass()); writeInternal(obj, dbo, entity); }
From source file:org.springframework.data.document.mongodb.query.Criteria.java
License:Apache License
public DBObject getCriteriaObject() { if (this.criteriaChain.size() == 1) { return criteriaChain.get(0).getSingleCriteriaObject(); } else {/* w w w . j a v a 2s .c o m*/ DBObject criteriaObject = new BasicDBObject(); for (Criteria c : this.criteriaChain) { criteriaObject.putAll(c.getSingleCriteriaObject()); } return criteriaObject; } }