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.相比之前的版本,这个版本中又很大的变化,相应地,本文中的方法可能在旧的版本 ...
一周排行
  • 51單片機 SPI nRF24L01 無線簡單程序 1
      main.c 1 #include <reg51.h> 2 #includ ...
  • ebtables是以太网桥防火墙,以太网工作在数据链路层,ebtables过滤数据链路层包.2.6内核内置了ebtables,要使用它必须先按装她的用户空间工具(ebtables-V2.0.6),安装完成后可以使用e
  • 用BFS和DFS解決圓盤狀態搜索問題
    人工智能课程的实验(我的解法其实更像是算法课程的实验) 用到的算法:深度优先搜索.宽度优先
  • 公司服务器全部在ldap环境下. 上次ldap服务异常终止后,然后centos服务器ls.top什么的都很慢(感觉要卡一下). 通过 [[email protected] ~]# ss | grep CLOSE | wc -l 20
  • hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设置JVM重用. ...
  • 1. 后台注册控件,页面form提交,后台获取控件方法  Request.Form["name"]:
  • 稳定排序 Time Limit: 3/1 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4042 
  • MUA
    a big dealanalysisanalyticscooperate 合作efficientexplicitlyfine grainedGranularitygraphgeographichive  [haɪv]
  • Android(java)學習筆記83:finally關鍵字的作用
    package cn.itcast_07; import java.text.ParseE
  • HTML使用mailto在網頁中鏈接Email地址
    <a>标签还有一个作用是可以链接Email地址,使用mailto能让访问者便捷 ...