Search in sources :

Example 16 with Datastore

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);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Datastore(org.mongodb.morphia.Datastore) Test(org.junit.Test)

Example 17 with Datastore

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());
}
Also used : Datastore(org.mongodb.morphia.Datastore) NamedIndexOnValue(org.mongodb.morphia.entities.NamedIndexOnValue) IndexOnValue(org.mongodb.morphia.entities.IndexOnValue) UniqueIndexOnValue(org.mongodb.morphia.entities.UniqueIndexOnValue) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 18 with Datastore

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());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Datastore(org.mongodb.morphia.Datastore) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 19 with Datastore

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));
}
Also used : Datastore(org.mongodb.morphia.Datastore) City(org.mongodb.morphia.geo.City) Test(org.junit.Test)

Example 20 with Datastore

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));
}
Also used : Datastore(org.mongodb.morphia.Datastore) City(org.mongodb.morphia.geo.City) Test(org.junit.Test)

Aggregations

Datastore (org.mongodb.morphia.Datastore)21 Test (org.junit.Test)15 BasicDBObject (com.mongodb.BasicDBObject)5 DBObject (com.mongodb.DBObject)4 DBCollection (com.mongodb.DBCollection)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Key (org.mongodb.morphia.Key)2 City (org.mongodb.morphia.geo.City)2 UpdateResults (org.mongodb.morphia.query.UpdateResults)2 MongoClient (com.mongodb.MongoClient)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Morphia (org.mongodb.morphia.Morphia)1 IndexOnValue (org.mongodb.morphia.entities.IndexOnValue)1 NamedIndexOnValue (org.mongodb.morphia.entities.NamedIndexOnValue)1 UniqueIndexOnValue (org.mongodb.morphia.entities.UniqueIndexOnValue)1