use of org.springframework.data.mongodb.examples.hello.domain.Person in project spring-data-document-examples by spring-projects.
the class SimpleMongoTest method saveAndRetrieveDocuments.
@Test
public void saveAndRetrieveDocuments() {
String collectionName = operations.getCollectionName(Person.class);
Person p = new Person("Bob", 33);
operations.insert(p);
assertEquals(1, operations.getCollection(collectionName).count());
Person qp = operations.findOne(query(where("id").is(p.getId())), Person.class);
assertNotNull(qp);
assertEquals(p.getName(), qp.getName());
}
use of org.springframework.data.mongodb.examples.hello.domain.Person 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);
}
use of org.springframework.data.mongodb.examples.hello.domain.Person 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());
}
use of org.springframework.data.mongodb.examples.hello.domain.Person 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());
}
Aggregations