Search in sources :

Example 11 with Datastore

use of org.mongodb.morphia.Datastore in project morphia by mongodb.

the class ReferenceTest method testReferencesWithoutMapping.

@Test
public void testReferencesWithoutMapping() {
    Child child1 = new Child();
    getDs().save(child1);
    Parent parent1 = new Parent();
    parent1.children.add(child1);
    getDs().save(parent1);
    List<Parent> parentList = getDs().find(Parent.class).asList();
    Assert.assertEquals(1, parentList.size());
    // reset Datastore to reset internal Mapper cache, so Child class
    // already cached by previous save is cleared
    Datastore localDs = getMorphia().createDatastore(getMongoClient(), new Mapper(), getDb().getName());
    parentList = localDs.find(Parent.class).asList();
    Assert.assertEquals(1, parentList.size());
}
Also used : Datastore(org.mongodb.morphia.Datastore) Test(org.junit.Test)

Example 12 with Datastore

use of org.mongodb.morphia.Datastore in project morphia by mongodb.

the class TestMaxMin method testMaxCompoundIndex.

@Test
@SuppressWarnings("deprecation")
public void testMaxCompoundIndex() {
    final IndexedEntity a1 = new IndexedEntity("a");
    final IndexedEntity a2 = new IndexedEntity("a");
    final IndexedEntity b1 = new IndexedEntity("b");
    final IndexedEntity b2 = new IndexedEntity("b");
    final IndexedEntity c1 = new IndexedEntity("c");
    final IndexedEntity c2 = new IndexedEntity("c");
    Datastore ds = getDs();
    ds.save(a1);
    ds.save(a2);
    ds.save(b1);
    ds.save(b2);
    ds.save(c1);
    ds.save(c2);
    List<IndexedEntity> l = ds.find(IndexedEntity.class).order("testField, id").upperIndexBound(new BasicDBObject("testField", "b").append("_id", b2.id)).asList();
    Assert.assertEquals("size", 3, l.size());
    Assert.assertEquals("item", b1.id, l.get(2).id);
    l = ds.find(IndexedEntity.class).order("testField, id").asList(new FindOptions().modifier("$max", new BasicDBObject("testField", "b").append("_id", b2.id)));
    Assert.assertEquals("size", 3, l.size());
    Assert.assertEquals("item", b1.id, l.get(2).id);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Datastore(org.mongodb.morphia.Datastore) Test(org.junit.Test)

Example 13 with Datastore

use of org.mongodb.morphia.Datastore in project morphia by mongodb.

the class MapObjectReference method syncKeys.

@SuppressWarnings("unchecked")
private void syncKeys() {
    final Datastore ds = getDatastore();
    keyMap.clear();
    final Map<Object, Object> map = (Map) object;
    for (final Map.Entry<Object, Object> e : map.entrySet()) {
        keyMap.put(e.getKey(), ds.getKey(e.getValue()));
    }
}
Also used : Datastore(org.mongodb.morphia.Datastore) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with Datastore

use of org.mongodb.morphia.Datastore in project morphia by mongodb.

the class TestMaxMin method testMinCompoundIndex.

@Test
@SuppressWarnings("deprecation")
public void testMinCompoundIndex() {
    final IndexedEntity a1 = new IndexedEntity("a");
    final IndexedEntity a2 = new IndexedEntity("a");
    final IndexedEntity b1 = new IndexedEntity("b");
    final IndexedEntity b2 = new IndexedEntity("b");
    final IndexedEntity c1 = new IndexedEntity("c");
    final IndexedEntity c2 = new IndexedEntity("c");
    Datastore ds = getDs();
    ds.save(a1);
    ds.save(a2);
    ds.save(b1);
    ds.save(b2);
    ds.save(c1);
    ds.save(c2);
    List<IndexedEntity> l = ds.find(IndexedEntity.class).order("testField, id").lowerIndexBound(new BasicDBObject("testField", "b").append("_id", b1.id)).asList();
    Assert.assertEquals("size", 4, l.size());
    Assert.assertEquals("item", b1.id, l.get(0).id);
    l = ds.find(IndexedEntity.class).order("testField, id").asList(new FindOptions().modifier("$min", new BasicDBObject("testField", "b").append("_id", b1.id)));
    Assert.assertEquals("size", 4, l.size());
    Assert.assertEquals("item", b1.id, l.get(0).id);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Datastore(org.mongodb.morphia.Datastore) Test(org.junit.Test)

Example 15 with Datastore

use of org.mongodb.morphia.Datastore in project morphia by mongodb.

the class TestMaxMin method testMax.

@Test
@SuppressWarnings("deprecation")
public void testMax() {
    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").upperIndexBound(new BasicDBObject("testField", "c")).get().id);
    Assert.assertEquals("last", b.id, ds.find(IndexedEntity.class).order("-id").get(new FindOptions().modifier("$max", new BasicDBObject("testField", "c"))).id);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Datastore(org.mongodb.morphia.Datastore) 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