Search in sources :

Example 66 with BasicDBObject

use of com.mongodb.BasicDBObject in project mongo-hadoop by mongodb.

the class MongoLoaderTest method testSimpleMap.

@Test
public void testSimpleMap() throws Exception {
    //String userSchema = "m:[int]";
    // Note: before pig 0.9, explicitly setting the type for
    // map keys was not allowed, so can't test that here :(
    String userSchema = "m:[]";
    BasicDBObject obj = new BasicDBObject().append("k1", 1).append("k2", 2);
    MongoLoader ml = new MongoLoader(userSchema);
    Map m = (Map) BSONLoader.readField(obj, ml.getFields()[0]);
    assertEquals(2, m.size());
    assertEquals(1, m.get("k1"));
    assertEquals(2, m.get("k2"));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Map(java.util.Map) Test(org.junit.Test)

Example 67 with BasicDBObject

use of com.mongodb.BasicDBObject in project mongo-hadoop by mongodb.

the class MongoLoaderTest method testBagThatIsNotABag.

@Test
public void testBagThatIsNotABag() throws IOException {
    String userSchema = "b:{t:tuple(t1:chararray, t2:chararray)}";
    BasicDBObject notABag = new BasicDBObject();
    notABag.append("f1", new BasicDBObject().append("t1", "t11_value").append("t2", "t12_value"));
    notABag.append("f2", new BasicDBObject().append("t1", "t21_value").append("t2", "t22_value"));
    MongoLoader ml = new MongoLoader(userSchema);
    Object result = BSONLoader.readField(notABag, ml.getFields()[0]);
    assertNull(result);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 68 with BasicDBObject

use of com.mongodb.BasicDBObject in project mongo-hadoop by mongodb.

the class MongoOutputReader method readKeyValue.

@Override
public boolean readKeyValue() throws IOException {
    // Actually, just read the value as the key is embedded.
    try {
        currentValue.readFields(in);
        Object id = currentValue.getDoc().get("_id");
        currentKey.setDoc(new BasicDBObject("_id", id));
        // If successful we'll have an _id field
        return id != null;
    } catch (IndexOutOfBoundsException e) {
        // No more data
        LOG.info("No more data; no key/value pair read.");
        return false;
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 69 with BasicDBObject

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

the class Decimal128LegacyAPIQuickTour method main.

/**
     * Run this main method to see the output of this quick example.
     *
     * @param args takes an optional single argument for the connection string
     */
public static void main(final String[] args) {
    MongoClient mongoClient;
    if (args.length == 0) {
        // connect to the local database server
        mongoClient = new MongoClient();
    } else {
        mongoClient = new MongoClient(new MongoClientURI(args[0]));
    }
    // get handle to "mydb" database
    DB database = mongoClient.getDB("mydb");
    // get a handle to the "test" collection
    DBCollection collection = database.getCollection("test");
    // drop all the data in it
    collection.drop();
    // make a document and insert it
    BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("amount1", Decimal128.parse(".10")).append("amount2", new Decimal128(42L)).append("amount3", new Decimal128(new BigDecimal(".200")));
    collection.insert(doc);
    DBObject first = collection.findOne(QueryBuilder.start("amount1").is(new Decimal128(new BigDecimal(".10"))).get());
    Decimal128 amount3 = (Decimal128) first.get("amount3");
    BigDecimal amount2AsBigDecimal = amount3.bigDecimalValue();
    System.out.println(amount3.toString());
    System.out.println(amount2AsBigDecimal.toString());
}
Also used : MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) MongoClientURI(com.mongodb.MongoClientURI) Decimal128(org.bson.types.Decimal128) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DB(com.mongodb.DB) BigDecimal(java.math.BigDecimal)

Example 70 with BasicDBObject

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

the class GridFSDBFile method remove.

/**
     * Removes file from GridFS i.e. removes documents from files and chunks collections.
     */
void remove() {
    fs.getFilesCollection().remove(new BasicDBObject("_id", id));
    fs.getChunksCollection().remove(new BasicDBObject("files_id", id));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject)

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