mongodb java sample_Java MongoDB : Insert Document(s) in Collection Examples

List of examples in this tutorial

1) Insert BasicDBObject in collection

2) Use DBObjectBuilder to insert document in collection

3) Use java.util.HashMap to build BasicDBObject and insert into collection

4) Parse JSON to build DBObject and insert into collection

Sample document which we will insert into collection{

"name":"lokesh",

"website":"howtodoinjava.com",

"address":{

"addressLine1":"Some address",

"addressLine2":"Karol Bagh",

"addressLine3":"New Delhi, India"

}

}

1) Insert BasicDBObject in collection

这是最简单的。 创建BasicDBObject的实例,填充数据并调用collection.insert()方法。

BasicDBObject document = new BasicDBObject();

document.put("name", "lokesh");

document.put("website", "howtodoinjava.com");

BasicDBObject documentDetail = new BasicDBObject();

documentDetail.put("addressLine1", "Sweet Home");

documentDetail.put("addressLine2", "Karol Bagh");

documentDetail.put("addressLine3", "New Delhi, India");

document.put("address", documentDetail);

collection.insert(document);

Output:

{ "_id" : { "$oid" : "538d56a3364192189d4f98fe"} , "name" : "lokesh" , "website" : "howtodoinjava.com" ,

"address" : { "addressLine1" : "Sweet Home" , "addressLine2" : "Karol Bagh" , "addressLine3" : "New Delhi, India"}}

2) Use DBObjectBuilder to insert document in collection

与上面的示例非常相似,仅使用DBObjectBuilder来构建DBObject。BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()

.add("name", "lokesh")

.add("website", "howtodoinjava.com");

BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start()

.add("addressLine1", "Some address")

.add("addressLine2", "Karol Bagh")

.add("addressLine3", "New Delhi, India");

documentBuilder.add("address", documentBuilderDetail.get());

collection.insert(documentBuilder.get());

Output:

{ "_id" : { "$oid" : "538d56a3364192189d4f98ff"} , "name" : "lokesh" , "website" : "howtodoinjava.com" ,

"address" : { "addressLine1" : "Sweet Home" , "addressLine2" : "Karol Bagh" , "addressLine3" : "New Delhi, India"}}

3) Use java.util.HashMap to build BasicDBObject and insert into collection

在这里,您首先将数据放入哈希图中,然后使用该哈希图构建BasicDBObject。Map documentMap = new HashMap();

documentMap.put("name", "lokesh");

documentMap.put("website", "howtodoinjava.com");

Map documentMapDetail = new HashMap();

documentMapDetail.put("addressLine1", "Some address");

documentMapDetail.put("addressLine2", "Karol Bagh");

documentMapDetail.put("addressLine3", "New Delhi, India");

documentMap.put("address", documentMapDetail);

collection.insert(new BasicDBObject(documentMap));

Output:

{ "_id" : { "$oid" : "538d56a3364192189d4f98fg"} , "name" : "lokesh" , "website" : "howtodoinjava.com" ,

"address" : { "addressLine1" : "Sweet Home" , "addressLine2" : "Karol Bagh" , "addressLine3" : "New Delhi, India"}}

4) Parse JSON to build DBObject and insert into collectionString json = "{ 'name' : 'lokesh' , " +

"'website' : 'howtodoinjava.com' , " +

"'address' : { 'addressLine1' : 'Some address' , " +

"'addressLine2' : 'Karol Bagh' , " +

"'addressLine3' : 'New Delhi, India'}" +

"}";

DBObject dbObject = (DBObject)JSON.parse(json);

collection.insert(dbObject);

Output:

{ "_id" : { "$oid" : "538d56a3364192189d4f98fg"} , "name" : "lokesh" , "website" : "howtodoinjava.com" ,

"address" : { "addressLine1" : "Sweet Home" , "addressLine2" : "Karol Bagh" , "addressLine3" : "New Delhi, India"}}

Complete Example and Sourcecode

以上所有示例的完整工作代码如下:package examples.mongodb.crud;

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

