热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java程序操作MongoDB插入、读取、修改以及删除数据

packagemongodb;importjava.net.UnknownHostException;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;importjava.util.Set;importcom.mongodb
package mongodb;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
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;
/**
 * java
 * mongodb的数据插入、读取、更新、删除
 */
public class MongodbCRUD {
        private static Mongo m = null;
        private static DB db = null;
        //数据集合名称
        private static final String COLLECTION_NAME = "mcpang";
        /*
         * 测试java处理mongodb的增、删、改、查操作
         */
        public static void main(String[] args) {
                //获取数据库连接
                startMongoDBConn();
                //保存数据
                createColData();
                //读取数据
                readColData();
                //更新数据
                updateColData();
                //读取数据
                readColData();
                //删除数据
                deleteColData();
                //读取数据
                readColData();
                //删除数据集
                db.getCollection(COLLECTION_NAME).drop();
                //关闭数据库连接
                stopMondoDBConn();
        }
        /**
         * 数据插入
         * *测试数据:
         * 【name:小李、age:30、address:江苏南京】
         * 【name:小张、age:25、address:江苏苏州】
         * @return
         */
        private static void createColData(){
                DBCollection dbCol = db.getCollection(COLLECTION_NAME);
                System.out.println("向数据集中插入数据开始:");
                List dbList = new ArrayList();
                BasicDBObject doc1 = new BasicDBObject();
                doc1.put("name", "小李");
                doc1.put("age", 30);
                doc1.put("address", "江苏南京");
                dbList.add(doc1);
                BasicDBObject doc2 = new BasicDBObject();
                doc2.put("name", "小张");
                doc2.put("age", 25);
                doc2.put("address", "江苏苏州");
                dbList.add(doc2);
                dbCol.insert(dbList);
                System.out.println("向数据集中插入数据完成!");
                System.out.println("------------------------------");
        }
        /**
         * 数据读取
         */
        private static void readColData(){
                DBCollection dbCol = db.getCollection(COLLECTION_NAME);
                DBCursor ret = dbCol.find();
                System.out.println("从数据集中读取数据:");
                while(ret.hasNext()){
                        BasicDBObject bdbObj = (BasicDBObject) ret.next();
                        if(bdbObj != null){
                                System.out.println("name:"+bdbObj.getString("name"));
                                System.out.println("age:"+bdbObj.getInt("age"));
                                System.out.println("address:"+bdbObj.getString("address"));
                        }
                }
        }
        /**
         * 数据更新
         * update(q, o, upsert, multi)
         * update(q, o, upsert, multi, concern)
         * update(arg0, arg1, arg2, arg3, arg4, arg5)
         * updateMulti(q, o)
         */
        private static void updateColData(){
                System.out.println("------------------------------");
                DBCollection dbCol = db.getCollection(COLLECTION_NAME);
                DBCursor ret = dbCol.find();
                BasicDBObject doc = new BasicDBObject();
                BasicDBObject res = new BasicDBObject();
                res.put("age", 40);
                System.out.println("将数据集中的所有文档的age修改成40!");
                doc.put("$set", res);
                dbCol.update(new BasicDBObject(),doc,false,true);
                System.out.println("更新数据完成!");
                System.out.println("------------------------------");
        }
        /**
         * 数据删除
         */
        private static void deleteColData(){
                System.out.println("------------------------------");
                DBCollection dbCol = db.getCollection(COLLECTION_NAME);
                System.out.println("删除【小李】!");
                BasicDBObject doc = new BasicDBObject();
                doc.put("name", "小李");
                dbCol.remove(doc);
                System.out.println("------------------------------");
        }
        /**
         * 关闭mongodb数据库连接
         */
        private static void stopMondoDBConn(){
                if (null != m) {
                        if (null != db) {
                                // 结束Mongo数据库的事务请求
                                try {
                                        db.requestDone();
                                } catch(Exception e) {
                                        e.printStackTrace();
                                }
                        }
                        try
                        {
                                m.close();
                        } catch(Exception e) {
                                e.printStackTrace();
                        }
                        m = null;
                        db = null;
                }
        }
        /**
         * 获取mongodb数据库连接
         */
        private static void startMongoDBConn(){
                try {
                        //Mongo(p1, p2):p1=>IP地址     p2=>端口
                        m = new Mongo("127.0.0.1", 27017);
                        //根据mongodb数据库的名称获取mongodb对象
                        db = m.getDB("sdap");
                        //校验用户密码是否正确
                        if (!db.authenticate("sdap", "sdap123".toCharArray())){
                                System.out.println("连接MongoDB数据库,校验失败!");
                        }else{
                                System.out.println("连接MongoDB数据库,校验成功!");
                        }
                } catch (UnknownHostException e) {
                        e.printStackTrace();
                } catch (MongoException e) {
                        e.printStackTrace();
                }
        }
}

执行结果:

连接MongoDB数据库,校验成功!
向数据集中插入数据开始:
向数据集中插入数据完成!
------------------------------
从数据集中读取数据:
name:小李
age:30
address:江苏南京
name:小张
age:25
address:江苏苏州
------------------------------
将数据集中的所有文档的age修改成40!
更新数据完成!
------------------------------
从数据集中读取数据:
name:小李
age:40
address:江苏南京
name:小张
age:40
address:江苏苏州
------------------------------
删除【小李】!
------------------------------
从数据集中读取数据:
name:小张
age:40
address:江苏苏州


推荐阅读
author-avatar
mobiledu2502887833
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有