Java Code Examples for org.springframework.data.mongodb.core.MongoTemplate#save()

The following examples show how to use org.springframework.data.mongodb.core.MongoTemplate#save() . 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: InitialSetupMigration.java    From java-microservices-examples with Apache License 2.0 7 votes vote down vote up
@ChangeSet(order = "01", author = "initiator", id = "01-addAuthorities")
public void addAuthorities(MongoTemplate mongoTemplate) {
    Authority adminAuthority = new Authority();
    adminAuthority.setName(AuthoritiesConstants.ADMIN);
    Authority userAuthority = new Authority();
    userAuthority.setName(AuthoritiesConstants.USER);
    mongoTemplate.save(adminAuthority);
    mongoTemplate.save(userAuthority);
}
 
Example 2
Source File: MongoEnvironmentRepositoryTests.java    From spring-cloud-config-server-mongodb with Apache License 2.0 6 votes vote down vote up
@Test
public void defaultRepo() {
	// Prepare context
	Map<String, Object> props = new HashMap<>();
	props.put("spring.data.mongodb.database", "testdb");
	context = new SpringApplicationBuilder(TestConfiguration.class).web(WebApplicationType.NONE).properties(props).run();
	// Prepare test
	MongoTemplate mongoTemplate = this.context.getBean(MongoTemplate.class);
	mongoTemplate.dropCollection("testapp");
	MongoPropertySource ps = new MongoPropertySource();
	ps.getSource().put("testkey", "testval");
	mongoTemplate.save(ps, "testapp");
	// Test
	EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
	Environment environment = repository.findOne("testapp", "default", null);
	assertEquals("testapp-default", environment.getPropertySources().get(0).getName());
	assertEquals(1, environment.getPropertySources().size());
	assertEquals(true, environment.getPropertySources().get(0).getSource().containsKey("testkey"));
	assertEquals("testval", environment.getPropertySources().get(0).getSource().get("testkey"));
}
 
Example 3
Source File: MongoEnvironmentRepositoryTests.java    From spring-cloud-config-server-mongodb with Apache License 2.0 6 votes vote down vote up
@Test
public void nestedPropertySource() {
	// Prepare context
	Map<String, Object> props = new HashMap<>();
	props.put("spring.data.mongodb.database", "testdb");
	context = new SpringApplicationBuilder(TestConfiguration.class).web(WebApplicationType.NONE).properties(props).run();
	// Prepare test
	MongoTemplate mongoTemplate = this.context.getBean(MongoTemplate.class);
	mongoTemplate.dropCollection("testapp");
	MongoPropertySource ps = new MongoPropertySource();
	Map<String, String> inner = new HashMap<String, String>();
	inner.put("inner", "value");
	ps.getSource().put("outer", inner);
	mongoTemplate.save(ps, "testapp");
	// Test
	EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
	Environment environment = repository.findOne("testapp", "default", null);
	assertEquals("testapp-default", environment.getPropertySources().get(0).getName());
	assertEquals(1, environment.getPropertySources().size());
	assertEquals(true, environment.getPropertySources().get(0).getSource().containsKey("outer.inner"));
	assertEquals("value", environment.getPropertySources().get(0).getSource().get("outer.inner"));
}
 
Example 4
Source File: MongoEnvironmentRepositoryTests.java    From spring-cloud-config-server-mongodb with Apache License 2.0 6 votes vote down vote up
@Test
public void repoWithProfileAndLabelInSource() {
	// Prepare context
	Map<String, Object> props = new HashMap<>();
	props.put("spring.data.mongodb.database", "testdb");
	context = new SpringApplicationBuilder(TestConfiguration.class).web(WebApplicationType.NONE).properties(props).run();
	// Prepare test
	MongoTemplate mongoTemplate = this.context.getBean(MongoTemplate.class);
	mongoTemplate.dropCollection("testapp");
	MongoPropertySource ps = new MongoPropertySource();
	ps.setProfile("confprofile");
	ps.setLabel("conflabel");
	ps.getSource().put("profile", "sourceprofile");
	ps.getSource().put("label", "sourcelabel");
	mongoTemplate.save(ps, "testapp");
	// Test
	EnvironmentRepository repository = this.context.getBean(EnvironmentRepository.class);
	Environment environment = repository.findOne("testapp", "confprofile", "conflabel");
	assertEquals(1, environment.getPropertySources().size());
	assertEquals("testapp-confprofile-conflabel", environment.getPropertySources().get(0).getName());
	assertEquals(true, environment.getPropertySources().get(0).getSource().containsKey("profile"));
	assertEquals("sourceprofile", environment.getPropertySources().get(0).getSource().get("profile"));
	assertEquals(true, environment.getPropertySources().get(0).getSource().containsKey("label"));
	assertEquals("sourcelabel", environment.getPropertySources().get(0).getSource().get("label"));
}
 
