MongoDB教程
作者: 时海
Java连接Mongo

1、Maven依赖

 <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
            <version>3.2.2</version>
</dependency>

2、简单Mongo

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class MongoUtil {
    private static MongoClient mongoClient = new MongoClient("localhost",27017);

    public static MongoCollection<Document> getCollection(String dbName, String collectionName) {
        MongoDatabase mongoDatabase = mongoClient.getDatabase(dbName);
        MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
        return collection;
    }

    public static void close() {
        try {
            mongoClient.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

3、复杂连接Mongo,同时设置参数选项


import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ReadPreference;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.Arrays;

public class MongoUtil {

    private static MongoClient mongoClient = null;

    static {
        MongoClientOptions.Builder build = new MongoClientOptions.Builder();
        build.requiredReplicaSetName("name");
        build.readPreference(ReadPreference.secondaryPreferred());
        build.connectTimeout(60000);

        mongoClient = new MongoClient(
                Arrays.asList(
                        new ServerAddress("ip1", port1),
                        new ServerAddress("ip2", port2)
                ),
                build.build()
        );

    }

    public static MongoCollection<Document> getCollection(String dbName, String collectionName) {
        MongoDatabase mongoDatabase = mongoClient.getDatabase(dbName);
        MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
        return collection;

    }

    public static void close() {
        try {
            mongoClient.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}


4、使用MongoClientURI

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class MongoUtil {

    private static MongoClient mongoClient = null;

    static {
        mongoClient = new MongoClient(new MongoClientURI("mongodb://ip1:port1," +
                "ip2:port2/contact?replicaSet=contactnew&readPreference=secondaryPreferred&readPreferenceTags" +
                "=tag1&readPreferenceTags=tag2&maxPoolSize=15&waitQueueMultiple=3" +
                "&connectTimeoutMS=600000"));

    }

    public static MongoCollection<Document> getCollection(String dbName, String collectionName) {
        MongoDatabase mongoDatabase = mongoClient.getDatabase(dbName);
        MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
        return collection;

    }

    public static void close() {
        try {
            mongoClient.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}


标签: mongoclient、mongodatabase、mongodb、mongocollection、import
一个创业中的苦逼程序员
  • 回复
隐藏