Java Code Examples for org.bson.Document#remove()

The following examples show how to use org.bson.Document#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticBulkService.java    From mongolastic with MIT License 6 votes vote down vote up
@Override
public void proceed(List content) {
    try {
        logger.info("Transferring data began to elasticsearch.");
        final String indexName = config.getMisc().getDindex().getAs();
        final String typeName = config.getMisc().getCtype().getAs();
        for (Object o : content) {
            Document doc = (Document) o;
            Object id = doc.get("_id");
            IndexRequest indexRequest = new IndexRequest(indexName, typeName, String.valueOf(id));
            doc.remove("_id");
            indexRequest.source(doc.toJson(encoder), XContentType.JSON);
            bulkProcessor.add(indexRequest);
        }
    } catch (Exception ex) {
        logger.debug(ex.getMessage(), ex);
    }
}
 
Example 2
Source File: MongoDocumentStorage.java    From lumongo with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream getAssociatedDocumentStream(String uniqueId, String fileName) {
	GridFSBucket gridFS = createGridFSConnection();
	GridFSFile file = gridFS.find(new Document(ASSOCIATED_METADATA + "." + FILE_UNIQUE_ID_KEY, getGridFsId(uniqueId, fileName))).first();

	if (file == null) {
		return null;
	}

	InputStream is = gridFS.openDownloadStream(file.getObjectId());
	;

	Document metadata = file.getMetadata();
	if (metadata.containsKey(COMPRESSED_FLAG)) {
		boolean compressed = (boolean) metadata.remove(COMPRESSED_FLAG);
		if (compressed) {
			is = new InflaterInputStream(is);
		}
	}

	return is;
}
 
Example 3
Source File: TestCaseStore.java    From EDDI with Apache License 2.0 6 votes vote down vote up
@Override
public TestCase loadTestCase(String id) throws IResourceStore.ResourceNotFoundException, IResourceStore.ResourceStoreException {
    Document document = testcaseCollection.find(new Document("_id", new ObjectId(id))).first();

    try {
        if (document == null) {
            String message = "Could not find TestCase (id=%s)";
            message = String.format(message, id);
            throw new IResourceStore.ResourceNotFoundException(message);
        }

        document.remove("_id");

        return jsonSerialization.deserialize(document.toString(), TestCase.class);
    } catch (IOException e) {
        log.debug(e.getLocalizedMessage(), e);
        throw new IResourceStore.ResourceStoreException(e.getLocalizedMessage(), e);
    }
}
 
Example 4
Source File: GroupStore.java    From EDDI with Apache License 2.0 6 votes vote down vote up
public Group readGroup(String groupId) throws IResourceStore.ResourceStoreException, IResourceStore.ResourceNotFoundException {
    Document groupDocument = collection.find(new Document("_id", new ObjectId(groupId))).first();

    try {
        if (groupDocument == null) {
            String message = "Resource 'Group' not found. (groupId=%s)";
            message = String.format(message, groupId);
            throw new IResourceStore.ResourceNotFoundException(message);
        }

        groupDocument.remove("_id");

        return documentBuilder.build(groupDocument, Group.class);
    } catch (IOException e) {
        throw new IResourceStore.ResourceStoreException("Cannot parse json structure into Group entity.", e);
    }
}
 
Example 5
Source File: DatastoreImpl.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T merge(final T entity, final InsertOneOptions options) {
    final Object id = mapper.getId(entity);
    if (id == null) {
        throw new MappingException("Could not get id for " + entity.getClass().getName());
    }

    final Document document = mapper.toDocument(entity);
    document.remove("_id");

    final Query<T> query = (Query<T>) find(entity.getClass()).filter(eq("_id", id));
    if (!tryVersionedUpdate(entity, mapper.getCollection(entity.getClass()), options)) {
        UpdateResult execute = query.update(UpdateOperators.set(entity))
                                    .execute(new UpdateOptions()
                                                 .clientSession(findSession(options))
                                                 .writeConcern(options.writeConcern()));
        if (execute.getModifiedCount() != 1) {
            throw new UpdateException("Nothing updated");
        }
    }

    return query.first();
}
 