Example 5
Source File: DbRefMappingBenchmark.java    From spring-data-dev-tools with Apache License 2.0 6 votes vote down vote up
@Setup
public void setUp() throws Exception {

	client = MongoClients.create();
	template = new MongoTemplate(client, DB_NAME);

	List<RefObject> refObjects = new ArrayList<>();
	for (int i = 0; i < 1; i++) {
		RefObject o = new RefObject();
		template.save(o);
		refObjects.add(o);
	}

	ObjectWithDBRef singleDBRef = new ObjectWithDBRef();
	singleDBRef.ref = refObjects.iterator().next();
	template.save(singleDBRef);

	ObjectWithDBRef multipleDBRefs = new ObjectWithDBRef();
	multipleDBRefs.refList = refObjects;
	template.save(multipleDBRefs);

	queryObjectWithDBRef = query(where("id").is(singleDBRef.id));
	queryObjectWithDBRefList = query(where("id").is(multipleDBRefs.id));
}
 
Example 6
Source File: ProjectionsBenchmark.java    From spring-data-dev-tools with Apache License 2.0 6 votes vote down vote up
@Setup
public void setUp() {

	client = MongoClients.create();
	template = new MongoTemplate(client, DB_NAME);

	source = new Person();
	source.firstname = "luke";
	source.lastname = "skywalker";

	source.address = new Address();
	source.address.street = "melenium falcon 1";
	source.address.city = "deathstar";

	template.save(source, COLLECTION_NAME);

	asPerson = template.query(Person.class).inCollection(COLLECTION_NAME);
	asDtoProjection = template.query(Person.class).inCollection(COLLECTION_NAME).as(DtoProjection.class);
	asClosedProjection = template.query(Person.class).inCollection(COLLECTION_NAME).as(ClosedProjection.class);
	asOpenProjection = template.query(Person.class).inCollection(COLLECTION_NAME).as(OpenProjection.class);

	asPersonWithFieldsRestriction = template.query(Person.class).inCollection(COLLECTION_NAME)
			.matching(new BasicQuery(new Document(), fields));

	mongoCollection = client.getDatabase(DB_NAME).getCollection(COLLECTION_NAME);
}
 
Example 7
Source File: InitialSetupMigration.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 5 votes vote down vote up
@ChangeSet(order = "01", author = "initiator", id = "01-addAuthorities")
public void addAuthorities(MongoTemplate mongoTemplate) {
    Authority adminAuthority = new Authority();
    adminAuthority.setName(AuthoritiesConstants.ADMIN);
    Authority userAuthority = new Authority();
    userAuthority.setName(AuthoritiesConstants.USER);
    mongoTemplate.save(adminAuthority);
    mongoTemplate.save(userAuthority);
}
 
Example 8
Source File: MongoDbSpringIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@DisplayName("Given object When save object using MongoDB template Then object can be found")
@Test
public void test(@Autowired MongoTemplate mongoTemplate) {
    // given
    DBObject objectToSave = BasicDBObjectBuilder.start()
        .add("key", "value")
        .get();

    // when
    mongoTemplate.save(objectToSave, "collection");

    // then
    assertThat(mongoTemplate.findAll(DBObject.class, "collection")).extracting("key")
        .containsOnly("value");
}
 
Example 9
Source File: ApplicationConfiguration.java    From spring-data-examples with Apache License 2.0 4 votes vote down vote up
@Bean
CommandLineRunner init(MongoTemplate template) {

	return (args) -> {

		if (template.collectionExists(COLLECTION)) {
			template.dropCollection(COLLECTION);
		}

		GeospatialIndex index = new GeospatialIndex("homePlanet.coordinates") //
				.typed(GeoSpatialIndexType.GEO_2DSPHERE) //
				.named("planet-coordinate-idx");

		template.createCollection(COLLECTION);
		template.indexOps(SWCharacter.class).ensureIndex(index);

		Planet alderaan = new Planet("alderaan", new Point(-73.9667, 40.78));
		Planet stewjon = new Planet("stewjon", new Point(-73.9836, 40.7538));
		Planet tatooine = new Planet("tatooine", new Point(-73.9928, 40.7193));

		Jedi anakin = new Jedi("anakin", "skywalker");
		anakin.setHomePlanet(tatooine);

		Jedi luke = new Jedi("luke", "skywalker");
		luke.setHomePlanet(tatooine);

		Jedi leia = new Jedi("leia", "organa");
		leia.setHomePlanet(alderaan);

		Jedi obiWan = new Jedi("obi-wan", "kenobi");
		obiWan.setHomePlanet(stewjon);

		Human han = new Human("han", "solo");

		template.save(anakin, COLLECTION);
		template.save(luke, COLLECTION);
		template.save(leia, COLLECTION);
		template.save(obiWan, COLLECTION);
		template.save(han, COLLECTION);
	};
}