Search in sources :

Example 46 with Datastore

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

the class TestMaxMin method testMaxCompoundIndex.

@Test
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).iterator(new FindOptions().sort(ascending("testField"), ascending("id")).hint(new Document("testField", 1).append("_id", 1)).max(new Document("testField", "b").append("_id", b2.id))).toList();
    Assert.assertEquals(l.size(), 3, "size");
    Assert.assertEquals(l.get(2).id, b1.id, "item");
    l = ds.find(IndexedEntity.class).iterator(new FindOptions().sort(ascending("testField"), ascending("id")).hint(new Document("testField", 1).append("_id", 1)).max(new Document("testField", "b").append("_id", b2.id))).toList();
    Assert.assertEquals(l.size(), 3, "size");
    Assert.assertEquals(l.get(2).id, b1.id, "item");
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Document(org.bson.Document) Test(org.testng.annotations.Test)

Aggregations

Datastore (dev.morphia.Datastore)46 Test (org.testng.annotations.Test)38 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)12 FindOptions (dev.morphia.query.FindOptions)12 ArrayList (java.util.ArrayList)9 Document (org.bson.Document)9 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)6 UpdateOptions (dev.morphia.UpdateOptions)5 UpdateResult (com.mongodb.client.result.UpdateResult)4 MapperOptions (dev.morphia.mapping.MapperOptions)4 ObjectId (org.bson.types.ObjectId)4 Mapper (dev.morphia.mapping.Mapper)3 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)3 Circle (dev.morphia.test.models.Circle)3 Rectangle (dev.morphia.test.models.Rectangle)3 Versioned (dev.morphia.test.models.versioned.Versioned)3 Point (com.mongodb.client.model.geojson.Point)2 Position (com.mongodb.client.model.geojson.Position)2 DeleteOptions (dev.morphia.DeleteOptions)2 Key (dev.morphia.Key)2