Search in sources :

Example 96 with BasicDBObject

use of com.mongodb.BasicDBObject 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 97 with BasicDBObject

use of com.mongodb.BasicDBObject in project querydsl by querydsl.

the class MongodbQueryTest method asDBObject.

@Test
public void asDBObject() {
    MorphiaQuery<User> query = query();
    query.where(user.firstName.eq("Bob"), user.lastName.eq("Wilson"));
    assertEquals(new BasicDBObject().append("firstName", "Bob").append("lastName", "Wilson"), query.asDBObject());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 98 with BasicDBObject

use of com.mongodb.BasicDBObject in project querydsl by querydsl.

the class MongodbSerializerTest method collectionIsNotEmpty.

@Test
public void collectionIsNotEmpty() {
    BasicDBObject expected = dbo("$nor", dblist(dbo("addresses", dblist()), dbo("addresses", dbo("$exists", false))));
    assertQuery(QUser.user.addresses.isNotEmpty(), expected);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 99 with BasicDBObject

use of com.mongodb.BasicDBObject in project querydsl by querydsl.

the class MongodbSerializerTest method collectionIsEmpty.

@Test
public void collectionIsEmpty() {
    BasicDBObject expected = dbo("$or", dblist(dbo("addresses", dblist()), dbo("addresses", dbo("$exists", false))));
    assertQuery(QUser.user.addresses.isEmpty(), expected);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 100 with BasicDBObject

use of com.mongodb.BasicDBObject in project spring-data-document-examples by spring-projects.

the class ChartController method getTopRecommendedRestaurants.

public DBObject getTopRecommendedRestaurants(MongoTemplate mongoTemplate) {
    //This circumvents exception translation
    DBCollection collection = mongoTemplate.getCollection("mvc");
    Date startDate = createDate(1, 5, 2010);
    Date endDate = createDate(1, 12, 2010);
    DBObject cond = QueryBuilder.start("date").greaterThanEquals(startDate).lessThan(endDate).and("action").is("addFavoriteRestaurant").get();
    DBObject key = new BasicDBObject("parameters.p1", true);
    DBObject intitial = new BasicDBObject("count", 0);
    DBObject result = collection.group(key, cond, intitial, "function(doc, out){ out.count++; }");
    return result;
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Date(java.util.Date)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)497 DBObject (com.mongodb.DBObject)289 Test (org.junit.Test)166 DBCollection (com.mongodb.DBCollection)72 ArrayList (java.util.ArrayList)48 ObjectId (org.bson.types.ObjectId)40 DBCursor (com.mongodb.DBCursor)38 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)38 ApiOperation (io.swagger.annotations.ApiOperation)37 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)36 HashMap (java.util.HashMap)32 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)30 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)29 BasicDBList (com.mongodb.BasicDBList)28 List (java.util.List)24 Map (java.util.Map)23 BSONObject (org.bson.BSONObject)23 MongoException (com.mongodb.MongoException)22 Date (java.util.Date)18 IOException (java.io.IOException)15