public void
testFindAll() throws UnknownHostException, MongoException {
// TODO
Auto-generated method stub
ArrayList<ServerAddress>
addrs = new ArrayList<ServerAddress>();
addrs.add(new
ServerAddress("192.168.1.101", 27017));
addrs.add(new
ServerAddress("192.168.1.102", 27017));
addrs.add(new
ServerAddress("192.168.1.103", 27017));
Mongo connection = new
Mongo(addrs);
DB
mongoJUnit = connection.getDB("deptDB");
DBCollection
dept = mongoJUnit.getCollection("deptJUnit");
DBCursor
cur = dept.find();
int index =
1;
for (
Iterator<DBObject> it = cur.iterator(); it.hasNext(); ++index) {
DBObject
obj = it.next();
System.out.println("data("+ index+"):"+obj.toString());
}
System.out.println(connection.getAddress());
}
'Brain Trainning > NoSQL' 카테고리의 다른 글
[NOSQL] 101 Questions To Ask When Considering A NoSQL Database (펌글) (0) | 2011.06.17 |
---|---|
[MongoDB] Auto Increment with MongoDB (0) | 2011.04.04 |
[MongoDB] 트위터 Collections... (0) | 2011.03.31 |
[MongoDB] Config 파일 옵션 (0) | 2011.03.09 |
[MongoDB] Java Code Sample (0) | 2011.02.28 |
The last two you are mentioning (GuicyData and Sculptor) are covered here:http://nosql.mypopescu.com/post/816470307/tutorial-mongodb-in-java
You are using the low-level mapping capabilities of morphia instead of the much nicer datastore, or DAO interfaces. There should be no need to use the driver DBCollection object. You can remove most of your code by using the higher-level abstractions. Also, all the annotations (except @Id) are optional in your example.
@Before
public void setUp() throws UnknownHostException {
ds = new Morphia().createDatastore("test");
}
public void storePersonThroughMorphiaMapping () {
Person test = new Person(new Address("Josefines gate", "0401", "Oslo", "Norge") );
test.setName("Knut Haugen");
ds.save(test);
Person test2 = ds.get(Person.class, test.getId());
assertNotNull(test2.getId());
}