Example 6
Source File: MongoUserProvider.java    From graviteeio-access-management with Apache License 2.0 6 votes vote down vote up
private User convert(Document document) {
    String username = document.getString(configuration.getUsernameField());
    DefaultUser user = new DefaultUser(username);
    user.setId(document.getString(FIELD_ID));
    user.setCredentials(document.getString(configuration.getPasswordField()));
    user.setCreatedAt(document.get(FIELD_CREATED_AT, Date.class));
    user.setUpdatedAt(document.get(FIELD_UPDATED_AT, Date.class));
    // additional claims
    Map<String, Object> claims = new HashMap<>();
    claims.put(StandardClaims.SUB, document.getString(FIELD_ID));
    claims.put(StandardClaims.PREFERRED_USERNAME, username);
    // remove reserved claims
    document.remove(FIELD_ID);
    document.remove(configuration.getUsernameField());
    document.remove(configuration.getPasswordField());
    document.entrySet().forEach(entry -> claims.put(entry.getKey(), entry.getValue()));
    user.setAdditionalInformation(claims);
    return user;
}
 
Example 7
Source File: SetEntityOperator.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public OperationTarget toTarget(final PathTarget pathTarget) {
    return new OperationTarget(null, value()) {
        @Override
        public Object encode(final Mapper mapper) {
            MappedClass mappedClass = mapper.getMappedClass(getValue().getClass());
            if (mappedClass.getVersionField() == null) {
                return super.encode(mapper);
            }

            Codec codec = mapper.getCodecRegistry().get(getValue().getClass());
            DocumentWriter writer = new DocumentWriter();

            codec.encode(writer, getValue(), EncoderContext.builder().build());

            Document document = writer.getDocument();
            document.remove(mappedClass.getVersionField().getMappedFieldName());
            return document;
        }
    };
}
 
Example 8
Source File: ContractInfo.java    From nuls-v2 with MIT License 6 votes vote down vote up
public static ContractInfo toInfo(Document document) {
    List<ContractMethod> methods = new ArrayList<>();
    List<Document> methodsList = (List<Document>) document.get("methods");
    for (Document doc : methodsList) {
        ContractMethod method = DocumentTransferTool.toInfo(doc, ContractMethod.class);
        List<ContractMethodArg> params = new ArrayList<>();
        List<Document> paramsList = (List<Document>) doc.get("params");
        for (Document paramDoc : paramsList) {
            ContractMethodArg param = DocumentTransferTool.toInfo(paramDoc, ContractMethodArg.class);
            params.add(param);
        }
        doc.remove("params");
        method.setParams(params);
        methods.add(method);
    }
    document.remove("methods");
    ContractInfo info = DocumentTransferTool.toInfo(document, "contractAddress", ContractInfo.class);

    info.setMethods(methods);
    return info;

}
 
Example 9
Source File: ContractInfo.java    From nuls-v2 with MIT License 6 votes vote down vote up
public Document toDocument() {
    Document document = DocumentTransferTool.toDocument(this, "contractAddress");
    List<Document> methodsList = new ArrayList<>();
    List<Document> paramsList;
    if (methods != null) {
        for (ContractMethod method : methods) {
            List<ContractMethodArg> params = method.getParams();
            paramsList = new ArrayList<>();
            for (ContractMethodArg param : params) {
                Document paramDoc = DocumentTransferTool.toDocument(param);
                paramsList.add(paramDoc);
            }
            Document doc = DocumentTransferTool.toDocument(method);
            doc.put("params", paramsList);
            methodsList.add(doc);
        }
    }
    document.put("methods", methodsList);

    //document.put("resultInfo", resultInfo.toDocument());
    document.remove("resultInfo");
    return document;
}
 
Example 10
Source File: PutMongoTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdate() throws Exception {
    Document doc = DOCUMENTS.get(0);

    // pre-insert document
    collection.insertOne(doc);

    // modify the object
    doc.put("abc", "123");
    doc.put("xyz", "456");
    doc.remove("c");

    byte[] bytes = documentToByteArray(doc);

    runner.setProperty(PutMongo.MODE, "update");
    runner.enqueue(bytes);
    runner.run();

    runner.assertAllFlowFilesTransferred(PutMongo.REL_SUCCESS, 1);
    MockFlowFile out = runner.getFlowFilesForRelationship(PutMongo.REL_SUCCESS).get(0);
    out.assertContentEquals(bytes);

    assertEquals(1, collection.count());
    assertEquals(doc, collection.find().first());
}
 
