Search in sources :

Example 76 with BasicDBObject

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

the class GridFSTest method testOutStream.

void testOutStream(final String s) throws Exception {
    int[] start = getCurrentCollectionCounts();
    GridFSInputFile in = gridFS.createFile();
    OutputStream writeStream = in.getOutputStream();
    writeStream.write(s.getBytes(defaultCharset()), 0, s.length());
    writeStream.close();
    GridFSDBFile out = gridFS.findOne(new BasicDBObject("_id", in.getId()));
    assert (out.getId().equals(in.getId()));
    assert (out.getChunkSize() == (long) GridFS.DEFAULT_CHUNKSIZE);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    out.writeTo(bout);
    String outString = new String(bout.toByteArray(), defaultCharset());
    assert (outString.equals(s));
    out.remove();
    int[] end = getCurrentCollectionCounts();
    assertEquals(start[0], end[0]);
    assertEquals(start[1], end[1]);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 77 with BasicDBObject

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

the class GridFSTest method testMetadata.

@Test
public void testMetadata() throws Exception {
    GridFSInputFile in = gridFS.createFile("foo".getBytes(defaultCharset()));
    in.put("meta", 5);
    in.save();
    GridFSDBFile out = gridFS.findOne(new BasicDBObject("_id", in.getId()));
    assertTrue(out.get("meta").equals(5));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 78 with BasicDBObject

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

the class JSONTest method testNumbers2.

@Test
public void testNumbers2() {
    DBObject x = new BasicDBObject("x", 123);
    assertEquals(JSON.parse(x.toString()), x);
    x = new BasicDBObject("x", 123123123123L);
    assertEquals(JSON.parse(x.toString()), x);
    x = new BasicDBObject("x", 123123123);
    assertEquals(JSON.parse(x.toString()), x);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 79 with BasicDBObject

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

the class JSONTest method testJSONEncoding.

@Test
public void testJSONEncoding() throws ParseException {
    String json = "{ 'str' : 'asdfasd' , 'long' : 123123123123 , 'int' : 5 , 'float' : 0.4 , 'bool' : false , " + "'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , " + "'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , " + "'$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , " + "'codews' : { '$code' : 'ggggg' , " + "'$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' :  null, " + "'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}";
    BasicDBObject a = (BasicDBObject) JSON.parse(json);
    assertEquals("asdfasd", a.get("str"));
    assertEquals(5, a.get("int"));
    assertEquals(123123123123L, a.get("long"));
    assertEquals(0.4d, a.get("float"));
    assertEquals(false, a.get("bool"));
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
    assertEquals(format.parse("2011-05-18T18:56:00Z"), a.get("date"));
    Pattern pat = (Pattern) a.get("pat");
    Pattern pat2 = Pattern.compile(".*", BSON.regexFlags(""));
    assertEquals(pat.pattern(), pat2.pattern());
    assertEquals(pat.flags(), pat2.flags());
    ObjectId oid = (ObjectId) a.get("oid");
    assertEquals(new ObjectId("4d83ab3ea39562db9c1ae2ae"), oid);
    //        DBRef ref = (DBRef) a.get("ref");
    //        assertEquals(new DBRef(null, "test.test", new ObjectId("4d83ab59a39562db9c1ae2af")), ref);
    assertEquals(new Code("asdfdsa"), a.get("code"));
    assertEquals(new CodeWScope("ggggg", new BasicBSONObject()), a.get("codews"));
    assertEquals(new BSONTimestamp(1300474885, 10), a.get("ts"));
    assertEquals(UUID.fromString("60f65152-6d4a-4f11-9c9b-590b575da7b5"), a.get("uuid"));
    String json2 = JSON.serialize(a);
    BasicDBObject b = (BasicDBObject) JSON.parse(json2);
    assertEquals(JSON.serialize(b), JSON.serialize(b));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Pattern(java.util.regex.Pattern) BasicBSONObject(org.bson.BasicBSONObject) SimpleTimeZone(java.util.SimpleTimeZone) ObjectId(org.bson.types.ObjectId) GregorianCalendar(java.util.GregorianCalendar) BSONTimestamp(org.bson.types.BSONTimestamp) SimpleDateFormat(java.text.SimpleDateFormat) Code(org.bson.types.Code) CodeWScope(org.bson.types.CodeWScope) Test(org.junit.Test)

Example 80 with BasicDBObject

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

the class JSONTest method testEscape1.

@Test
public void testEscape1() {
    String raw = "a\nb";
    DBObject x = new BasicDBObject("x", raw);
    assertEquals("\"a\\nb\"", JSON.serialize(raw));
    assertEquals(x, JSON.parse(x.toString()));
    assertEquals(raw, ((DBObject) JSON.parse(x.toString())).get("x"));
    x = new BasicDBObject("x", "a\nb\bc\td\re");
    assertEquals(x, JSON.parse(x.toString()));
    String thingy = "va\"lue";
    x = new BasicDBObject("name", thingy);
    x = (DBObject) JSON.parse(x.toString());
    assertEquals(thingy, x.get("name"));
    thingy = "va\\lue";
    x = new BasicDBObject("name", thingy);
    x = (DBObject) JSON.parse(x.toString());
    assertEquals(thingy, x.get("name"));
    assertEquals("va/lue", JSON.parse("\"va\\/lue\""));
    assertEquals("value", JSON.parse("\"va\\lue\""));
    assertEquals("va\\lue", JSON.parse("\"va\\\\lue\""));
    escapeChar("\t");
    escapeChar("\b");
    escapeChar("\n");
    escapeChar("\r");
    escapeChar("\'");
    escapeChar("\"");
    escapeChar("\\");
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

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