mongo insert java_Java 往 MongoDB 插入文档的几种方法

展开全部

代码62616964757a686964616fe4b893e5b19e31333332636334片段(5)[全屏查看所有代码]1. [代码]方法1 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 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) 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));4. [代码]方法4(使用JSON) String json = "{'database' : 'mkyongDB','table' : 'hosting'," + "'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}"; DBObject dbObject = (DBObject)JSON.parse(json); collection.insert(dbObject);5. [代码]完整示例 InsertDocumentApp.java package com.mkyong.core; import java.net.UnknownHostException; import java.util.HashMap; import java.util.Map; import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObjectBuilder; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.Mongo; import com.mongodb.MongoException; import com.mongodb.util.JSON; /** * Java MongoDB : Insert a Document * */ public class InsertDocumentApp { public static void main(String[] args) { try { Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("yourdb"); // get a single collection DBCollection collection = db.getCollection("dummyColl"); // BasicDBObject example System.out.println("BasicDBObject example..."); 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); DBCursor cursorDoc = collection.find(); while (cursorDoc.hasNext()) { System.out.println(cursorDoc.next()); } collection.remove(new BasicDBObject()); // BasicDBObjectBuilder example System.out.println("BasicDBObjectBuilder example..."); 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()); DBCursor cursorDocBuilder = collection.find(); while (cursorDocBuilder.hasNext()) { System.out.println(cursorDocBuilder.next()); } collection.remove(new BasicDBObject()); // Map example System.out.println("Map example..."); 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)); DBCursor cursorDocMap = collection.find(); while (cursorDocMap.hasNext()) { System.out.println(cursorDocMap.next()); } collection.remove(new BasicDBObject()); // JSON parse example System.out.println("JSON parse example..."); String json = "{'database' : 'mkyongDB','table' : 'hosting'," + "'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}"; DBObject dbObject = (DBObject)JSON.parse(json); collection.insert(dbObject); DBCursor cursorDocJSON = collection.find(); while (cursorDocJSON.hasNext()) { System.out.println(cursorDocJSON.next()); } collection.remove(new BasicDBObject()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } } }

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值