Example 11
Source File: EventWriteConverter.java    From ic with MIT License 6 votes vote down vote up
@Nullable
@Override
public Document convert(Event event) {
    if (Objects.equals(event.getDummy(), true)) {
        return convertDummy(event);
    }
    Document document = new Document(event.getBody());

    document.remove("type");
    document.remove("timestamp");
    document.remove("updatedTimestamp");
    document.entrySet().removeIf(entry -> entry.getKey().startsWith("_"));

    document.put("id", event.getId());
    document.put("operator", event.getOperator());
    document.put("source", event.getSource());
    document.put("time", event.getTime());
    document.put("updated", event.getUpdated());
    document.put("_ip", event.getIp());
    document.put("_hidden", event.getHidden());
    document.put("_dummy", event.getDummy());
    //数据库中存储的字段为event,展示给用户的是type
    document.put("event", event.getType());
    return document;
}
 
Example 12
Source File: MongoDocumentStorage.java    From lumongo with Apache License 2.0 5 votes vote down vote up
private AssociatedDocument loadGridFSToAssociatedDocument(GridFSBucket gridFS, GridFSFile file, FetchType fetchType) throws IOException {
	AssociatedDocument.Builder aBuilder = AssociatedDocument.newBuilder();
	aBuilder.setFilename(file.getFilename());
	Document metadata = file.getMetadata();

	boolean compressed = false;
	if (metadata.containsKey(COMPRESSED_FLAG)) {
		compressed = (boolean) metadata.remove(COMPRESSED_FLAG);
	}

	long timestamp = (long) metadata.remove(TIMESTAMP);

	aBuilder.setCompressed(compressed);
	aBuilder.setTimestamp(timestamp);

	aBuilder.setDocumentUniqueId((String) metadata.remove(DOCUMENT_UNIQUE_ID_KEY));
	for (String field : metadata.keySet()) {
		aBuilder.addMetadata(Metadata.newBuilder().setKey(field).setValue((String) metadata.get(field)));
	}

	if (FetchType.FULL.equals(fetchType)) {

		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		gridFS.downloadToStream(file.getObjectId(), byteArrayOutputStream);
		byte[] bytes = byteArrayOutputStream.toByteArray();
		if (null != bytes) {
			if (compressed) {
				bytes = CommonCompression.uncompressZlib(bytes);
			}
			aBuilder.setDocument(ByteString.copyFrom(bytes));
		}
	}
	aBuilder.setIndexName(indexName);
	return aBuilder.build();
}
 
Example 13
Source File: MongoAuthenticationProvider.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
private User createUser(Document document) {
    String username = document.getString(FIELD_USERNAME);
    DefaultUser user = new DefaultUser(username);
    Map<String, Object> claims = new HashMap<>();

    String sub = document.containsKey(FIELD_ID) ?
            document.get(FIELD_ID) instanceof ObjectId ? ((ObjectId) document.get(FIELD_ID)).toString() : document.getString(FIELD_ID)
            : username;
    // set technical id
    user.setId(sub);

    // set user roles
    user.setRoles(getUserRoles(document));

    // set claims
    claims.put(StandardClaims.SUB, sub);
    claims.put(StandardClaims.PREFERRED_USERNAME, username);
    if (this.mapper.getMappers() != null && !this.mapper.getMappers().isEmpty()) {
        this.mapper.getMappers().forEach((k, v) -> claims.put(k, document.get(v)));
    } else {
        // default claims
        // remove reserved claims
        document.remove(FIELD_ID);
        document.remove(FIELD_USERNAME);
        document.remove(configuration.getPasswordField());
        document.remove(FIELD_CREATED_AT);
        if (document.containsKey(FIELD_UPDATED_AT)) {
            document.put(StandardClaims.UPDATED_AT, document.get(FIELD_UPDATED_AT));
            document.remove(FIELD_UPDATED_AT);
        }
        document.entrySet().forEach(entry -> claims.put(entry.getKey(), entry.getValue()));
    }
    user.setAdditionalInformation(claims);

    return user;
}
 
