Search in sources :

Example 1 with Account

use of org.springframework.data.mongodb.examples.hello.domain.Account in project spring-data-document-examples by spring-projects.

the class HelloMongo method run.

public void run() {
    if (mongoOperations.collectionExists(Person.class)) {
        mongoOperations.dropCollection(Person.class);
    }
    mongoOperations.createCollection(Person.class);
    Person p = new Person("John", 39);
    Account a = new Account("1234-59873-893-1", Account.Type.SAVINGS, 123.45D);
    p.getAccounts().add(a);
    mongoOperations.insert(p);
    List<Person> results = mongoOperations.findAll(Person.class);
    System.out.println("Results: " + results);
}
Also used : Account(org.springframework.data.mongodb.examples.hello.domain.Account) Person(org.springframework.data.mongodb.examples.hello.domain.Person)

Example 2 with Account

use of org.springframework.data.mongodb.examples.hello.domain.Account in project spring-data-document-examples by spring-projects.

the class SimpleMongoTest method updatingDocuments.

@Test
public void updatingDocuments() {
    String collectionName = operations.getCollectionName(Person.class);
    Person p1 = new Person("Bob", 33);
    p1.addAccount(new Account("198-998-2188", Account.Type.SAVINGS, 123.55d));
    operations.insert(p1);
    Person p2 = new Person("Mary", 25);
    p2.addAccount(new Account("860-98107-681", Account.Type.CHECKING, 400.51d));
    operations.insert(p2);
    Person p3 = new Person("Chris", 68);
    p3.addAccount(new Account("761-002-8901", Account.Type.SAVINGS, 10531.00d));
    operations.insert(p3);
    Person p4 = new Person("Janet", 33);
    p4.addAccount(new Account("719-100-0019", Account.Type.SAVINGS, 1209.10d));
    operations.insert(p4);
    assertEquals(4, operations.getCollection(collectionName).count());
    WriteResult wr = operations.updateMulti(query(where("accounts.accountType").is(Account.Type.SAVINGS.name())), new Update().inc("accounts.$.balance", 50.00), Person.class);
    assertNotNull(wr);
    assertEquals(3, wr.getN());
}
Also used : Account(org.springframework.data.mongodb.examples.hello.domain.Account) WriteResult(com.mongodb.WriteResult) Update(org.springframework.data.mongodb.core.query.Update) Person(org.springframework.data.mongodb.examples.hello.domain.Person) Test(org.junit.Test)

Example 3 with Account

use of org.springframework.data.mongodb.examples.hello.domain.Account in project spring-data-document-examples by spring-projects.

the class SimpleMongoTest method queryingForDocuments.

@Test
public void queryingForDocuments() {
    String collectionName = MongoCollectionUtils.getPreferredCollectionName(Person.class);
    Person p1 = new Person("Bob", 33);
    p1.addAccount(new Account("198-998-2188", Account.Type.SAVINGS, 123.55d));
    operations.insert(p1);
    Person p2 = new Person("Mary", 25);
    p2.addAccount(new Account("860-98107-681", Account.Type.CHECKING, 400.51d));
    operations.insert(p2);
    Person p3 = new Person("Chris", 68);
    p3.addAccount(new Account("761-002-8901", Account.Type.SAVINGS, 10531.00d));
    operations.insert(p3);
    Person p4 = new Person("Janet", 33);
    p4.addAccount(new Account("719-100-0019", Account.Type.SAVINGS, 1209.10d));
    operations.insert(p4);
    assertEquals(4, operations.getCollection(collectionName).count());
    List<Person> result = operations.find(new Query(where("age").lt(50).and("accounts.balance").gt(1000.00d)), Person.class);
    System.out.println(result);
    assertNotNull(result);
    assertEquals(1, result.size());
}
Also used : Account(org.springframework.data.mongodb.examples.hello.domain.Account) Query(org.springframework.data.mongodb.core.query.Query) Person(org.springframework.data.mongodb.examples.hello.domain.Person) Test(org.junit.Test)

Aggregations

Account (org.springframework.data.mongodb.examples.hello.domain.Account)3 Person (org.springframework.data.mongodb.examples.hello.domain.Person)3 Test (org.junit.Test)2 WriteResult (com.mongodb.WriteResult)1 Query (org.springframework.data.mongodb.core.query.Query)1 Update (org.springframework.data.mongodb.core.query.Update)1