@@ -15,14 +15,8 @@
|
||||
*/
|
||||
package example.springdata.mongodb.fluent;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@Data
|
||||
class Human {
|
||||
|
||||
final String firstname;
|
||||
final String lastname;
|
||||
record Human(String firstname, String lastname) {
|
||||
}
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
*/
|
||||
package example.springdata.mongodb.fluent;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
|
||||
@@ -24,9 +22,6 @@ import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
@Data
|
||||
public class Planet {
|
||||
public record Planet(@Id String name, @GeoSpatialIndexed Point coordinates) {
|
||||
|
||||
final @Id String name;
|
||||
final @GeoSpatialIndexed Point coordinates;
|
||||
}
|
||||
|
||||
@@ -40,30 +40,30 @@ public class ApplicationConfiguration {
|
||||
template.dropCollection(COLLECTION);
|
||||
}
|
||||
|
||||
GeospatialIndex index = new GeospatialIndex("homePlanet.coordinates") //
|
||||
var 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));
|
||||
var alderaan = new Planet("alderaan", new Point(-73.9667, 40.78));
|
||||
var stewjon = new Planet("stewjon", new Point(-73.9836, 40.7538));
|
||||
var tatooine = new Planet("tatooine", new Point(-73.9928, 40.7193));
|
||||
|
||||
Jedi anakin = new Jedi("anakin", "skywalker");
|
||||
var anakin = new Jedi("anakin", "skywalker");
|
||||
anakin.setHomePlanet(tatooine);
|
||||
|
||||
Jedi luke = new Jedi("luke", "skywalker");
|
||||
var luke = new Jedi("luke", "skywalker");
|
||||
luke.setHomePlanet(tatooine);
|
||||
|
||||
Jedi leia = new Jedi("leia", "organa");
|
||||
var leia = new Jedi("leia", "organa");
|
||||
leia.setHomePlanet(alderaan);
|
||||
|
||||
Jedi obiWan = new Jedi("obi-wan", "kenobi");
|
||||
var obiWan = new Jedi("obi-wan", "kenobi");
|
||||
obiWan.setHomePlanet(stewjon);
|
||||
|
||||
Human han = new Human("han", "solo");
|
||||
var han = new Human("han", "solo");
|
||||
|
||||
template.save(anakin, COLLECTION);
|
||||
template.save(luke, COLLECTION);
|
||||
|
||||
@@ -111,7 +111,7 @@ public class FluentMongoApiTests {
|
||||
@Test
|
||||
public void fetchInterfaceProjection() {
|
||||
|
||||
Sith anakin = mongoOps.query(SWCharacter.class) // SWCharacter does only define the collection, id and name
|
||||
var anakin = mongoOps.query(SWCharacter.class) // SWCharacter does only define the collection, id and name
|
||||
.as(Sith.class) // use an interface as return type to create a projection
|
||||
.matching(query(where("firstname").is("anakin"))) // so properties are taken as is
|
||||
.oneValue();
|
||||
@@ -179,7 +179,7 @@ public class FluentMongoApiTests {
|
||||
@Test
|
||||
public void geoNearQuery() {
|
||||
|
||||
GeoResults<Jedi> results = mongoOps.query(SWCharacter.class) // SWCharacter defines collection, id and name
|
||||
var results = mongoOps.query(SWCharacter.class) // SWCharacter defines collection, id and name
|
||||
.as(Jedi.class) // but we want to map the results to Jedi
|
||||
.near(alderaanWithin3Parsecs) // and find those with home planet near alderaan
|
||||
.all();
|
||||
@@ -204,7 +204,7 @@ public class FluentMongoApiTests {
|
||||
@Test
|
||||
public void querySpecificCollection() {
|
||||
|
||||
List<Human> skywalkers = mongoOps.query(Human.class) // Human does not define a collection via @Document
|
||||
var skywalkers = mongoOps.query(Human.class) // Human does not define a collection via @Document
|
||||
.inCollection("star-wars") // so we set an explicit collection name
|
||||
.matching(query(where("lastname").is("skywalker"))) // to find all documents with a matching "lastname"
|
||||
.all();
|
||||
@@ -218,7 +218,7 @@ public class FluentMongoApiTests {
|
||||
@Test
|
||||
public void justInsertOne() {
|
||||
|
||||
SWCharacter chewbacca = new SWCharacter("Chewbacca");
|
||||
var chewbacca = new SWCharacter("Chewbacca");
|
||||
|
||||
mongoOps.insert(SWCharacter.class).one(chewbacca);
|
||||
|
||||
@@ -241,7 +241,7 @@ public class FluentMongoApiTests {
|
||||
@Test
|
||||
public void updateAndUpsert() {
|
||||
|
||||
UpdateResult result = mongoOps.update(Jedi.class) // Jedi defines the collection and field mapping
|
||||
var result = mongoOps.update(Jedi.class) // Jedi defines the collection and field mapping
|
||||
.matching(query(where("lastname").is("windu"))) // so "last" maps to "lastname".
|
||||
.apply(update("name", "mence")) // We'll update the "firstname" to "mence"
|
||||
.upsert(); // and add a new document if it does not exist already.
|
||||
|
||||
Reference in New Issue
Block a user