Search in sources :

Example 41 with MongoClient

use of com.mongodb.MongoClient in project spring-data-mongodb by spring-projects.

the class DbRefMappingBenchmark method setUp.

@Setup
public void setUp() throws Exception {
    client = new MongoClient(new ServerAddress());
    template = new MongoTemplate(client, DB_NAME);
    List<RefObject> refObjects = new ArrayList<>();
    for (int i = 0; i < 1; i++) {
        RefObject o = new RefObject();
        template.save(o);
        refObjects.add(o);
    }
    ObjectWithDBRef singleDBRef = new ObjectWithDBRef();
    singleDBRef.ref = refObjects.iterator().next();
    template.save(singleDBRef);
    ObjectWithDBRef multipleDBRefs = new ObjectWithDBRef();
    multipleDBRefs.refList = refObjects;
    template.save(multipleDBRefs);
    queryObjectWithDBRef = query(where("id").is(singleDBRef.id));
    queryObjectWithDBRefList = query(where("id").is(multipleDBRefs.id));
}
Also used : MongoClient(com.mongodb.MongoClient) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Setup(org.openjdk.jmh.annotations.Setup)

Example 42 with MongoClient

use of com.mongodb.MongoClient in project spring-data-mongodb by spring-projects.

the class MongoResultsWriter method write.

@Override
public void write(Collection<RunResult> results) {
    Date now = new Date();
    StandardEnvironment env = new StandardEnvironment();
    String projectVersion = env.getProperty("project.version", "unknown");
    String gitBranch = env.getProperty("git.branch", "unknown");
    String gitDirty = env.getProperty("git.dirty", "no");
    String gitCommitId = env.getProperty("git.commit.id", "unknown");
    MongoClientURI uri = new MongoClientURI(this.uri);
    MongoClient client = new MongoClient(uri);
    String dbName = StringUtils.hasText(uri.getDatabase()) ? uri.getDatabase() : "spring-data-mongodb-benchmarks";
    MongoDatabase db = client.getDatabase(dbName);
    for (BasicDBObject dbo : (List<BasicDBObject>) JSON.parse(ResultsWriter.jsonifyResults(results))) {
        String collectionName = extractClass(dbo.get("benchmark").toString());
        Document sink = new Document();
        sink.append("_version", projectVersion);
        sink.append("_branch", gitBranch);
        sink.append("_commit", gitCommitId);
        sink.append("_dirty", gitDirty);
        sink.append("_method", extractBenchmarkName(dbo.get("benchmark").toString()));
        sink.append("_date", now);
        sink.append("_snapshot", projectVersion.toLowerCase().contains("snapshot"));
        sink.putAll(dbo);
        db.getCollection(collectionName).insertOne(fixDocumentKeys(sink));
    }
    client.close();
}
Also used : MongoClient(com.mongodb.MongoClient) BasicDBObject(com.mongodb.BasicDBObject) MongoClientURI(com.mongodb.MongoClientURI) List(java.util.List) Document(org.bson.Document) Date(java.util.Date) StandardEnvironment(org.springframework.core.env.StandardEnvironment) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 43 with MongoClient

use of com.mongodb.MongoClient in project spring-data-mongodb by spring-projects.

the class MongoVersionRule method fetchCurrentVersion.

private Version fetchCurrentVersion() {
    try {
        MongoClient client;
        client = new MongoClient(host, port);
        MongoDatabase database = client.getDatabase("test");
        Document result = database.runCommand(new Document("buildInfo", 1));
        client.close();
        return Version.parse(result.get("version", String.class));
    } catch (Exception e) {
        return ANY;
    }
}
Also used : MongoClient(com.mongodb.MongoClient) Document(org.bson.Document) AssumptionViolatedException(org.junit.AssumptionViolatedException) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 44 with MongoClient

use of com.mongodb.MongoClient in project spring-data-mongodb by spring-projects.

the class ReplicaSet method runsAsReplicaSet.

public boolean runsAsReplicaSet() {
    if (runsAsReplicaSet.get() == null) {
        try (MongoClient client = new MongoClient()) {
            boolean tmp = client.getDatabase("admin").runCommand(new Document("getCmdLineOpts", "1")).get("argv", List.class).contains("--replSet");
            runsAsReplicaSet.compareAndSet(null, tmp);
        }
    }
    return runsAsReplicaSet.get();
}
Also used : MongoClient(com.mongodb.MongoClient) List(java.util.List) Document(org.bson.Document)

Example 45 with MongoClient

use of com.mongodb.MongoClient in project spring-data-mongodb by spring-projects.

the class DefaultMessageListenerContainerTests method setUp.

@Before
public void setUp() {
    dbFactory = new SimpleMongoDbFactory(new MongoClient(), DATABASE_NAME);
    template = new MongoTemplate(dbFactory);
    template.dropCollection(COLLECTION_NAME);
    collection = template.getCollection(COLLECTION_NAME);
    messageListener = new CollectingMessageListener<>();
}
Also used : MongoClient(com.mongodb.MongoClient) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Before(org.junit.Before)

Aggregations

MongoClient (com.mongodb.MongoClient)126 Test (org.junit.Test)31 MongoClientURI (com.mongodb.MongoClientURI)29 Document (org.bson.Document)26 ServerAddress (com.mongodb.ServerAddress)21 MongoDatabase (com.mongodb.client.MongoDatabase)21 Before (org.junit.Before)20 BasicDBObject (com.mongodb.BasicDBObject)11 ArrayList (java.util.ArrayList)11 MongoCredential (com.mongodb.MongoCredential)9 MongoException (com.mongodb.MongoException)8 DB (com.mongodb.DB)7 DBCollection (com.mongodb.DBCollection)7 UnknownHostException (java.net.UnknownHostException)7 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)7 DBObject (com.mongodb.DBObject)6 MongoClientOptions (com.mongodb.MongoClientOptions)6 IOException (java.io.IOException)5 List (java.util.List)5 MongoDbFactory (org.springframework.data.mongodb.MongoDbFactory)5