NoSql mongodb 다루기 3 우혁이 아빠 2011. 7. 12. 20:09 package org.springframework.data.mongodb.examples.mongo; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import java.util.Set; import java.util.regex.Pattern; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="/META-INF/spring/applicationContext.xml") public class MongodbBasicTest { @Autowired private MongoTemplate template; private String collectionName = "people"; @Test public void test() throws Exception { DBCollection collection = template.getCollection(collectionName); DBCursor cursor = collection.find(); while (cursor.hasNext()) { collection.remove(cursor.next()); } collection.ensureIndex(new BasicDBObject("id", 1).append("unique", true)); collection.createIndex(new BasicDBObject("name", 1)); collection.insert(makePersonDocument(6655, "James", "male")); collection.insert(makePersonDocument(6797, "Bond", "male")); collection.insert(makePersonDocument(6643, "Cheryl", "female")); collection.insert(makePersonDocument(7200, "Scarlett", "female")); collection.insert(makePersonDocument(6400, "Jacks", "male")); assertThat(collection.getCount(), is(5L)); cursor = collection.find(); printResults(cursor, "Find All Records"); cursor = collection.find(new BasicDBObject("id", 6655)); printResults(cursor, "Find id = 6655"); cursor = collection.find( new BasicDBObject("id", new BasicDBObject("$lte", 6700)) ); printResults(cursor, "Find id <= 6700"); cursor = collection.find( new BasicDBObject( "id", new BasicDBObject("$lte", 6700) ).append("gender", "male") ); printResults(cursor, "Find id <= 6700 and gender = male"); cursor = collection.find( new BasicDBObject( "name", Pattern.compile("^ja.*?s$", Pattern.CASE_INSENSITIVE)) ).sort(new BasicDBObject("name", -1) ); printResults(cursor, "Find name like Ja%s and sort reverse by name"); cursor = collection.find( new BasicDBObject("gender", "female") ).sort(new BasicDBObject("id", -1)).limit(2); printResults(cursor, "Get top 2 (by id) ladies"); cursor = collection.find(); while (cursor.hasNext()) { BasicDBObject set = new BasicDBObject("$inc", new BasicDBObject("id", -10)); if ("male".equals(cursor.next().get("gender"))) { set.append( "$set", new BasicDBObject( "name", "Sir ".concat((String) cursor.curr().get("name")) ) ); } else { set.append( "$set", new BasicDBObject( "name", "Mme ".concat((String) cursor.curr().get("name")) ) ); } collection.update(cursor.curr(), set); } cursor = collection.find(); printResults(cursor, "All, after id and name update"); } private void printResults(DBCursor cursor, String message) { System.out.println("<<<<<<<<<< " + message + " >>>>>>>>>>"); while (cursor.hasNext()) { cursor.next(); Set keys = cursor.curr().keySet(); int i = 0; for (String key : keys) { i++; System.out.print(key + " : " + cursor.curr().get(key)); if (keys.size() == i) { System.out.println(""); } else { System.out.print(" ,\t"); } } } } private DBObject makePersonDocument(int id, String name, String gender) { BasicDBObject doc = new BasicDBObject(); doc.put("id", id); doc.put("name", name); doc.put("gender", gender); return doc; } } 저작자표시