Search in sources :

Example 1 with BsonValue

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

the class DocumentCodec 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 2 with BsonValue

use of org.bson.BsonValue 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 3 with BsonValue

use of org.bson.BsonValue 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;
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) BsonValue(org.bson.BsonValue)

Example 4 with BsonValue

use of org.bson.BsonValue 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();
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) List(java.util.List) MongoNamespace(com.mongodb.MongoNamespace) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 5 with BsonValue

use of org.bson.BsonValue 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);
                    }
                }
            });
        }
    };
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) BsonValue(org.bson.BsonValue)

Aggregations

BsonValue (org.bson.BsonValue)45 BsonDocument (org.bson.BsonDocument)37 ArrayList (java.util.ArrayList)17 BsonArray (org.bson.BsonArray)15 BsonString (org.bson.BsonString)14 BsonInt32 (org.bson.BsonInt32)6 BsonObjectId (org.bson.BsonObjectId)4 Test (org.junit.Test)4 MongoNamespace (com.mongodb.MongoNamespace)3 AggregateToCollectionOperation (com.mongodb.operation.AggregateToCollectionOperation)3 List (java.util.List)3 BsonInt64 (org.bson.BsonInt64)3 BsonNumber (org.bson.BsonNumber)3 ObjectId (org.bson.types.ObjectId)3 Before (org.junit.Before)3 ConnectionString (com.mongodb.ConnectionString)2 MongoGridFSException (com.mongodb.MongoGridFSException)2 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)2 FindOptions (com.mongodb.client.model.FindOptions)2 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)2