返回顶部

收藏

Java 往 MongoDB 插入文档的几种方法

更多

MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似 json的bjson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几 乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。

方法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

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)

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)

String json = "{'database' : 'mkyongDB','table' : 'hosting'," +
    "'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}";

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

collection.insert(dbObject);

完整示例 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<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());

            // 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();
        }

    }
}

标签:NoSQL,MongoDB

收藏

0人收藏

支持

0

反对

0

发表评论