Java MongoDB : Insert a document(译)

?

原文出自:http://www.mkyong.com/mongodb/java-mongodb-insert-a-document/

返回目录:http://ysj5125094.iteye.com/blog/2192754??

?

Java MongoDB : Insert a document

In this tutorial, we show you 4 ways to insert below?JSON?data into a “document“, via Java MongoDB API.

译:在本教程中我们给你4种方式将下面的JSON数据插入到一个“document”中通过Java?API的MongoDB实现

?

Test Data

Test data in JSON format.

{
	"database" : "mkyongDB",
	"table" : "hosting",
	"detail" : 
		{
			records : 99,
			index : "vps_index1",
			active : "true"
		}
	}
}

?

1. 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);

?

2. 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());

?

3. Map example

	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. 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);

?

Full example

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");
 
	DBCollection collection = db.getCollection("dummyColl");
 
	// 1. 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());
 
	// 2. 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());
 
	// 3. Map example
	System.out.println("Map example...");
	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));
 
	DBCursor cursorDocMap = collection.find();
	while (cursorDocMap.hasNext()) {
		System.out.println(cursorDocMap.next());
	}
 
	collection.remove(new BasicDBObject());
 
	// 4. 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();
    }
 
  }
}

?

Output…

BasicDBObject example...
{ "_id" : { "$oid" : "4dc9ef6f237f86642d5b34bd"} , "database" : "mkyongDB" , 
"table" : "hosting" , "detail" : { "records" : "99" , "index" : "vps_index1" , "active" : "true"}}
 
BasicDBObjectBuilder example...
{ "_id" : { "$oid" : "4dc9ef6f237f86642d5b34be"} , "database" : "mkyongDB" , 
"table" : "hosting" , "detail" : { "records" : "99" , "index" : "vps_index1" , "active" : "true"}}
 
Map example...
{ "_id" : { "$oid" : "4dc9ef6f237f86642d5b34bf"} , "detail" : { "index" : "vps_index1" , 
"active" : "true" , "records" : "99"} , "table" : "hosting" , "database" : "mkyongDB"}
 
JSON parse example...
{ "_id" : { "$oid" : "4dc9ef6f237f86642d5b34c0"} , "database" : "mkyongDB" , 
"table" : "hosting" , "detail" : { "records" : 199 , "index" : "vps_index1" , "active" : "true"}}

?

说明:?

What is “_id” ?
The?_id?is added by MongoDB automatically, for identity purpose. From MongoDB document, it said, all element names that start with “_”, “/” and “$” are reserved for internal use.

译:_id 是MongoDB自动添加的,起到身份的目的(相关于唯一主键)。MongoDB文档中说,所有的元素名称以"_", "/" 和 "$" 开头的为保留关键字。

?

References

  1. BasicDBObject Java Doc
  2. BasicDBObjectBuilder Java Doc

?

?

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。