Search in sources :

Example 16 with MongoClient

use of com.mongodb.MongoClient in project mongo-java-driver by mongodb.

the class CommandMonitoringTest method beforeClass.

@BeforeClass
public static void beforeClass() {
    commandListener = new TestCommandListener();
    mongoClient = new MongoClient(getMongoClientURI(MongoClientOptions.builder().addCommandListener(commandListener)));
}
Also used : MongoClient(com.mongodb.MongoClient) TestCommandListener(com.mongodb.connection.TestCommandListener) BeforeClass(org.junit.BeforeClass)

Example 17 with MongoClient

use of com.mongodb.MongoClient in project morphia by mongodb.

the class TestBase method setUp.

@Before
public void setUp() {
    final MongoClient mongoClient;
    try {
        mongoClient = new MongoClient();
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    mongoClient.dropDatabase("morphia_test");
    morphia = new Morphia();
    this.db = mongoClient.getDB("morphia_test");
    this.ds = this.morphia.createDatastore(mongoClient, this.db.getName());
}
Also used : MongoClient(com.mongodb.MongoClient) Morphia(org.mongodb.morphia.Morphia) Before(org.junit.Before)

Example 18 with MongoClient

use of com.mongodb.MongoClient in project morphia by mongodb.

the class Employee method main.

public static void main(final String[] args) throws UnknownHostException {
    final Morphia morphia = new Morphia();
    // tell morphia where to find your classes
    // can be called multiple times with different packages or classes
    morphia.mapPackage("org.mongodb.morphia.example");
    // create the Datastore connecting to the database running on the default port on the local host
    final Datastore datastore = morphia.createDatastore(new MongoClient(), "morphia_example");
    datastore.getDB().dropDatabase();
    datastore.ensureIndexes();
    final Employee elmer = new Employee("Elmer Fudd", 50000.0);
    datastore.save(elmer);
    final Employee daffy = new Employee("Daffy Duck", 40000.0);
    datastore.save(daffy);
    final Employee pepe = new Employee("Pepé Le Pew", 25000.0);
    datastore.save(pepe);
    elmer.getDirectReports().add(daffy);
    elmer.getDirectReports().add(pepe);
    datastore.save(elmer);
    Query<Employee> query = datastore.find(Employee.class);
    final List<Employee> employees = query.asList();
    Assert.assertEquals(3, employees.size());
    List<Employee> underpaid = datastore.find(Employee.class).filter("salary <=", 30000).asList();
    Assert.assertEquals(1, underpaid.size());
    underpaid = datastore.find(Employee.class).field("salary").lessThanOrEq(30000).asList();
    Assert.assertEquals(1, underpaid.size());
    final Query<Employee> underPaidQuery = datastore.find(Employee.class).filter("salary <=", 30000);
    final UpdateOperations<Employee> updateOperations = datastore.createUpdateOperations(Employee.class).inc("salary", 10000);
    final UpdateResults results = datastore.update(underPaidQuery, updateOperations);
    Assert.assertEquals(1, results.getUpdatedCount());
    final Query<Employee> overPaidQuery = datastore.find(Employee.class).filter("salary >", 100000);
    datastore.delete(overPaidQuery);
}
Also used : MongoClient(com.mongodb.MongoClient) Morphia(org.mongodb.morphia.Morphia) Datastore(org.mongodb.morphia.Datastore) UpdateResults(org.mongodb.morphia.query.UpdateResults)

Example 19 with MongoClient

use of com.mongodb.MongoClient in project openhab1-addons by openhab.

the class MongoDBPersistenceService method connectToDatabase.

/**
     * Connects to the database
     */
private void connectToDatabase() {
    try {
        logger.debug("Connect MongoDB");
        this.cl = new MongoClient(new MongoClientURI(this.url));
        mongoCollection = cl.getDB(this.db).getCollection(this.collection);
        BasicDBObject idx = new BasicDBObject();
        idx.append(FIELD_TIMESTAMP, 1).append(FIELD_ITEM, 1);
        this.mongoCollection.createIndex(idx);
        logger.debug("Connect MongoDB ... done");
    } catch (Exception e) {
        logger.error("Failed to connect to database {}", this.url);
        throw new RuntimeException("Cannot connect to database", e);
    }
}
Also used : MongoClient(com.mongodb.MongoClient) BasicDBObject(com.mongodb.BasicDBObject) MongoClientURI(com.mongodb.MongoClientURI) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 20 with MongoClient

use of com.mongodb.MongoClient in project spring-cloud-connectors by spring-cloud.

the class MongoDbFactoryCreator method createMongoDbFactory.

private SimpleMongoDbFactory createMongoDbFactory(MongoServiceInfo serviceInfo, MongoClientOptions.Builder mongoOptionsToUse) throws UnknownHostException {
    MongoClientURI mongoClientURI = new MongoClientURI(serviceInfo.getUri(), mongoOptionsToUse);
    MongoClient mongo = new MongoClient(mongoClientURI);
    return new SimpleMongoDbFactory(mongo, mongoClientURI.getDatabase());
}
Also used : MongoClient(com.mongodb.MongoClient) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MongoClientURI(com.mongodb.MongoClientURI)

Aggregations

MongoClient (com.mongodb.MongoClient)81 MongoClientURI (com.mongodb.MongoClientURI)27 Test (org.junit.Test)20 Document (org.bson.Document)15 ServerAddress (com.mongodb.ServerAddress)13 MongoDatabase (com.mongodb.client.MongoDatabase)13 Before (org.junit.Before)10 BasicDBObject (com.mongodb.BasicDBObject)9 MongoCredential (com.mongodb.MongoCredential)8 ArrayList (java.util.ArrayList)8 DBCollection (com.mongodb.DBCollection)7 DB (com.mongodb.DB)6 DBObject (com.mongodb.DBObject)5 IOException (java.io.IOException)5 MongoClientURIBuilder (com.mongodb.hadoop.util.MongoClientURIBuilder)4 File (java.io.File)4 Closer (com.google.common.io.Closer)3 BasicDBList (com.mongodb.BasicDBList)3 MongoClientOptions (com.mongodb.MongoClientOptions)3 MongoException (com.mongodb.MongoException)3