Java 对芒果数据库 MongoDB的增删改查入门例子

昨天看了 芒果数据库 开胃菜,如果是使用过传统关系数据,而且对javascript json 比较熟悉的人,理解起来非常容易。所以单纯MongoDB的入门来说,还是非常简单的,今天继续测试用java 实现增删改查,java下的驱动包已经非常成熟,而且已经在大量的生产环境中得到检验。

首先下载 java mongodb 驱动包
下载的地址: github: 这里下载: MongoDB Java Driver jar file download

创建 java 工程进行测试
程序代码 程序代码

package com.yihaomen.mongodb.crud;

import java.net.UnknownHostException;
import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;

public class App {
    public static void main(String[] args) {

        try {
            /**** Connect to MongoDB ****/
            // Since 2.10.0, uses MongoClient
            MongoClient mongo = new MongoClient("localhost", 27017);

            /**** Get database ****/
            // if database doesn't exists, MongoDB will create it for you
            DB db = mongo.getDB("yihaomen");

            /**** Get collection / table from 'yihaomen' ****/
            // if collection doesn't exists, MongoDB will create it for you
            DBCollection table = db.getCollection("crudCollection");

            /**** Insert ****/
            // create a document to store key and value
            BasicDBObject document = new BasicDBObject();
            document.put("name", "yihaomen");
            document.put("age", 30);
            document.put("createdDate", new Date());
            table.insert(document);

            /**** Find and display ****/
            BasicDBObject searchQuery = new BasicDBObject();
            searchQuery.put("name", "yihaomen");

            DBCursor cursor = table.find(searchQuery);

            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }

            /**** Update ****/
            // search document where name="yihaomen" and update it with new values
            BasicDBObject query = new BasicDBObject();
            query.put("name", "yihaomen");

            BasicDBObject newDocument = new BasicDBObject();
            newDocument.put("name", "yihaomen-updated");

            BasicDBObject updateObj = new BasicDBObject();
            updateObj.put("$set", newDocument);

            table.update(query, updateObj);

            /**** Find and display ****/
            BasicDBObject searchQuery2
                = new BasicDBObject().append("name", "yihaomen-updated");

            DBCursor cursor2 = table.find(searchQuery2);

            while (cursor2.hasNext()) {
                System.out.println(cursor2.next());
            }
            
            /**** Remove test ****/
            //BasicDBObject delDocument = new BasicDBObject();
            //newDocument.put("name", "yihaomen-updated");
            //table.remove(delDocument);

            /**** Done ****/
            System.out.println("Done");

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (MongoException e) {
            e.printStackTrace();
        }

    }
}



这个例子实现了简单的增删改查。在代码中,将删除的代码注释掉了,是为了在MongoDB的 shell 中检查是否正常。

先启动 mongodb 服务
程序代码 程序代码

mongod --config D:\MongoDB2.6\mongo.config

然后,在你另一个控制台中,开启 MongoDB shell  进行测试,检查数据是否插入而且是更新了的数据:


看到所插入的数据了吧,说明能正常工作了,当然这只是个很小的测试程序。后面有时间详细测试 MongoDB.
测试代码下载:
下载文件 java MongoDB crud sample


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