java MongoDB 增加数据的四种方式

前面实现了java 连接 MongoDB 数据库实现简单的增删改查的例子。作为入门确实足够了,但事实上还有很多其他的方法来做这些操作,比如增加数据,至少就有四种方法:
1. 利用 BasicDBObject 增加文档
2. 利用 BasicDBObjectBuilder 增加文档
3. 利用Map(HashMap)  增加文档
4. 利用json 字符串方式增加文档

假设要增加的文档模型如下:
程序代码 程序代码

database" : "yihaomen",
    "table" : "hosting",
    "detail" :
        {
            records : 99,
            index : "aliyun",
            active : "true"
        }
    }

下面的java代码,详细介绍了如何利用这四种方式来增加数据:
程序代码 程序代码

package com.yihaomen.mongodb.insert;

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.DBObject;
import com.mongodb.Mongo;
import com.mongodb.util.JSON;

public class App {
    
    public static void main(String[] args) throws UnknownHostException {
        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("yihaomen");    
        DBCollection collection = db.getCollection("myinsert");
        //insertByBasicDBObject(collection);
        //insertByBasicDBObjectBuilder(collection);
        //insertByHashMap(collection);
        insertByJsonString(collection);
    }
    
    public static void insertByBasicDBObject(DBCollection collection){
        BasicDBObject document = new BasicDBObject();
        document.put("database", "yihaomen");
        document.put("table", "hosting");
        
        BasicDBObject documentDetail = new BasicDBObject();
        documentDetail.put("records", 99);
        documentDetail.put("index", "aliyun");
        documentDetail.put("active", "true");    
        document.put("detail", documentDetail);    
        
        collection.insert(document);
    }
    
    public static void insertByBasicDBObjectBuilder(DBCollection collection){
        BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()
            .add("database", "yihaomen")
            .add("table", "hosting");
        
        BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start()
            .add("records", 99)
            .add("index", "aliyun")
            .add("active", "true");
        
        documentBuilder.add("detail", documentBuilderDetail.get());        
        collection.insert(documentBuilder.get());
    }
    
    public static void insertByHashMap(DBCollection collection){
        Map<String, Object> documentMap = new HashMap<String, Object>();
        documentMap.put("database", "yihaomen");
        documentMap.put("table", "hosting");
    
        Map<String, Object> documentMapDetail = new HashMap<String, Object>();
        documentMapDetail.put("records", 99);
        documentMapDetail.put("index", "aliyun");
        documentMapDetail.put("active", "true");
    
        documentMap.put("detail", documentMapDetail);    
        collection.insert(new BasicDBObject(documentMap));
    }
    
    public static void insertByJsonString(DBCollection collection){
        String json = "{'database' : 'yihaomen','table' : 'hosting'," +
            "'detail' : {'records' : 99, 'index' : 'aliyun', 'active' : 'true'}}}";            
        DBObject dbObject = (DBObject)JSON.parse(json);            
        collection.insert(dbObject);
    }
}



测试,启动 mongod 进程,运行测试java 程序,插入数据。再启动mongo 进程,查看插入的数据如下:



除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: mongodb
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.