/** * Clean up resources on shutdown */ @PreDestroy public void cleanUpWhenShuttingDown() { // Disable profiling setProfilingLevel(0); if (operations.collectionExists(SYSTEM_PROFILE_DB)) { operations.dropCollection(SYSTEM_PROFILE_DB); } }
private StatusItemDTO checkMongoDBStatus() { boolean isOk = true; final String description = messageSource.getMessage("status.mongo.desc", new Object[] {}, LocaleContextHolder.getLocale()); final String errorDesc = messageSource.getMessage("status.mongo.desc.error", new Object[] {}, LocaleContextHolder.getLocale()); final String successDesc = messageSource.getMessage("status.mongo.desc.success", new Object[] {}, LocaleContextHolder.getLocale()); try { mongoOps.collectionExists(User.class); } catch (final Exception e) { isOk = false; } return new StatusItemDTO(1, "MongoDB database", description, isOk, isOk ? successDesc : errorDesc); }
private void createCollectionIfNotExists(String collection, MongoOperations mongoTemplate) { Assert.notNull(collection); if (!mongoTemplate.collectionExists(collection)) { mongoTemplate.createCollection(collection); } }
/** * Drops collections with specified name. * @param collectionName */ protected void dropCollection(String collectionName) { if (mongoTemplate.collectionExists(collectionName)) { mongoTemplate.dropCollection(collectionName); } }
@Override public void afterPropertiesSet() throws Exception { if (this.dropCollection) { log.debug("Dropping database collection: {}", this.collectionName); this.mongoTemplate.dropCollection(this.collectionName); } if (!this.mongoTemplate.collectionExists(this.collectionName)) { log.debug("Creating database collection: {}", this.collectionName); this.mongoTemplate.createCollection(this.collectionName); } }
/** * Create collection. * * @param mongoTemplate the mongo template * @param collectionName the collection name * @param dropCollection the drop collection */ public void createCollection(final MongoOperations mongoTemplate, final String collectionName, final boolean dropCollection) { if (dropCollection) { LOGGER.trace("Dropping database collection: [{}]", collectionName); mongoTemplate.dropCollection(collectionName); } if (!mongoTemplate.collectionExists(collectionName)) { LOGGER.trace("Creating database collection: [{}]", collectionName); mongoTemplate.createCollection(collectionName); } }
Collection<Object> collection = (Collection<Object>) fieldValue; for (Object item : collection) { if (mongoOperations.collectionExists(item.getClass())) { mongoOperations.save(item); logger.debug("Set of {}s saved.", item.getClass().getSimpleName()); if (mongoOperations.collectionExists(fieldValue.getClass())) { mongoOperations.save(fieldValue); logger.debug("{} saved.", fieldValue.getClass().getSimpleName());
public void run() { if (mongoOperations.collectionExists(Person.class)) { mongoOperations.dropCollection(Person.class); } mongoOperations.createCollection(Person.class); Person p = new Person("John", 39); Account a = new Account("1234-59873-893-1", Account.Type.SAVINGS, 123.45D); p.getAccounts().add(a); mongoOperations.insert(p); List<Person> results = mongoOperations.findAll(Person.class); System.out.println("Results: " + results); }