2 回答
TA贡献2041条经验 获得超4个赞
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似 json的bjson格式,因此可以存储比较复杂的数据类型。
Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几 乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。
1 2 3 4 5 6 7 8 9 10 11 12 | 1. [代码]方法1 ? 123456789101112 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); 2. [代码]方法2 ? 123456789101112 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()); 3. [代码]方法3(使用Map) ? 123456789101112 Map<String, Object> documentMap = new HashMap<String, Object>();documentMap.put("database", "mkyongDB");documentMap.put("table", "hosting"); Map<String, Object> documentMapDetail = new HashMap<String, Object>();documentMapDetail.put("records", "99");documentMapDetail.put("index", "vps_index1");documentMapDetail.put("active", "true"); documentMap.put("detail", documentMapDetail); collection.insert(new BasicDBObject(documentMap)); 4. [代码]方法4(使用JSON) ? 123456 String json = "{'database' : 'mkyongDB','table' : 'hosting'," + "'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}"; DBObject dbObject = (DBObject)JSON.parse(json); collection.insert(dbObject); |
TA贡献1886条经验 获得超2个赞
1 2 3 4 5 6 7 8 9 10 11 12 | 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); |
- 2 回答
- 0 关注
- 1311 浏览
添加回答
举报