Search in sources :

Example 1 with Datastore

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

the class Employee method main.

public static void main(final String[] args) throws UnknownHostException {
    final Morphia morphia = new Morphia();
    // tell morphia where to find your classes
    // can be called multiple times with different packages or classes
    morphia.mapPackage("org.mongodb.morphia.example");
    // create the Datastore connecting to the database running on the default port on the local host
    final Datastore datastore = morphia.createDatastore(new MongoClient(), "morphia_example");
    datastore.getDB().dropDatabase();
    datastore.ensureIndexes();
    final Employee elmer = new Employee("Elmer Fudd", 50000.0);
    datastore.save(elmer);
    final Employee daffy = new Employee("Daffy Duck", 40000.0);
    datastore.save(daffy);
    final Employee pepe = new Employee("Pepé Le Pew", 25000.0);
    datastore.save(pepe);
    elmer.getDirectReports().add(daffy);
    elmer.getDirectReports().add(pepe);
    datastore.save(elmer);
    Query<Employee> query = datastore.find(Employee.class);
    final List<Employee> employees = query.asList();
    Assert.assertEquals(3, employees.size());
    List<Employee> underpaid = datastore.find(Employee.class).filter("salary <=", 30000).asList();
    Assert.assertEquals(1, underpaid.size());
    underpaid = datastore.find(Employee.class).field("salary").lessThanOrEq(30000).asList();
    Assert.assertEquals(1, underpaid.size());
    final Query<Employee> underPaidQuery = datastore.find(Employee.class).filter("salary <=", 30000);
    final UpdateOperations<Employee> updateOperations = datastore.createUpdateOperations(Employee.class).inc("salary", 10000);
    final UpdateResults results = datastore.update(underPaidQuery, updateOperations);
    Assert.assertEquals(1, results.getUpdatedCount());
    final Query<Employee> overPaidQuery = datastore.find(Employee.class).filter("salary >", 100000);
    datastore.delete(overPaidQuery);
}
Also used : MongoClient(com.mongodb.MongoClient) Morphia(org.mongodb.morphia.Morphia) Datastore(org.mongodb.morphia.Datastore) UpdateResults(org.mongodb.morphia.query.UpdateResults)

Example 2 with Datastore

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

the class CollectionObjectReference method syncKeys.

private void syncKeys() {
    final Datastore ds = getDatastore();
    listOfKeys.clear();
    for (final Object e : ((Collection) object)) {
        listOfKeys.add(ds.getKey(e));
    }
}
Also used : Datastore(org.mongodb.morphia.Datastore)

Example 3 with Datastore

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

the class JsonList method empties.

private void empties() {
    Datastore ds = getDs();
    ds.delete(ds.find(Employee.class));
    Employee employee = new Employee();
    employee.byteList = asList((byte) 1, (byte) 2);
    ds.save(employee);
    Employee loaded = ds.find(Employee.class).get();
    assertEquals(employee.byteList, loaded.byteList);
    assertNull(loaded.floatList);
}
Also used : Datastore(org.mongodb.morphia.Datastore)

Example 4 with Datastore

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

the class TestJavaMaps method empties.

private void empties() {
    Datastore ds = getDs();
    ds.delete(ds.find(Employee.class));
    Employee employee = new Employee();
    HashMap<String, Byte> byteMap = new HashMap<String, Byte>();
    byteMap.put("b", (byte) 1);
    employee.byteMap = byteMap;
    ds.save(employee);
    Employee loaded = ds.find(Employee.class).get();
    assertEquals(Byte.valueOf((byte) 1), loaded.byteMap.get("b"));
    assertNull(loaded.floatMap);
}
Also used : Datastore(org.mongodb.morphia.Datastore) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with Datastore

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

the class TestEmbeddedValidation method testDottedNames.

@Test
public void testDottedNames() {
    ParentType parentType = new ParentType();
    EmbeddedSubtype embedded = new EmbeddedSubtype();
    embedded.setText("text");
    embedded.setNumber(42L);
    embedded.setFlag(true);
    parentType.setEmbedded(embedded);
    Datastore ds = getDs();
    ds.save(parentType);
    Query<ParentType> query = ds.find(ParentType.class).disableValidation().field("embedded.flag").equal(true);
    Assert.assertEquals(parentType, query.get());
}
Also used : 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