import com.mongodb.WriteResult;

import com.mongodb.util.JSON;

public class MongoDBInsertDataExample

{

public static void main(String[] args) throws UnknownHostException

{

MongoClient mongo = new MongoClient("localhost", 27017);

DB db = mongo.getDB("howtodoinjava");

DBCollection collection = db.getCollection("users");

///Delete All documents before running example again

WriteResult result = collection.remove(new BasicDBObject());

System.out.println(result.toString());

basicDBObject_Example(collection);

basicDBObjectBuilder_Example(collection);

hashMap_Example(collection);

parseJSON_Example(collection);

DBCursor cursor = collection.find();

while(cursor.hasNext()) {

System.out.println(cursor.next());

}

}

private static void basicDBObject_Example(DBCollection collection){

BasicDBObject document = new BasicDBObject();

document.put("name", "lokesh");

document.put("website", "howtodoinjava.com");

BasicDBObject documentDetail = new BasicDBObject();

documentDetail.put("addressLine1", "Sweet Home");

documentDetail.put("addressLine2", "Karol Bagh");

documentDetail.put("addressLine3", "New Delhi, India");

document.put("address", documentDetail);

collection.insert(document);

}

private static void basicDBObjectBuilder_Example(DBCollection collection){

BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()

.add("name", "lokesh")

.add("website", "howtodoinjava.com");

BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start()

.add("addressLine1", "Some address")

.add("addressLine2", "Karol Bagh")

.add("addressLine3", "New Delhi, India");

documentBuilder.add("address", documentBuilderDetail.get());

collection.insert(documentBuilder.get());

}

private static void hashMap_Example(DBCollection collection){

Map documentMap = new HashMap();

documentMap.put("name", "lokesh");

documentMap.put("website", "howtodoinjava.com");

Map documentMapDetail = new HashMap();

documentMapDetail.put("addressLine1", "Some address");

documentMapDetail.put("addressLine2", "Karol Bagh");

documentMapDetail.put("addressLine3", "New Delhi, India");

documentMap.put("address", documentMapDetail);

collection.insert(new BasicDBObject(documentMap));

}

private static void parseJSON_Example(DBCollection collection){

String json = "{ 'name' : 'lokesh' , " +

"'website' : 'howtodoinjava.com' , " +

"'address' : { 'addressLine1' : 'Some address' , " +

"'addressLine2' : 'Karol Bagh' , " +

"'addressLine3' : 'New Delhi, India'}" +

"}";

DBObject dbObject = (DBObject)JSON.parse(json);

collection.insert(dbObject);

}

}

Output:

{ "serverUsed" : "localhost/127.0.0.1:27017" , "connectionId" : 3 , "n" : 4 , "syncMillis" : 0 , "writtenTo" : null , "err" : null , "ok" : 1.0}

{ "_id" : { "$oid" : "538d5b3936417871aa391d20"} , "name" : "lokesh" , "website" : "howtodoinjava.com" , "address" : { "addressLine1" : "Sweet Home" , "addressLine2" : "Karol Bagh" , "addressLine3" : "New Delhi, India"}}

{ "_id" : { "$oid" : "538d5b3936417871aa391d21"} , "name" : "lokesh" , "website" : "howtodoinjava.com" , "address" : { "addressLine1" : "Some address" , "addressLine2" : "Karol Bagh" , "addressLine3" : "New Delhi, India"}}

{ "_id" : { "$oid" : "538d5b3936417871aa391d22"} , "address" : { "addressLine3" : "New Delhi, India" , "addressLine2" : "Karol Bagh" , "addressLine1" : "Some address"} , "website" : "howtodoinjava.com" , "name" : "lokesh"}

{ "_id" : { "$oid" : "538d5b3936417871aa391d23"} , "name" : "lokesh" , "website" : "howtodoinjava.com" , "address" : { "addressLine1" : "Some address" , "addressLine2" : "Karol Bagh" , "addressLine3" : "New Delhi, India"}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值