Example 14
Source File: MongoSession.java    From ymate-platform-v2 with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T extends IEntity> T insert(T entity) throws Exception {
    Document _document = ResultSetHelper.toDocument(entity);
    if (entity.getId() == null && StringUtils.isBlank(entity.getId().toString())) {
        _document.remove(IMongo.OPT.ID);
    }
    __doGetCollection(entity.getClass()).insertOne(_document);
    String _id = _document.get(IMongo.OPT.ID).toString();
    entity.setId(_id);
    return entity;
}
 
Example 15
Source File: DateExpressionTest.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Test
public void testDateFromParts() {
    getDs().save(new Sales());
    Document result =
        getDs()
            .aggregate(Sales.class)
            .project(of()
                         .include("date", dateFromParts()
                                              .year(2017)
                                              .month(2)
                                              .day(8)
                                              .hour(12))
                         .include("date_iso", dateFromParts()
                                                  .isoWeekYear(2017)
                                                  .isoWeek(6)
                                                  .isoDayOfWeek(3)
                                                  .hour(12))
                         .include("date_timezone", dateFromParts()
                                                       .year(2016)
                                                       .month(12)
                                                       .day(31)
                                                       .hour(23)
                                                       .minute(46)
                                                       .second(12)
                                                       .timezone("America/New_York")))
            .execute(Document.class)
            .next();
    result.remove("_id");
    assertEquals(parse("{'date': ISODate('2017-02-08T12:00:00Z'), 'date_iso': ISODate('2017-02-08T12:00:00Z'),"
                       + "'date_timezone': ISODate('2017-01-01T04:46:12Z')}"), result);
}
 
Example 16
Source File: UpdateDocument.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> tDocumentClass, final CodecRegistry codecRegistry) {
    DocumentWriter writer = new DocumentWriter();

    MorphiaCodec codec = (MorphiaCodec) codecRegistry.get(entity.getClass());
    codec.encode(writer, entity, EncoderContext.builder().build());

    Document document = writer.getDocument();
    document.remove("_id");
    MappedField versionField = codec.getMappedClass().getVersionField();
    if (versionField != null) {
        document.remove(versionField.getMappedFieldName());
    }
    return document.toBsonDocument(Document.class, codecRegistry);
}
 
Example 17
Source File: MongoDocumentStorage.java    From lumongo with Apache License 2.0 5 votes vote down vote up
@Override
public Lumongo.ResultDocument getSourceDocument(String uniqueId, FetchType fetchType) throws Exception {
	if (!FetchType.NONE.equals(fetchType)) {
		MongoDatabase db = mongoClient.getDatabase(database);
		MongoCollection<Document> coll = db.getCollection(rawCollectionName);
		Document search = new Document(MongoConstants.StandardFields._ID, uniqueId);

		Document result = coll.find(search).first();

		if (null != result) {

			long timestamp = (long) result.remove(TIMESTAMP);

			ResultDocument.Builder dBuilder = ResultDocument.newBuilder();
			dBuilder.setUniqueId(uniqueId);
			dBuilder.setTimestamp(timestamp);

			if (result.containsKey(METADATA)) {
				Document metadata = (Document) result.remove(METADATA);
				for (String key : metadata.keySet()) {
					dBuilder.addMetadata(Metadata.newBuilder().setKey(key).setValue((String) metadata.get(key)));
				}
			}

			if (FetchType.FULL.equals(fetchType)) {
				BasicDBObject resultObj = new BasicDBObject();
				resultObj.putAll(result);

				ByteString document = ByteString.copyFrom(BSON.encode(resultObj));
				dBuilder.setDocument(document);

			}

			dBuilder.setIndexName(indexName);

			return dBuilder.build();
		}
	}
	return null;
}
 
