MongoDB-java-api

MongoDB-java-api
package com.me.Utils;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: wzs
 * @Date: 2020/10/23 17:13
 * @Description:
 */
public class MongoUtil {
    //获取连接 dbName 数据库名称
    public static MongoDatabase getConn(String dbName){
        try{
            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient( "hadoop102" , 27017 );

            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase(dbName);
            System.out.println("Connect to database successfully");
            return mongoDatabase;
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            return null;
        }
    }

    //创建集合 (表) collectionName 表名
    public static void createCollection(String dbName,String collectionName){
        MongoDatabase conn = getConn(dbName);
        conn.createCollection(collectionName);
        System.out.println("集合创建成功");
    }

    //获取集合
    public static MongoCollection<Document> getCollection(String dbName,String collectionName){
        MongoDatabase conn = getConn(dbName);
        MongoCollection<Document> collection = conn.getCollection(collectionName);
        System.out.println("集合 test 选择成功");
        return collection;
    }

    //插入集合
    public static void insertCollection(String dbName,String collectionName,Document document){
        MongoCollection<Document> collection = getCollection(dbName, collectionName);
        //插入文档
        /**
         * 1. 创建文档 org.bson.Document 参数为key-value的格式
         * 2. 创建文档集合List<Document>
         * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>) 插入单个文档可以用 mongoCollection.insertOne(Document)
         * */
//        Document document = new Document("title", "MongoDB").
//                append("description", "database").
//                append("likes", 100).
//                append("by", "Fly");
        List<Document> documents = new ArrayList<Document>();
        documents.add(document);
        collection.insertMany(documents);
        System.out.println("文档插入成功");
    }

    //插入集合(多条)
    public static void insertCollection_(String dbName,String collectionName,List<Document> documents){
        MongoCollection<Document> collection = getCollection(dbName, collectionName);
        collection.insertMany(documents);
        System.out.println("多条文档插入成功");
    }

    //检索文档 all
    public static void selectCollection(String dbName,String collectionName){
        MongoCollection<Document> collection = getCollection(dbName, collectionName);
        //检索所有文档
        /**
         * 1. 获取迭代器FindIterable<Document>
         * 2. 获取游标MongoCursor<Document>
         * 3. 通过游标遍历检索出的文档集合
         * */
        FindIterable<Document> findIterable = collection.find();
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        while(mongoCursor.hasNext()){
            System.out.println(mongoCursor.next());
        }
    }

    //检索文档 all
    public static void selectCollection(String dbName, String collectionName, BasicDBObject basicDBObject,BasicDBObject basicDBObjectH){
        MongoCollection<Document> collection = getCollection(dbName, collectionName);
        //检索所有文档
        /**
         * 1. 获取迭代器FindIterable<Document>
         * 2. 获取游标MongoCursor<Document>
         * 3. 通过游标遍历检索出的文档集合
         * */
        FindIterable<Document> findIterable = collection.find(basicDBObject).projection(basicDBObjectH);
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        while(mongoCursor.hasNext()){
            System.out.println(mongoCursor.next());
        }

    }

  




    //更新文档
    public static void updateCollection(String dbName,String collectionName){
        MongoCollection<Document> collection = getCollection(dbName, collectionName);
        //更新文档   将文档中likes=100的文档修改为likes=200
        collection.updateMany(Filters.eq("likes", 100), new Document("$set",new Document("likes",200)));
    }

    //删除文档
    public static void deleteCollection(String dbName,String collectionName){
        MongoCollection<Document> collection = getCollection(dbName, collectionName);
        //删除符合条件的第一个文档
        collection.deleteOne(Filters.eq("likes", 200));
        //删除所有符合条件的文档
        collection.deleteMany (Filters.eq("likes", 200));
    }


    public static void main(String[] args) {
        BasicDBObject basicDBObject = new BasicDBObject("name", "zhangsan");
        BasicDBObject basicDBObject2 = new BasicDBObject("name",0);
        selectCollection("student","student",basicDBObject,basicDBObject2);

    }


}
View Code

 

MongoDB-java-api

上一篇:Hadoop学习笔记【分布式文件系统学习笔记】


下一篇:composer install 报错Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1 PackageManifest.php: Undefined index: name