zoukankan      html  css  js  c++  java
  • 【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

  • 相关阅读:
    docker常用操作
    Mybatis架构与原理
    无服务计算小项目 : 基于Google Cloud Function + Cloud Firestore + Cloud SQL构建实时数据处理流
    Kafka 学习笔记(一)
    Spark 学习笔记 (三): Spark MLlib库的数据类型
    Code Jam Kickstart 2019 Round A 题解
    Spark 学习笔记 (二): 深入Spark计算引擎
    LeetCode 121th Weekly Contest 总结
    算法总结
    943.Find the Shortest Superstring --- 旅行商问题&状态压缩DP
  • 原文地址:https://www.cnblogs.com/cac2020/p/11338227.html
Copyright © 2011-2022 走看看