Search in sources :

Example 21 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project spring-data-mongodb by spring-projects.

the class ParameterBindingDocumentCodec method getDocumentId.

@Override
public BsonValue getDocumentId(final Document document) {
    if (!documentHasId(document)) {
        throw new IllegalStateException("The document does not contain an _id");
    }
    Object id = document.get(ID_FIELD_NAME);
    if (id instanceof BsonValue) {
        return (BsonValue) id;
    }
    BsonDocument idHoldingDocument = new BsonDocument();
    BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
    writer.writeStartDocument();
    writer.writeName(ID_FIELD_NAME);
    writeValue(writer, EncoderContext.builder().build(), id);
    writer.writeEndDocument();
    return idHoldingDocument.get(ID_FIELD_NAME);
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonWriter(org.bson.BsonWriter) BsonValue(org.bson.BsonValue)

Example 22 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project morphia by mongodb.

the class IndexHelper method toBsonDocument.

@SuppressWarnings("unchecked")
private BsonDocument toBsonDocument(final String key, final Object value) {
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    writer.writeStartDocument();
    writer.writeName(key);
    ((Encoder) database.getCodecRegistry().get(value.getClass())).encode(writer, value, ENCODER_CONTEXT);
    writer.writeEndDocument();
    return writer.getDocument();
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) Encoder(org.bson.codecs.Encoder)

Example 23 with BsonDocumentWriter

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

the class CommandMonitoringTest method getWritableCloneOfCommand.

private BsonDocument getWritableCloneOfCommand(final BsonDocument original) {
    BsonDocument clone = new BsonDocument();
    BsonDocumentWriter writer = new BsonDocumentWriter(clone);
    new BsonDocumentCodec(CODEC_REGISTRY_HACK).encode(writer, original, EncoderContext.builder().build());
    return clone;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 24 with BsonDocumentWriter

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

the class TestBsonRecordReader method testArrayOfDocumentType.

@Test
public void testArrayOfDocumentType() throws IOException {
    BsonDocument bsonDoc = new BsonDocument();
    BsonWriter bw = new BsonDocumentWriter(bsonDoc);
    bw.writeStartDocument();
    bw.writeName("a");
    bw.writeString("MongoDB");
    bw.writeName("b");
    bw.writeStartArray();
    bw.writeStartDocument();
    bw.writeName("c");
    bw.writeInt32(1);
    bw.writeEndDocument();
    bw.writeEndArray();
    bw.writeEndDocument();
    bw.flush();
    writer.reset();
    bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
    FieldReader reader = writer.getMapVector().getReader();
    SingleMapReaderImpl mapReader = (SingleMapReaderImpl) reader;
    FieldReader reader3 = mapReader.reader("b");
    assertEquals("MongoDB", mapReader.reader("a").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) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader) Test(org.junit.Test)

Example 25 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project immutables by immutables.

the class BsonGeneratorTest method checkClosed.

@Test
void checkClosed() throws IOException {
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    BsonGenerator generator = generatorFor(writer);
    check(!generator.isClosed());
    generator.close();
    check(generator.isClosed());
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.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