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

}

}

mongoDB Java SDK CRUD操作例子的更多相关文章

  1. mongodb学习(四)CRUD操作

    CRUD操作: 1. 插入操作: 直接使用 insert可执行单个操作,也可以执行批量操作 书上的batchInsert会报错.似乎被废弃了. db.foo.insert({"bar&quo ...

  2. 关于Mongdb的java的CRUD操作

    package mongodb; import java.net.UnknownHostException;import java.util.List; import com.mongodb.Basi ...

  3. MongoDB -- JAVA基本API操作

    package com.example.mongodb.mongodb.demo; import com.mongodb.MongoClient; import com.mongodb.client. ...

  4. 【翻译】MongoDB指南/CRUD操作(四)

    [原文地址]https://docs.mongodb.com/manual/ CRUD操作(四) 1 查询方案(Query Plans) MongoDB 查询优化程序处理查询并且针对给定可利用的索引选 ...

  5. 【翻译】MongoDB指南/CRUD操作(三)

    [原文地址]https://docs.mongodb.com/manual/ CRUD操作(三) 主要内容: 原子性和事务(Atomicity and Transactions),读隔离.一致性和新近 ...

  6. 【翻译】MongoDB指南/CRUD操作(二)

    [原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...

  7. 【翻译】MongoDB指南/CRUD操作(一)

    [原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(一) 主要内容:CRUD操作简介,插入文档,查询文档. CRUD操作包括创建.读取.更新和删 ...

  8. MongoDB的CRUD操作

    1. 前言 在上一篇文章中,我们介绍了MongoDB.现在,我们来看下如何在MongoDB中进行常规的CRUD操作.毕竟,作为一个存储系统,它的基本功能就是对数据进行增删改查操作. MongoDB中的 ...

  9. 【Java EE 学习 44】【Hibernate学习第一天】【Hibernate对单表的CRUD操作】

    一.Hibernate简介 1.hibernate是对jdbc的二次开发 2.jdbc没有缓存机制,但是hibernate有. 3.hibernate的有点和缺点 (1)优点:有缓存,而且是二级缓存: ...

随机推荐

  1. 27.Remove Element---两指针

    题目链接:https://leetcode.com/problems/remove-element/description/ 题目大意:给出一个数组和一个值,从数组中删除与当前值相等的值,并将数组长度 ...

  2. 自定义topo遇到的坑

    错误:TypeError: __init__() got an unexpected keyword argument 'delay' 解决办法:在创建topo的地方加一个link=TCLink即可, ...

  3. mysql 故障整理(2)

    导入备份数据时报错. mysql> system mysql -uroot -p < /root/mingongge_bak.sqlEnter password: ERROR 1840 ( ...

  4. C#中使用aria2c进行下载并显示进度条

    正则表达式的生成网站: http://www.txt2re.com/index-csharp.php3 Aria2c下载地址: https://github.com/aria2/aria2/relea ...

  5. asp.net core 身份认证/权限管理系统简介及简单案例

    如今的网站大多数都离不开账号注册及用户管理,而这些功能就是通常说的身份验证.这些常见功能微软都为我们做了封装,我们只要利用.net core提供的一些工具就可以很方便的搭建适用于大部分应用的权限管理系 ...

  6. (23)C#XML操作

    APP.config是一个典型的XML文件 打开vs2008在项目上右键-添加-新建项 选择应用程序配置文件,默认名称为APP.config,新建打开后默认代码如下 <?xml version= ...

  7. Educational Codeforces Round 31 B. Japanese Crosswords Strike Back【暴力】

    B. Japanese Crosswords Strike Back time limit per test 1 second memory limit per test 256 megabytes ...

  8. Java中堆和栈有什么区别

    stack 和 heep 都是内存的一部分stack 空间小,速度比较快, 用来放对象的引用heep 大,一般所有创建的对象都放在这里. 栈(stack):是一个先进后出的数据结构,通常用于保存方法( ...

  9. SQL RANK、DENSE_RANK、NTILE

    现有数据如下: SELECT *, ROW_NUMBER() OVER (ORDER BY 科目) AS 'Row Number', RANK() OVER (ORDER BY 科目) AS 'Ran ...

  10. 三. Java类与对象8.再谈Java包

    在Java中,为了组织代码的方便,可以将功能相似的类放到一个文件夹内,这个文件夹,就叫做包. 包不但可以包含类,还可以包含接口和其他的包. 目录以"\"来表示层级关系,例如 E:\ ...