Created
March 16, 2016 13:15
-
-
Save mp911de/ff15f704b9154cab0d0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.springframework.data.mongodb.core.query.Criteria.where; | |
import java.net.UnknownHostException; | |
import java.util.List; | |
import javax.annotation.PreDestroy; | |
import lombok.Data; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.boot.test.SpringApplicationConfiguration; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.geo.Box; | |
import org.springframework.data.geo.Point; | |
import org.springframework.data.mongodb.core.MongoOperations; | |
import org.springframework.data.mongodb.core.SimpleMongoDbFactory; | |
import org.springframework.data.mongodb.core.aggregation.Aggregation; | |
import org.springframework.data.mongodb.core.aggregation.AggregationResults; | |
import org.springframework.data.mongodb.core.aggregation.TypedAggregation; | |
import org.springframework.data.mongodb.core.mapping.Document; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import com.mongodb.DBObject; | |
import com.mongodb.MongoClient; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@SpringApplicationConfiguration(classes = SpringDataMongoDBTestCase.Config.class) | |
public class SpringDataMongoDBTestCase { | |
@Autowired | |
MongoOperations mongoOperations; | |
@Test | |
public void test() throws Exception { | |
Box box = new Box(new Point(0, 0), new Point(20, 20)); | |
TypedAggregation<Model> aggregation = Aggregation.newAggregation(Model.class, | |
Aggregation.match(where("positions.location").within(box)), | |
Aggregation.project("modelId", "positions", "lods", "maxInstances").and("positions.location").as("locs"), | |
Aggregation.unwind("locs")); | |
AggregationResults<Model> results = mongoOperations.aggregate(aggregation, "model", Model.class); | |
List<Model> modelsInside = results.getMappedResults(); | |
} | |
@Data | |
@Document(collection = "model") | |
public static class Model { | |
private Long modelId; | |
private List<Position> positions; | |
private List<DBObject> lods; | |
private Integer maxInstances; | |
} | |
@Data | |
public static class Position { | |
// private Location location; | |
private double[] location; | |
private Float z; | |
private Float rotation; | |
} | |
@SpringBootApplication | |
@Configuration | |
static class Config { | |
private MongoClient mongo; | |
@Bean | |
public SimpleMongoDbFactory mongoDbFactory(MongoClient mongo) throws Exception { | |
return new SimpleMongoDbFactory(mongo, "playground"); | |
} | |
@PreDestroy | |
public void close() { | |
if (this.mongo != null) { | |
this.mongo.close(); | |
} | |
} | |
@Bean | |
public MongoClient mongo() throws UnknownHostException { | |
this.mongo = new MongoClient("localhost"); | |
return this.mongo; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment