Search in sources :

Example 31 with ObjectId

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());
    }
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonObjectId(org.bson.BsonObjectId) Test(org.junit.Test)

Example 32 with ObjectId

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);
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId)

Example 33 with ObjectId

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);
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) ObjectId(org.bson.types.ObjectId) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) Document(org.bson.Document) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 34 with ObjectId

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();
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId)

Example 35 with ObjectId

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));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) ObjectId(org.bson.types.ObjectId) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) WorkerCodecProvider(com.mongodb.client.test.WorkerCodecProvider) Worker(com.mongodb.client.test.Worker) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Date(java.util.Date) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Aggregations

ObjectId (org.bson.types.ObjectId)249 Test (org.junit.Test)157 BasicDBObject (com.mongodb.BasicDBObject)54 Document (org.bson.Document)38 DBObject (com.mongodb.DBObject)35 ArrayList (java.util.ArrayList)28 BsonObjectId (org.bson.BsonObjectId)27 Date (java.util.Date)24 DBRef (com.mongodb.DBRef)15 List (java.util.List)15 StreamRuleMock (org.graylog2.streams.matchers.StreamRuleMock)14 Message (org.graylog2.plugin.Message)13 BasicBSONObject (org.bson.BasicBSONObject)12 Query (org.springframework.data.mongodb.core.query.Query)11 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)10 Map (java.util.Map)10 Binary (org.bson.types.Binary)10 GridFSFindIterable (com.mongodb.client.gridfs.GridFSFindIterable)8 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)7 Code (org.bson.types.Code)7