• 【MongoDB学习之五】Java中使用MongoDB


    环境
      MongoDB 3.0
      CentOS6.5_x64

    相关jar:mongo-java-driver-3.0.0.jar

    package com.mongodb;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bson.Document;
    import org.bson.conversions.Bson;
    import org.bson.types.ObjectId;
    
    import com.mongodb.client.FindIterable;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoCursor;
    import com.mongodb.client.MongoDatabase;
    
    public class TestMongoDB {
    
        public static void main(String[] args) 
        {
            TestMongoDB test = new TestMongoDB();
            //插入数据
            test.insertData();
            //查询数据
            test.find();
            test.findone();
            //更新数据
            test.update();
            //删除数据
            test.delete();
        }
        
        public void find()
        {
            //创建连接客户端
                    MongoClient client = new MongoClient("localhost",27017);
                    //获取数据库对象
                    MongoDatabase db = client.getDatabase("test");
                    //获取操作的集合对象
                    MongoCollection<Document> collection = db.getCollection("wjy");
                    //具体操作
                    FindIterable<Document> it = collection.find();
                    //获取游标对象
                    MongoCursor<Document> cursor = it.iterator();
                    while (cursor.hasNext())
                    {
                        //取出每一个文档对象(行)
                        Document doc = cursor.next();
                        String name = doc.getString("name");
                        if ("乔峰".equals(name))
                        {
                            Integer age = doc.getInteger("age");
                            System.out.println(name+","+age);
                        }
                        else
                        {
                            Double age = doc.getDouble("age");
                            System.out.println(name+","+age);
                        }
                        
                    }
                    //释放资源
                    cursor.close();
                    client.close();
        }
        
        public void findone()
        {
            MongoClient client = new MongoClient("localhost",27017);
            //获取数据库对象
            MongoDatabase db = client.getDatabase("test");
            //获取操作的集合对象
            MongoCollection<Document> collection = db.getCollection("wjy");
            //具体操作
            Bson filter = new BasicDBObject("_id", new ObjectId("5850eacd7065f52b0eab7ff4"));
            FindIterable<Document> it = collection.find(filter);
            //获取游标对象
            MongoCursor<Document> cursor = it.iterator();
            while (cursor.hasNext())
            {
                //取出每一个文档对象(行)
                Document doc = cursor.next();
                String name = doc.getString("name");
                if ("乔峰".equals(name))
                {
                    Integer age = doc.getInteger("age");
                    System.out.println(name+","+age);
                }
                else
                {
                    Double age = doc.getDouble("age");
                    System.out.println(name+","+age);
                }
                
            }
            //释放资源
            cursor.close();
            client.close();
        }
        
        public void insertData()
        {
            MongoClient client = new MongoClient("localhost",27017);
            MongoDatabase db = client.getDatabase("test");
            MongoCollection<Document> collection = db.getCollection("wjy");
            //使用Map来封装json数据
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("name", "乔峰");
            map.put("age", 35);
            map.put("gender", "true man");
            Map<String,Object> hobbyMap = new HashMap<String,Object>();
            hobbyMap.put("girl", "阿朱");
            hobbyMap.put("gongfu", "降龙十八掌");
            map.put("hobby", hobbyMap);
            Document doc = new Document(map);
            
            collection.insertOne(doc);
            
            client.close();
        }
        
        public void update()
        {
            MongoClient client = new MongoClient("localhost",27017);
            MongoDatabase db = client.getDatabase("test");
            MongoCollection<Document> collection = db.getCollection("wjy");
            
            Bson filter = new BasicDBObject("_id",new ObjectId("5850eacd7065f52b0eab7ff4"));
            
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("name", "张无忌");
            map.put("age", 35);
            Bson update = new BasicDBObject(map);
            
            collection.updateOne(filter, new BasicDBObject("$set",update));
            
            client.close();
        }
        public void delete()
        {
            MongoClient client = new MongoClient("localhost",27017);
            MongoDatabase db = client.getDatabase("test");
            MongoCollection<Document> collection = db.getCollection("wjy");
            
            collection.deleteOne(new BasicDBObject("_id",new ObjectId("5850eacd7065f52b0eab7ff4")));
            
            client.close();
        }
    }

    参考:

    https://www.w3cschool.cn/mongodb/mongodb-java.html

  • 相关阅读:
    validation 参数效验框架
    小酌一下:Maven
    小酌一下:git 常用命令
    小酌一下:anaconda 基本操作
    小酌一下:Win10 解决fetch_20newsgroups下载速度巨慢
    学习笔记:Python3 异常处理
    学习笔记:Python3 面向对象
    学习笔记:Python3 函数式编程
    学习笔记:Python3 函数
    学习笔记:Python3 高级特性
  • 原文地址:https://www.cnblogs.com/cac2020/p/11338227.html
Copyright © 2020-2023  润新知