use of org.mongodb.morphia.Datastore in project morphia by mongodb.
the class TestMaxMin method testMin.
@Test
@SuppressWarnings("deprecation")
public void testMin() {
final IndexedEntity a = new IndexedEntity("a");
final IndexedEntity b = new IndexedEntity("b");
final IndexedEntity c = new IndexedEntity("c");
Datastore ds = getDs();
ds.save(a);
ds.save(b);
ds.save(c);
Assert.assertEquals("last", b.id, ds.find(IndexedEntity.class).order("id").lowerIndexBound(new BasicDBObject("testField", "b")).get().id);
Assert.assertEquals("last", b.id, ds.find(IndexedEntity.class).order("id").get(new FindOptions().modifier("$min", new BasicDBObject("testField", "b"))).id);
}
use of org.mongodb.morphia.Datastore in project morphia by mongodb.
the class TestIndexed method shouldNotCreateAnIndexWhenAnIndexedEntityIsMarkedAsNotSaved.
@Test
public void shouldNotCreateAnIndexWhenAnIndexedEntityIsMarkedAsNotSaved() {
// given
getMorphia().map(IndexOnValue.class, NoIndexes.class);
Datastore ds = getDs();
// when
ds.ensureIndexes();
ds.save(new IndexOnValue());
ds.save(new NoIndexes());
// then
List<DBObject> indexes = getDb().getCollection("NoIndexes").getIndexInfo();
assertEquals(1, indexes.size());
}
use of org.mongodb.morphia.Datastore in project morphia by mongodb.
the class TestIndexes method testIndexes.
@Test
public void testIndexes() {
final Datastore datastore = getDs();
datastore.delete(datastore.find(TestWithIndexOption.class));
final DBCollection indexOptionColl = getDs().getCollection(TestWithIndexOption.class);
indexOptionColl.drop();
assertEquals(0, indexOptionColl.getIndexInfo().size());
final DBCollection depIndexColl = getDs().getCollection(TestWithDeprecatedIndex.class);
depIndexColl.drop();
assertEquals(0, depIndexColl.getIndexInfo().size());
final DBCollection hashIndexColl = getDs().getCollection(TestWithHashedIndex.class);
hashIndexColl.drop();
assertEquals(0, hashIndexColl.getIndexInfo().size());
if (serverIsAtLeastVersion(3.4)) {
datastore.ensureIndexes(TestWithIndexOption.class, true);
assertEquals(2, indexOptionColl.getIndexInfo().size());
List<DBObject> indexInfo = indexOptionColl.getIndexInfo();
assertBackground(indexInfo);
for (DBObject dbObject : indexInfo) {
if (dbObject.get("name").equals("collated")) {
assertEquals(BasicDBObject.parse("{ name : { $exists : true } }"), dbObject.get("partialFilterExpression"));
BasicDBObject collation = (BasicDBObject) dbObject.get("collation");
assertEquals("en_US", collation.get("locale"));
assertEquals("upper", collation.get("caseFirst"));
assertEquals("shifted", collation.get("alternate"));
Assert.assertTrue(collation.getBoolean("backwards"));
assertEquals("upper", collation.get("caseFirst"));
Assert.assertTrue(collation.getBoolean("caseLevel"));
assertEquals("space", collation.get("maxVariable"));
Assert.assertTrue(collation.getBoolean("normalization"));
Assert.assertTrue(collation.getBoolean("numericOrdering"));
assertEquals(5, collation.get("strength"));
}
}
}
datastore.ensureIndexes(TestWithDeprecatedIndex.class, true);
assertEquals(2, depIndexColl.getIndexInfo().size());
assertBackground(depIndexColl.getIndexInfo());
datastore.ensureIndexes(TestWithHashedIndex.class);
assertEquals(2, hashIndexColl.getIndexInfo().size());
assertHashed(hashIndexColl.getIndexInfo());
}
use of org.mongodb.morphia.Datastore in project morphia by mongodb.
the class GeoQueriesTest method shouldFindCitiesCloseToAGivenPointWithinARadiusOfMeters.
@Test
public void shouldFindCitiesCloseToAGivenPointWithinARadiusOfMeters() {
// given
double latitude = 51.5286416;
double longitude = -0.1015987;
Datastore datastore = getDs();
City london = new City("London", point(latitude, longitude));
datastore.save(london);
City manchester = new City("Manchester", point(53.4722454, -2.2235922));
datastore.save(manchester);
City sevilla = new City("Sevilla", point(37.3753708, -5.9550582));
datastore.save(sevilla);
getDs().ensureIndexes();
// when
List<City> citiesOrderedByDistanceFromLondon = datastore.find(City.class).field("location").near(pointBuilder().latitude(latitude).longitude(longitude).build(), 200000).asList();
// then
assertThat(citiesOrderedByDistanceFromLondon.size(), is(1));
assertThat(citiesOrderedByDistanceFromLondon.get(0), is(london));
}
use of org.mongodb.morphia.Datastore in project morphia by mongodb.
the class GeoNearQueriesTest method shouldFindCitiesCloseToAGivenPointWithinARadiusOfMeters.
@Test
public void shouldFindCitiesCloseToAGivenPointWithinARadiusOfMeters() {
// given
double latitude = 51.5286416;
double longitude = -0.1015987;
Datastore datastore = getDs();
City london = new City("London", point(latitude, longitude));
datastore.save(london);
City manchester = new City("Manchester", point(53.4722454, -2.2235922));
datastore.save(manchester);
City sevilla = new City("Sevilla", point(37.3753708, -5.9550582));
datastore.save(sevilla);
getDs().ensureIndexes();
// when
List<City> citiesOrderedByDistanceFromLondon = datastore.find(City.class).field("location").near(GeoJson.point(latitude, longitude), 200000).asList();
// then
assertThat(citiesOrderedByDistanceFromLondon.size(), is(1));
assertThat(citiesOrderedByDistanceFromLondon.get(0), is(london));
}
Aggregations