use of org.bson.BsonDocument 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);
}
use of org.bson.BsonDocument 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);
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class CrudTest method getAggregateMongoOperation.
private AggregateIterable<BsonDocument> getAggregateMongoOperation(final BsonDocument arguments) {
List<BsonDocument> pipeline = new ArrayList<BsonDocument>();
for (BsonValue stage : arguments.getArray("pipeline")) {
pipeline.add(stage.asDocument());
}
AggregateIterable<BsonDocument> iterable = collection.aggregate(pipeline);
if (arguments.containsKey("batchSize")) {
iterable.batchSize(arguments.getNumber("batchSize").intValue());
}
if (arguments.containsKey("collation")) {
iterable.collation(getCollation(arguments.getDocument("collation")));
}
return iterable;
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class CrudTest method setUp.
@Before
@Override
public void setUp() {
super.setUp();
collection = Fixture.initializeCollection(new MongoNamespace(getDefaultDatabaseName(), getClass().getName())).withDocumentClass(BsonDocument.class);
new MongoOperation<Void>() {
@Override
public void execute() {
List<BsonDocument> documents = new ArrayList<BsonDocument>();
for (BsonValue document : data) {
documents.add(document.asDocument());
}
collection.insertMany(documents, getCallback());
}
}.get();
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class CrudTest method getInsertManyMongoOperation.
private MongoOperationInsertManyResult getInsertManyMongoOperation(final BsonDocument arguments) {
return new MongoOperationInsertManyResult() {
@Override
public void execute() {
final List<BsonDocument> documents = new ArrayList<BsonDocument>();
for (BsonValue document : arguments.getArray("documents")) {
documents.add(document.asDocument());
}
collection.insertMany(documents, new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
if (t != null) {
getCallback().onResult(null, t);
} else {
BsonArray insertedIds = new BsonArray();
for (BsonDocument document : documents) {
insertedIds.add(document.get("_id"));
}
getCallback().onResult(new InsertManyResult(insertedIds), null);
}
}
});
}
};
}
Aggregations