use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class BasicDBObjectTest method testGetObjectId.
@Test
public void testGetObjectId() {
final ObjectId objId = ObjectId.get();
BasicDBObject doc = new BasicDBObject("foo", objId);
assertTrue(doc.getObjectId("foo").equals(objId));
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class JSONCallbackTest method oidParsing.
@Test
public void oidParsing() {
ObjectId id = (ObjectId) JSON.parse(("{ \"$oid\" : \"01234567890123456789abcd\" }"));
assertEquals(new ObjectId("01234567890123456789abcd"), id);
}
use of org.bson.types.ObjectId 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));
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class JSONCallbackTest method refParsing.
@Test
public void refParsing() {
DBRef ref = (DBRef) JSON.parse(("{ \"$ref\" : \"friends\", \"$id\" : { \"$oid\" : \"01234567890123456789abcd\" } }"));
assertEquals("friends", ref.getCollectionName());
assertEquals(new ObjectId("01234567890123456789abcd"), ref.getId());
}
use of org.bson.types.ObjectId in project morphia by mongodb.
the class TestArrayUpdates method testUpdates.
@Test
public void testUpdates() {
BatchData theBatch = new BatchData();
theBatch.files.add(new Files(0, "fileName1", "fileHash1"));
theBatch.files.add(new Files(0, "fileName2", "fileHash2"));
getDs().save(theBatch);
ObjectId id = theBatch.id;
theBatch = new BatchData();
theBatch.files.add(new Files(0, "fileName3", "fileHash3"));
theBatch.files.add(new Files(0, "fileName4", "fileHash4"));
getDs().save(theBatch);
ObjectId id2 = theBatch.id;
UpdateOperations<BatchData> updateOperations = getDs().createUpdateOperations(BatchData.class).set("files.$.fileHash", "new hash");
getDs().update(getDs().find(BatchData.class).filter("_id", id).filter("files.fileName", "fileName1"), updateOperations);
BatchData data = getDs().find(BatchData.class).filter("_id", id).get();
Assert.assertEquals("new hash", data.files.get(0).fileHash);
Assert.assertEquals("fileHash2", data.files.get(1).fileHash);
data = getDs().find(BatchData.class).filter("_id", id2).get();
Assert.assertEquals("fileHash3", data.files.get(0).fileHash);
Assert.assertEquals("fileHash4", data.files.get(1).fileHash);
}
Aggregations