Search in sources :

Example 31 with BasicDBObject

use of com.mongodb.BasicDBObject in project camel by apache.

the class MongoDbDynamicityTest method testInsertDynamicityEnabledCollectionOnly.

@Test
public void testInsertDynamicityEnabledCollectionOnly() {
    assertEquals(0, testCollection.count());
    mongo.getDatabase("otherDB").drop();
    db.getCollection("otherCollection").drop();
    assertFalse("The otherDB database should not exist", StreamSupport.stream(mongo.listDatabaseNames().spliterator(), false).anyMatch("otherDB"::equals));
    String body = "{\"_id\": \"testInsertDynamicityEnabledCollectionOnly\", \"a\" : \"1\"}";
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(MongoDbConstants.COLLECTION, "otherCollection");
    Object result = template.requestBodyAndHeaders("direct:dynamicityEnabled", body, headers);
    MongoCollection<BasicDBObject> dynamicCollection = db.getCollection("otherCollection", BasicDBObject.class);
    DBObject b = dynamicCollection.find(new BasicDBObject("_id", "testInsertDynamicityEnabledCollectionOnly")).first();
    assertNotNull("No record with 'testInsertDynamicityEnabledCollectionOnly' _id", b);
    b = testCollection.find(new BasicDBObject("_id", "testInsertDynamicityEnabledDBOnly")).first();
    assertNull("There is a record with 'testInsertDynamicityEnabledCollectionOnly' _id in the test collection", b);
    assertFalse("The otherDB database should not exist", StreamSupport.stream(mongo.listDatabaseNames().spliterator(), false).anyMatch("otherDB"::equals));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) HashMap(java.util.HashMap) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 32 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 33 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 34 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 35 with BasicDBObject

use of com.mongodb.BasicDBObject in project jetty.project by eclipse.

the class MongoTest method main.

public static void main(String... args) throws Exception {
    Mongo m = new Mongo("127.0.0.1", 27017);
    DB db = m.getDB("mydb");
    Set<String> colls = db.getCollectionNames();
    System.err.println("Colls=" + colls);
    DBCollection coll = db.getCollection("testCollection");
    BasicDBObject key = new BasicDBObject("id", "1234");
    BasicDBObject sets = new BasicDBObject("name", "value");
    BasicDBObject upsert = new BasicDBObject("$set", sets);
    WriteResult result = coll.update(key, upsert, true, false);
    System.err.println(result.getLastError());
    while (coll.count() > 0) {
        DBObject docZ = coll.findOne();
        System.err.println("removing    " + docZ);
        if (docZ != null)
            coll.remove(docZ);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) WriteResult(com.mongodb.WriteResult) Mongo(com.mongodb.Mongo) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DB(com.mongodb.DB)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)566 DBObject (com.mongodb.DBObject)338 Test (org.junit.Test)175 DBCollection (com.mongodb.DBCollection)77 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)68 ApiOperation (io.swagger.annotations.ApiOperation)65 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)64 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)54 ArrayList (java.util.ArrayList)53 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)52 ObjectId (org.bson.types.ObjectId)43 DBCursor (com.mongodb.DBCursor)41 HashMap (java.util.HashMap)35 BasicDBList (com.mongodb.BasicDBList)32 List (java.util.List)30 Map (java.util.Map)26 BSONObject (org.bson.BSONObject)23 MongoException (com.mongodb.MongoException)22 Date (java.util.Date)22 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)18