Search in sources :

Example 31 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project mongo-java-driver by mongodb.

the class BsonHelper method toBson.

@SuppressWarnings("unchecked")
static BsonDocument toBson(final Bson bson) {
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    ((Codec<Bson>) CODEC_REGISTRY.get(bson.getClass())).encode(writer, bson, EncoderContext.builder().build());
    return writer.getDocument();
}
Also used : Codec(org.bson.codecs.Codec) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument)

Example 32 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project mongo-java-driver by mongodb.

the class Updates method pullByFilter.

/**
 * Creates an update that removes from an array all elements that match the given filter.
 *
 * @param filter the query filter
 * @return the update
 * @mongodb.driver.manual reference/operator/update/pull/ $pull
 */
public static Bson pullByFilter(final Bson filter) {
    return new Bson() {

        @Override
        public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> tDocumentClass, final CodecRegistry codecRegistry) {
            BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
            writer.writeStartDocument();
            writer.writeName("$pull");
            encodeValue(writer, filter, codecRegistry);
            writer.writeEndDocument();
            return writer.getDocument();
        }
    };
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Bson(org.bson.conversions.Bson)

Example 33 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project drill by apache.

the class TestBsonRecordReader method testRecursiveDocuments.

@Test
public void testRecursiveDocuments() throws IOException {
    BsonDocument topDoc = new BsonDocument();
    final int count = 3;
    for (int i = 0; i < count; ++i) {
        BsonDocument bsonDoc = new BsonDocument();
        BsonWriter bw = new BsonDocumentWriter(bsonDoc);
        bw.writeStartDocument();
        bw.writeName("k1" + i);
        bw.writeString("drillMongo1" + i);
        bw.writeName("k2" + i);
        bw.writeString("drillMongo2" + i);
        bw.writeEndDocument();
        bw.flush();
        topDoc.append("doc" + i, bsonDoc);
    }
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(topDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    for (int i = 0; i < count; ++i) {
        SingleMapReaderImpl reader = (SingleMapReaderImpl) mapReader.reader("doc" + i);
        assertEquals("drillMongo1" + i, reader.reader("k1" + i).readText().toString());
        assertEquals("drillMongo2" + i, reader.reader("k2" + i).readText().toString());
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonWriter(org.bson.BsonWriter) BsonDocumentReader(org.bson.BsonDocumentReader) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Example 34 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project drill by apache.

the class TestBsonRecordReader method testArrayType.

@Test
public void testArrayType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    BsonWriter bw = new BsonDocumentWriter(bsonDoc);
    bw.writeStartDocument();
    bw.writeName("arrayKey");
    bw.writeStartArray();
    bw.writeInt32(1);
    bw.writeInt32(2);
    bw.writeInt32(3);
    bw.writeEndArray();
    bw.writeEndDocument();
    bw.flush();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
    FieldReader reader = mapReader.reader("arrayKey");
    assertEquals(3, reader.size());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) SingleMapReaderImpl(org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl) BsonWriter(org.bson.BsonWriter) BsonDocumentReader(org.bson.BsonDocumentReader) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Aggregations

BsonDocument (org.bson.BsonDocument)34 BsonDocumentWriter (org.bson.BsonDocumentWriter)34 Test (org.junit.Test)13 BsonWriter (org.bson.BsonWriter)9 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)6 BsonDocumentReader (org.bson.BsonDocumentReader)6 FieldReader (org.apache.drill.exec.vector.complex.reader.FieldReader)4 BsonValue (org.bson.BsonValue)4 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)4 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)4 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)4 Test (org.junit.jupiter.api.Test)4 BaseTest (org.apache.drill.test.BaseTest)3 BsonInt32 (org.bson.BsonInt32)2 Codec (org.bson.codecs.Codec)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1