PageRenderTime 462ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 0ms

/test/net/ion/bleujin/mongo/TestDBObject.java

https://github.com/bleujin/mongoNode
Java | 153 lines | 110 code | 43 blank | 0 comment | 3 complexity | befa955fd66cdf564cd93dec039c06a3 MD5 | raw file
  1. package net.ion.bleujin.mongo;
  2. import java.util.List;
  3. import junit.framework.TestCase;
  4. import net.ion.framework.parse.gson.JsonObject;
  5. import net.ion.framework.parse.gson.JsonParser;
  6. import net.ion.framework.util.Debug;
  7. import net.ion.framework.util.ListUtil;
  8. import net.ion.framework.util.MapUtil;
  9. import net.ion.framework.util.RandomUtil;
  10. import com.mongodb.BasicDBList;
  11. import com.mongodb.BasicDBObject;
  12. import com.mongodb.DB;
  13. import com.mongodb.DBCollection;
  14. import com.mongodb.DBObject;
  15. import com.mongodb.Mongo;
  16. public class TestDBObject extends TestCase {
  17. private DBCollection col ;
  18. @Override
  19. protected void setUp() throws Exception {
  20. super.setUp();
  21. Mongo m = new Mongo("61.250.201.78");
  22. DB db = m.getDB("test");
  23. this.col = db.getCollection("mongonode");
  24. col.drop();
  25. }
  26. public void testDBObject() throws Exception {
  27. BasicDBObject wrapper = new BasicDBObject() ;
  28. List<BasicDBObject> list = ListUtil.newList() ;
  29. for (int i = 0; i < 5; i++) {
  30. list.add(makePerson(RandomUtil.nextRandomString(10), "seoul", 20)) ;
  31. }
  32. wrapper.append("people", list) ;
  33. col.save(wrapper);
  34. DBObject dbo = col.findOne();
  35. Debug.line(dbo);
  36. }
  37. public void testAppend() throws Exception {
  38. BasicDBObject wrapper = new BasicDBObject() ;
  39. for (int i = 0; i < 5; i++) {
  40. wrapper.append("people", makePerson(RandomUtil.nextRandomString(10), "seoul", 20)) ;
  41. }
  42. col.save(wrapper);
  43. DBObject dbo = col.findOne();
  44. Debug.line(dbo);
  45. }
  46. public void testArray() throws Exception {
  47. BasicDBObject wrapper = new BasicDBObject() ;
  48. BasicDBList list = new BasicDBList() ;
  49. for (int i = 0; i < 5; i++) {
  50. list.add(makePerson(RandomUtil.nextRandomString(10), "seoul", 20)) ;
  51. }
  52. wrapper.append("people", list) ;
  53. col.save(wrapper);
  54. DBObject dbo = col.findOne();
  55. Debug.line(dbo);
  56. }
  57. public void testJSON() throws Exception {
  58. BasicDBObject wrapper = new BasicDBObject() ;
  59. JsonObject json = JsonParser.fromString("{userid:'bleujin',num:23, address:{city:'seoul'}}").getAsJsonObject() ;
  60. wrapper.put("person", json) ;
  61. col.save(wrapper);
  62. DBObject dbo = col.findOne();
  63. Debug.line(dbo.get("person"), dbo.get("person.userId")) ;
  64. Debug.line(dbo.get("address"), dbo);
  65. }
  66. public void testSet() throws Exception {
  67. BasicDBObject dbo = new BasicDBObject() ;
  68. dbo.put("name", "bleujin") ;
  69. dbo.put("address", "seoul") ;
  70. col.save(dbo) ;
  71. Debug.debug(col.findOne()) ;
  72. BasicDBObject mod = new BasicDBObject() ;
  73. mod.put("$set", new BasicDBObject("name", "hero")) ;
  74. col.findAndModify(col.findOne(), mod) ;
  75. Debug.debug(col.findOne()) ;
  76. }
  77. public void testGroup() throws Exception {
  78. col.save(new ChainDBObject().put("grp", 1).put("name", "bleujin").put("rtime", 3).getDBObject() ) ;
  79. col.save(new ChainDBObject().put("grp", 1).put("name", "bleujin").put("rtime", 2).getDBObject() ) ;
  80. col.save(new ChainDBObject().put("grp", 1).put("name", "hero").put("rtime", 5).getDBObject() ) ;
  81. DBObject key = new BasicDBObject("name", true);
  82. DBObject cond = new BasicDBObject("grp", 1);
  83. DBObject initial = new BasicDBObject(MapUtil.chainMap().put("count", 0).put("total", 0).toMap()) ;
  84. String reduce = "function(doc, out){out.count++; out.total += doc.rtime; } ";
  85. String finalize = "function(out) {out.avg = out.total / out.count; }";
  86. DBObject gresult = col.group(key, cond, initial, reduce, finalize);
  87. Debug.line(gresult, gresult.getClass()) ;
  88. }
  89. public void testInGroup() throws Exception {
  90. ChainDBObject cdbo = new ChainDBObject().put("name", "bleujin").put("address", "seoul").inner("friend")
  91. .push(MapUtil.chainKeyMap().put("name", "novision").put("age", 20))
  92. .push(MapUtil.chainKeyMap().put("name", "iihi").put("age", 25))
  93. .push(MapUtil.chainKeyMap().put("name", "pm1200").put("age", 30)).getParent() ;
  94. col.save(cdbo.getDBObject()) ;
  95. String compareFn = "var comfn = function(f1, f2){ var order = 1 ; if (f1[out.sortkey] > f2[out.sortkey]) return 1 * order ; else if(f1[out.sortkey] < f2[out.sortkey]) return -1 * order; else return 0 }" ;
  96. DBObject key = new BasicDBObject("name", true);
  97. DBObject cond = new BasicDBObject("name", "bleujin");
  98. DBObject initial = new BasicDBObject(MapUtil.chainMap().put("sortkey", "name").put("skip", 0).put("limit", 2).put("friends", new BasicDBList()).toMap()) ;
  99. String reduce = "function(doc, out){ " + compareFn + "; out.friends = Array.prototype.slice.call(Array.prototype.sort.call(doc.friend, comfn), out.skip, out.limit); }";
  100. String finalize = "function(out) {;}";
  101. Debug.line(col.group(key, cond, initial, reduce, finalize)) ;
  102. }
  103. private BasicDBObject makePerson(String name, String address, int age) {
  104. BasicDBObject dbo = new BasicDBObject();
  105. dbo.append("name", name);
  106. dbo.append("address", address);
  107. dbo.append("age", age);
  108. return dbo;
  109. }
  110. }