Search in sources :

Example 56 with Document

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

the class DocumentCodecTest method testIterableEncoding.

@Test
public void testIterableEncoding() throws IOException {
    DocumentCodec documentCodec = new DocumentCodec();
    Document doc = new Document().append("list", asList(1, 2, 3, 4, 5)).append("set", new HashSet<Integer>(asList(1, 2, 3, 4)));
    documentCodec.encode(writer, doc, EncoderContext.builder().build());
    BsonInput bsonInput = createInputBuffer();
    Document decodedDocument = documentCodec.decode(new BsonBinaryReader(bsonInput), DecoderContext.builder().build());
    assertEquals(new Document().append("list", asList(1, 2, 3, 4, 5)).append("set", asList(1, 2, 3, 4)), decodedDocument);
}
Also used : BsonInput(org.bson.io.BsonInput) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) BsonBinaryReader(org.bson.BsonBinaryReader) Document(org.bson.Document) Test(org.junit.Test)

Example 57 with Document

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

the class DocumentCodec method decode.

@Override
public Document decode(final BsonReader reader, final DecoderContext decoderContext) {
    Document document = new Document();
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String fieldName = reader.readName();
        document.put(fieldName, readValue(reader, decoderContext));
    }
    reader.readEndDocument();
    return document;
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

Example 58 with Document

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

the class GridFSFileCodec method encode.

@Override
@SuppressWarnings("deprecation")
public void encode(final BsonWriter writer, final GridFSFile value, final EncoderContext encoderContext) {
    BsonDocument bsonDocument = new BsonDocument();
    bsonDocument.put("_id", value.getId());
    bsonDocument.put("filename", new BsonString(value.getFilename()));
    bsonDocument.put("length", new BsonInt64(value.getLength()));
    bsonDocument.put("chunkSize", new BsonInt32(value.getChunkSize()));
    bsonDocument.put("uploadDate", new BsonDateTime(value.getUploadDate().getTime()));
    bsonDocument.put("md5", new BsonString(value.getMD5()));
    Document metadata = value.getMetadata();
    if (metadata != null) {
        bsonDocument.put("metadata", new BsonDocumentWrapper<Document>(metadata, documentCodec));
    }
    Document extraElements = value.getExtraElements();
    if (extraElements != null) {
        bsonDocument.putAll(new BsonDocumentWrapper<Document>(extraElements, documentCodec));
    }
    bsonDocumentCodec.encode(writer, bsonDocument, encoderContext);
}
Also used : BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonDateTime(org.bson.BsonDateTime) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

Example 59 with Document

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

the class GridFSFileCodec method decode.

@Override
public GridFSFile decode(final BsonReader reader, final DecoderContext decoderContext) {
    BsonDocument bsonDocument = bsonDocumentCodec.decode(reader, decoderContext);
    BsonValue id = bsonDocument.get("_id");
    String filename = bsonDocument.getString("filename").getValue();
    long length = bsonDocument.getNumber("length").longValue();
    int chunkSize = bsonDocument.getNumber("chunkSize").intValue();
    Date uploadDate = new Date(bsonDocument.getDateTime("uploadDate").getValue());
    String md5 = bsonDocument.getString("md5").getValue();
    BsonDocument metadataBsonDocument = bsonDocument.getDocument("metadata", new BsonDocument());
    Document optionalMetadata = asDocumentOrNull(metadataBsonDocument);
    for (String key : VALID_FIELDS) {
        bsonDocument.remove(key);
    }
    Document deprecatedExtraElements = asDocumentOrNull(bsonDocument);
    return new GridFSFile(id, filename, length, chunkSize, uploadDate, md5, optionalMetadata, deprecatedExtraElements);
}
Also used : BsonDocument(org.bson.BsonDocument) GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Date(java.util.Date) BsonValue(org.bson.BsonValue)

Example 60 with Document

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

the class GridFSUploadStreamImpl method writeChunk.

private void writeChunk(final SingleResultCallback<Void> callback) {
    if (md5 == null) {
        callback.onResult(null, new MongoGridFSException("No MD5 message digest available, cannot upload file"));
    } else if (bufferOffset > 0) {
        chunksCollection.insertOne(new Document("files_id", fileId).append("n", chunkIndex).append("data", getData()), new SingleResultCallback<Void>() {

            @Override
            public void onResult(final Void result, final Throwable t) {
                if (t != null) {
                    callback.onResult(null, t);
                } else {
                    md5.update(buffer);
                    chunkIndex++;
                    bufferOffset = 0;
                    callback.onResult(null, null);
                }
            }
        });
    } else {
        callback.onResult(null, null);
    }
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) SingleResultCallback(com.mongodb.async.SingleResultCallback) Document(org.bson.Document)

Aggregations

Document (org.bson.Document)1104 Test (org.junit.Test)795 ArrayList (java.util.ArrayList)74 Update (org.springframework.data.mongodb.core.query.Update)71 List (java.util.List)55 BsonDocument (org.bson.BsonDocument)53 ObjectId (org.bson.types.ObjectId)41 MongoDatabase (com.mongodb.client.MongoDatabase)40 Query (org.springframework.data.mongodb.core.query.Query)40 BasicDBObject (com.mongodb.BasicDBObject)39 MongoClient (com.mongodb.MongoClient)32 Bson (org.bson.conversions.Bson)32 ReturnDocument (com.mongodb.client.model.ReturnDocument)31 DBObject (com.mongodb.DBObject)27 DBRef (com.mongodb.DBRef)25 UnknownHostException (java.net.UnknownHostException)25 HashMap (java.util.HashMap)24 FullDocument (com.mongodb.client.model.changestream.FullDocument)23 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)21 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)21