【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

 

posted @ 2019-08-12 10:25  cac2020  阅读(278)  评论(0编辑  收藏  举报