Example 18
Source File: MongoSdkBase.java    From Mongodb-WeAdmin with Apache License 2.0 5 votes vote down vote up
/***
 * 插入单条记录
 * @param table  表连接
 * @param doc Document
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public  String insertOneDocument(MongoCollection table, Document doc) {
    if (doc == null){ return null;}
    doc.remove("_id");
    doc.put("_id", new ObjectId().toString());
    table.insertOne(doc);
    return doc.get("_id").toString();
}
 
Example 19
Source File: DynamicMockRestController.java    From microcks with Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/{service}/{version}/{resource}/{resourceId}", method = RequestMethod.PUT)
public ResponseEntity<String> updateResource(
      @PathVariable("service") String serviceName,
      @PathVariable("version") String version,
      @PathVariable("resource") String resource,
      @PathVariable("resourceId") String resourceId,
      @RequestParam(value="delay", required=false) Long delay,
      @RequestBody(required=true) String body,
      HttpServletRequest request
) {
   log.debug("Update resource '{}:{}' for service '{}-{}'", resource, resourceId, serviceName, version);
   long startTime = System.currentTimeMillis();

   serviceName = sanitizeServiceName(serviceName);

   MockContext mockContext = getMockContext(serviceName, version, "PUT /" + resource + "/:id");
   if (mockContext != null) {
      // Get the requested generic resource.
      GenericResource genericResource = genericResourceRepository.findById(resourceId).orElse(null);
      if (genericResource != null) {
         Document document = null;

         try {
            // Try parsing body payload that should be json.
            document = Document.parse(body);
            document.remove(ID_FIELD);

            // Now update the generic resource payload.
            genericResource.setPayload(document);

            genericResourceRepository.save(genericResource);
         } catch (JsonParseException jpe) {
            // Return a 422 code : unprocessable entity.
            return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
         }
         // Wait if specified before returning.
         waitForDelay(startTime, delay, mockContext);

         // Return the updated resource as well as a 200 code.
         return new ResponseEntity<>(transformToResourceJSON(genericResource), HttpStatus.OK);

      } else {
         // Wait if specified before returning.
         waitForDelay(startTime, delay, mockContext);

         // Return a 404 code : not found.
         return new ResponseEntity<>(HttpStatus.NOT_FOUND);
      }
   }

   // Return a 400 code : bad request.
   return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
 
Example 20
Source File: MongoDocumentStorage.java    From lumongo with Apache License 2.0 4 votes vote down vote up
public void getAssociatedDocuments(OutputStream outputstream, Document filter) throws IOException {
	Charset charset = Charset.forName("UTF-8");

	GridFSBucket gridFS = createGridFSConnection();
	GridFSFindIterable gridFSFiles = gridFS.find(filter);
	outputstream.write("{\n".getBytes(charset));
	outputstream.write(" \"associatedDocs\": [\n".getBytes(charset));

	boolean first = true;
	for (GridFSFile gridFSFile : gridFSFiles) {
		if (first) {
			first = false;
		}
		else {
			outputstream.write(",\n".getBytes(charset));
		}

		Document metadata = gridFSFile.getMetadata();

		String uniqueId = metadata.getString(DOCUMENT_UNIQUE_ID_KEY);
		String uniquieIdKeyValue = "  { \"uniqueId\": \"" + uniqueId + "\", ";
		outputstream.write(uniquieIdKeyValue.getBytes(charset));

		String filename = gridFSFile.getFilename();
		String filenameKeyValue = "\"filename\": \"" + filename + "\", ";
		outputstream.write(filenameKeyValue.getBytes(charset));

		Date uploadDate = gridFSFile.getUploadDate();
		String uploadDateKeyValue = "\"uploadDate\": {\"$date\":" + uploadDate.getTime() + "}";
		outputstream.write(uploadDateKeyValue.getBytes(charset));

		metadata.remove(TIMESTAMP);
		metadata.remove(COMPRESSED_FLAG);
		metadata.remove(DOCUMENT_UNIQUE_ID_KEY);
		metadata.remove(FILE_UNIQUE_ID_KEY);

		if (!metadata.isEmpty()) {
			String metaJson = metadata.toJson();
			String metaString = ", \"meta\": " + metaJson;
			outputstream.write(metaString.getBytes(charset));
		}

		outputstream.write(" }".getBytes(charset));

	}
	outputstream.write("\n ]\n}".getBytes(charset));
}