mongoDB Java SDK CRUD操作例子

Example:
 
package com.sdfwerwer.test;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
 * mongoDB CURD test
 *
 * author:svennee
 */
public class testMongoDB {
        public static void main(String[] args) {
                try {
                        /**
                         * Connection MongoDB
                         */
                        /*
                         * MongoObject have a constructor(String, int)
                         *
                         * String = host:127.0.0.1(default value) or 192.168.1.155
                         * int = port:27017(default value)
                         */
                        Mongo mongo = new Mongo("192.168.1.161", 27017);
                        DB db = mongo.getDB("n_db_test");
                        DBCollection city = db.getCollection("city");

                        /**
                         * Insert data
                         */
                        BasicDBObject needAddObject = new BasicDBObject();
                        needAddObject.put("cid", "c001");
                        needAddObject.put("city_name", "武汉");
                        needAddObject.put("country", "湖北");
                        city.insert(needAddObject);
                        System.out.println("Insert data success~");

                        /**
                         * Read data
                         */
                        BasicDBObject queryCondition = new BasicDBObject();
                        queryCondition.put("city_name", "武汉");
                        DBCursor queryCursor = city.find(queryCondition);
                        DBCursor queryAllCursor = city.find();
                        while (queryCursor.hasNext()) {
                                System.out.println("Read condition data:"+queryCursor.hashCode() + " ReadValue:" + queryCursor.next());
                        }
                        while (queryAllCursor.hasNext()) {
                                System.out.println("Read All data:"+queryAllCursor.hashCode() + " ReadValue:" + queryAllCursor.next());
                        }
                        System.out.println("Read data success~");
                        
                        /**
                         * Modify data
                         */
                        // One method(这种强大些!)
                        BasicDBObject needModifyData = new BasicDBObject();
                        needModifyData.put("cid", "c001");
                        DBCursor modifyCursor = city.find(needModifyData);
                        if (modifyCursor.hasNext()) {
                                DBObject modifyObject = modifyCursor.next();
                                modifyObject.put("cid", "c002");
                                city.save(modifyObject);
                        }
                        
                        // Two method
                        BasicDBObject old_val = new BasicDBObject(),new_val = new BasicDBObject();
                        old_val.put("city_name", "武汉");
                        new_val.put("city_name", "武汉苏州");
                        city.update(old_val, new_val);
                        System.out.println("Modfiy data success~");

                        /**
                         * Delete data
                         */
                        BasicDBObject needDelData = new BasicDBObject();
                        needDelData.put("city_name", "武汉苏州");
                        DBCursor delCursor = city.find(needDelData);
                        while (delCursor.hasNext()) {
                                DBObject delObject = delCursor.next();
                                city.remove(delObject);
                        }
                        System.out.println("Delete data success~");
                        
                } catch (UnknownHostException e) {
                        System.err.println("UnknownHostException");
                        e.printStackTrace();
                } catch (MongoException e) {
                        System.err.println("MongoException");
                        e.printStackTrace();
                }

        }

}
更多相关文章
  • package mongodb; import java.net.UnknownHostException;import java.util.List; import com.mongodb.BasicDBObjec
  • Java EE 學習第44天Hibernate學習第一天Hibernate對單表的CRUD操作
    一.Hibernate简介 1.hibernate是对jdbc的二次开发 2.jdbc没有缓存机制,但是hibernate有. 3.hibernate的有点和缺点 (1)优点:有缓存,而且是二级缓存:完全的面向对象编程,代码操作比较简单. (2)缺点:大量的数据条件下不适合使用hibernate:程 ...
  • JAVA解析XML文件實現CRUD操作
    一.简介. 1.xml解析技术有两种:dom和sax 2.dom:Document Object Model,即文档对象模型,是W3C组织推荐的解析XML的一种方式. sax:Simple API for XML,不是官方标准,单它是xml社区事实上的标准. 3.XML解析器:Crimson(sun
  • Mongodb Manual閱讀筆記:CH2 Mongodb CRUD 操作
    2 Mongodb CRUD 操作 Mongodb Manual阅读笔记:CH2 Mongodb CRUD 操作Mongodb Manual阅读笔记:CH3 数据模型(Data Models)Mongodb Manual阅读笔记:CH4 管理Mongodb Manual阅读笔记:CH5 安全性Mon
  • JAVA使用XPath、DOM4J解析XML文件,實現對XML文件的CRUD操作
    一.简介 1.使用XPath可以快速精确定位指定的节点,以实现对XML文件的CRUD操作.
  • JAVA與DOM4J實現對XML文檔的CRUD操作
    一.简介 1.网上下载DOM4J 1.6.1压缩包,解压开之后,发现几个目录和一个jar文件,jar文件是必须的文件其它目录: docs目录:幫助文档的目录,单击index.html: Quick start是快速入门超链接,主要参考这里的代码完成对dom4j的认识,并完成CRUD的操作. Javd ...
  •        随着shh2各种操作方便框架.越来越多JAVA WEB效率,可是,假设在不了解这些框架使用的场合的情况下,一拿到项目就盲目地选择这些框架进行系统架构的搭建,就有可能造成非常多不是必需的资源浪费.       在项目开发中.对数据库的CRUD操作我们一般都是无法避免的操作,尽管hiber ...
  • MongoDB为Java提供了非常丰富的API操作,相比关系型数据库,这种NoSQL本身的数据也有点面向对象的意思,所以对于Java来说,Mongo的数据结构更加友好. MongoDB在今年做了一次重大升级,版本来到了3.0.相比之前的版本,这个版本中又很大的变化,相应地,本文中的方法可能在旧的版本 ...
一周排行