需要保存个人信息树到MongoDb里面,写了段测试代码,比较简单。主要用来测试节点的保存情况
使用BaseObject 对象声明一个节点。然后把子节点都作为BaseObject的一个熟悉。当子节点仍然有子节点的时候,可以在嵌套一层。
当一个节点有多个子文档返回的时候,使用DBObjectList对象来返回保存。
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Mongo mongo = null;
BasicDBObject documentRoot = new BasicDBObject();
BasicDBObject c = new BasicDBObject();
try {
mongo = new Mongo("localhost", 27017);
} catch (UnknownHostException e) {
e.printStackTrace();
}
DB db = mongo.getDB("his");
DBCollection testdb = db.getCollection("test");
documentRoot.put("aa", "aa");
documentRoot.put("bb", "aa");
documentRoot.put("cc", "aa");
List test = new ArrayList();
test.add("aa");
test.add("aa");
Map<String, Map> aa = new HashMap<String, Map>();
Map<String, String> bb = new HashMap<String, String>();
Map<String, String> cc = new HashMap<String, String>();
Map<String, String> dd = new HashMap<String, String>();
bb.put("name", "chenhua");
bb.put("age", "13");
bb.put("sex", "f");
cc.put("fuck", "fuck");
dd.put("1 record", "name:chenhua;age:13;sex:f");
dd.put("2 record", "name:chenhua;age:13;sex:f");
aa.put("11", bb);
aa.put("12", cc);
aa.put("13", dd);
// c.put("c",aa);
documentRoot.put("aaaaaaa", aa);
testdb.insert(documentRoot);
}
}