Example usage for org.springframework.data.mongodb.core MongoTemplate createCollection

List of usage examples for org.springframework.data.mongodb.core MongoTemplate createCollection

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core MongoTemplate createCollection.

Prototype

public MongoCollection<Document> createCollection(final String collectionName) 

Source Link

Usage

From source file:example.springdata.mongodb.fluent.ApplicationConfiguration.java

@Bean
CommandLineRunner init(MongoTemplate template) {

    return (args) -> {

        if (template.collectionExists(COLLECTION)) {
            template.dropCollection(COLLECTION);
        }//from w  w  w.java  2  s .com

        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);
    };
}