use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class DBCollectionTest method testWriteConcernExceptionOnFindAndModify.
@Test
public void testWriteConcernExceptionOnFindAndModify() throws UnknownHostException {
assumeThat(serverVersionAtLeast(3, 2), is(true));
assumeThat(isDiscoverableReplicaSet(), is(true));
ObjectId id = new ObjectId();
WriteConcern writeConcern = new WriteConcern(5, 1);
// FindAndUpdateOperation path
try {
collection.findAndModify(new BasicDBObject("_id", id), null, null, false, new BasicDBObject("$set", new BasicDBObject("x", 1)), true, true, writeConcern);
fail("Expected findAndModify to error");
} catch (WriteConcernException e) {
assertNotNull(e.getServerAddress());
assertNotNull(e.getErrorMessage());
assertTrue(e.getCode() > 0);
assertTrue(e.getWriteConcernResult().wasAcknowledged());
assertEquals(1, e.getWriteConcernResult().getCount());
assertFalse(e.getWriteConcernResult().isUpdateOfExisting());
assertEquals(new BsonObjectId(id), e.getWriteConcernResult().getUpsertedId());
}
// FindAndReplaceOperation path
try {
collection.findAndModify(new BasicDBObject("_id", id), null, null, false, new BasicDBObject("x", 1), true, true, writeConcern);
fail("Expected findAndModify to error");
} catch (WriteConcernException e) {
assertNotNull(e.getServerAddress());
assertNotNull(e.getErrorMessage());
assertTrue(e.getCode() > 0);
assertTrue(e.getWriteConcernResult().wasAcknowledged());
assertEquals(1, e.getWriteConcernResult().getCount());
assertTrue(e.getWriteConcernResult().isUpdateOfExisting());
assertNull(e.getWriteConcernResult().getUpsertedId());
}
// FindAndDeleteOperation path
try {
collection.findAndModify(new BasicDBObject("_id", id), null, null, true, null, false, false, writeConcern);
fail("Expected findAndModify to error");
} catch (WriteConcernException e) {
assertNotNull(e.getServerAddress());
assertNotNull(e.getErrorMessage());
assertTrue(e.getCode() > 0);
assertTrue(e.getWriteConcernResult().wasAcknowledged());
assertEquals(1, e.getWriteConcernResult().getCount());
assertFalse(e.getWriteConcernResult().isUpdateOfExisting());
assertNull(e.getWriteConcernResult().getUpsertedId());
}
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class ConcreteCodec method decode.
@Override
public Concrete decode(final BsonReader reader, final DecoderContext decoderContext) {
reader.readStartDocument();
ObjectId id = reader.readObjectId("_id");
String str = reader.readString("str");
int i = reader.readInt32("i");
long l = reader.readInt64("l");
double d = reader.readDouble("d");
long date = reader.readDateTime("date");
reader.readEndDocument();
return new Concrete(id, str, i, l, d, date);
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class MongoCollectionTest method testFindAndUpdateWithGenerics.
@Test
public void testFindAndUpdateWithGenerics() {
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new ConcreteCodecProvider()));
MongoCollection<Concrete> collection = database.getCollection(getCollectionName()).withDocumentClass(Concrete.class).withCodecRegistry(codecRegistry).withReadPreference(ReadPreference.primary()).withWriteConcern(WriteConcern.ACKNOWLEDGED);
Concrete doc = new Concrete(new ObjectId(), "str", 5, 10L, 4.0, 3290482390480L);
collection.insertOne(doc);
Concrete newDoc = collection.findOneAndUpdate(new Document("i", 5), new Document("$set", new Document("i", 6)));
assertNotNull(newDoc);
assertEquals(doc, newDoc);
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class ConcreteCodec method encode.
@Override
public void encode(final BsonWriter writer, final Concrete c, final EncoderContext encoderContext) {
writer.writeStartDocument();
if (!documentHasId(c)) {
c.setId(new ObjectId());
}
writer.writeObjectId("_id", c.getId());
writer.writeString("str", c.getStr());
writer.writeInt32("i", c.getI());
writer.writeInt64("l", c.getL());
writer.writeDouble("d", c.getD());
writer.writeDateTime("date", c.getDate());
writer.writeEndDocument();
}
use of org.bson.types.ObjectId in project mongo-java-driver by mongodb.
the class FindAndUpdateAcceptanceTest method shouldFindAndReplaceWithDocumentRequiringACustomEncoder.
@Test
public void shouldFindAndReplaceWithDocumentRequiringACustomEncoder() {
Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 7);
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new WorkerCodecProvider()));
MongoCollection<Worker> collection = database.getCollection(getCollectionName(), Worker.class).withCodecRegistry(codecRegistry);
collection.insertOne(pat);
assertThat(collection.count(), is(1L));
Document updateOperation = new Document("$inc", new Document("numberOfJobs", 1));
Worker updatedDocument = collection.findOneAndUpdate(new Document("name", "Pat"), updateOperation, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
assertThat("Worker returned from updateOneAndGet should have the", updatedDocument.getNumberOfJobs(), equalTo(8));
}
Aggregations