java操作mongodb的几种方法
2014-02-13 10:45
435 查看
第一种方法,是使用BasicDBObject,方法如下代码所示:
BasicDBObject document = new BasicDBObject();
document.put("database", "mkyongDB");
document.put("table", "hosting");
BasicDBObject documentDetail = new BasicDBObject();
documentDetail.put("records", "99");
documentDetail.put("index", "vps_index1");
documentDetail.put("active", "true");
document.put("detail", documentDetail);
collection.insert(document);
第二种方法是使用BasicDBObjectBuilder对象,如下代码所示:
BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()
.add("database", "mkyongDB")
.add("table", "hosting");
BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start()
.add("records", "99")
.add("index", "vps_index1")
.add("active", "true");
documentBuilder.add("detail", documentBuilderDetail.get());
collection.insert(documentBuilder.get());
第三种方法是使用Map对象,代码如下:
Map documentMap =new HashMap();
documentMap.put("database", "mkyongDB");
documentMap.put("table", "hosting");
Map documentMapDetail =new HashMap();
documentMapDetail.put("records", "99");
documentMapDetail.put("index", "vps_index1");
documentMapDetail.put("active", "true");
documentMap.put("detail", documentMapDetail);
collection.insert(new BasicDBObject(documentMap));
第四种方法,也就是最简单的,即直接插入JSON格式数据
String json ="{'database' : 'mkyongDB','table' : 'hosting',"+
"'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}";
DBObject dbObject =(DBObject)JSON.parse(json);
collection.insert(dbObject);
这里使用了JSON的parse方法,将解析后的JSON字符串转变为DBObject对象后再直接插入到collection中去。
BasicDBObject document = new BasicDBObject();
document.put("database", "mkyongDB");
document.put("table", "hosting");
BasicDBObject documentDetail = new BasicDBObject();
documentDetail.put("records", "99");
documentDetail.put("index", "vps_index1");
documentDetail.put("active", "true");
document.put("detail", documentDetail);
collection.insert(document);
第二种方法是使用BasicDBObjectBuilder对象,如下代码所示:
BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()
.add("database", "mkyongDB")
.add("table", "hosting");
BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start()
.add("records", "99")
.add("index", "vps_index1")
.add("active", "true");
documentBuilder.add("detail", documentBuilderDetail.get());
collection.insert(documentBuilder.get());
第三种方法是使用Map对象,代码如下:
Map documentMap =new HashMap();
documentMap.put("database", "mkyongDB");
documentMap.put("table", "hosting");
Map documentMapDetail =new HashMap();
documentMapDetail.put("records", "99");
documentMapDetail.put("index", "vps_index1");
documentMapDetail.put("active", "true");
documentMap.put("detail", documentMapDetail);
collection.insert(new BasicDBObject(documentMap));
第四种方法,也就是最简单的,即直接插入JSON格式数据
String json ="{'database' : 'mkyongDB','table' : 'hosting',"+
"'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}";
DBObject dbObject =(DBObject)JSON.parse(json);
collection.insert(dbObject);
这里使用了JSON的parse方法,将解析后的JSON字符串转变为DBObject对象后再直接插入到collection中去。
相关文章推荐
- mongodb安装过程与常用命令
- MongoDB面试题__加强
- mongodb简介、安装、启停
- Mongodb java代码
- mongodb数据迁移命令
- MongoDB权限管理之用户名和密码的操作
- MongoDB 入门与实例
- Mongodb在Windows下安装及配置
- Codeigniter MongoDB类库
- MySQL和MongoDB语句的写法对照
- Mongodb在Windows下安装及配置
- 十分钟了解MongoDB
- 一段封装mongodb连接的代码
- mongodb笔记
- Cassandra HBase和MongoDb性能比较
- mongodb gridfs分析
- Windows下MongoDB安装
- Quora上的一个哥们对mongodb由爱转恨
- MongoDB设置访问权限、设置用户
- 基于jsp+javabean+servlet+mongodb 增删改查