Search in sources :

Example 1 with BsonString

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

the class GridFSBucketImpl method rename.

@Override
public void rename(final BsonValue id, final String newFilename, final SingleResultCallback<Void> callback) {
    notNull("id", id);
    notNull("newFilename", newFilename);
    notNull("callback", callback);
    final SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
    filesCollection.updateOne(new BsonDocument("_id", id), new BsonDocument("$set", new BsonDocument("filename", new BsonString(newFilename))), new SingleResultCallback<UpdateResult>() {

        @Override
        public void onResult(final UpdateResult result, final Throwable t) {
            if (t != null) {
                errHandlingCallback.onResult(null, t);
            } else if (result.wasAcknowledged() && result.getMatchedCount() == 0) {
                errHandlingCallback.onResult(null, new MongoGridFSException(format("No file found with the ObjectId: %s", id)));
            } else {
                errHandlingCallback.onResult(null, null);
            }
        }
    });
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MongoGridFSException(com.mongodb.MongoGridFSException) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 2 with BsonString

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

the class ClientMetadataHelper method listToBsonString.

static BsonString listToBsonString(final List<String> listOfStrings) {
    StringBuilder stringBuilder = new StringBuilder();
    int i = 0;
    for (String val : listOfStrings) {
        if (i > 0) {
            stringBuilder.append(SEPARATOR);
        }
        stringBuilder.append(val);
        i++;
    }
    return new BsonString(stringBuilder.toString());
}
Also used : BsonString(org.bson.BsonString) BsonString(org.bson.BsonString)

Example 3 with BsonString

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

the class GetMoreProtocol method asGetMoreCommandResponseDocument.

private BsonDocument asGetMoreCommandResponseDocument(final QueryResult<T> queryResult, final ResponseBuffers responseBuffers) {
    List<ByteBufBsonDocument> rawResultDocuments = Collections.emptyList();
    if (responseBuffers.getReplyHeader().getNumberReturned() != 0) {
        responseBuffers.getBodyByteBuffer().position(0);
        rawResultDocuments = ByteBufBsonDocument.create(responseBuffers);
    }
    BsonDocument cursorDocument = new BsonDocument("id", queryResult.getCursor() == null ? new BsonInt64(0) : new BsonInt64(queryResult.getCursor().getId())).append("ns", new BsonString(namespace.getFullName())).append("nextBatch", new BsonArray(rawResultDocuments));
    return new BsonDocument("cursor", cursorDocument).append("ok", new BsonDouble(1));
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble)

Example 4 with BsonString

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

the class IndexHelper method generateIndexName.

/**
     * Convenience method to generate an index name from the set of fields it is over.
     *
     * @return a string representation of this index's fields
     */
static String generateIndexName(final BsonDocument index) {
    StringBuilder indexName = new StringBuilder();
    for (final String keyNames : index.keySet()) {
        if (indexName.length() != 0) {
            indexName.append('_');
        }
        indexName.append(keyNames).append('_');
        BsonValue ascOrDescValue = index.get(keyNames);
        if (ascOrDescValue instanceof BsonNumber) {
            indexName.append(((BsonNumber) ascOrDescValue).intValue());
        } else if (ascOrDescValue instanceof BsonString) {
            indexName.append(((BsonString) ascOrDescValue).getValue().replace(' ', '_'));
        }
    }
    return indexName.toString();
}
Also used : BsonNumber(org.bson.BsonNumber) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Example 5 with BsonString

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

the class ListCollectionsOperation method projectFromFullNamespaceToCollectionName.

private List<T> projectFromFullNamespaceToCollectionName(final List<BsonDocument> unstripped) {
    if (unstripped == null) {
        return null;
    }
    List<T> stripped = new ArrayList<T>(unstripped.size());
    String prefix = databaseName + ".";
    for (BsonDocument cur : unstripped) {
        String name = cur.getString("name").getValue();
        String collectionName = name.substring(prefix.length());
        cur.put("name", new BsonString(collectionName));
        stripped.add(decoder.decode(new BsonDocumentReader(cur), DecoderContext.builder().build()));
    }
    return stripped;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ArrayList(java.util.ArrayList) BsonDocumentReader(org.bson.BsonDocumentReader) BsonString(org.bson.BsonString)

Aggregations

BsonString (org.bson.BsonString)178 BsonDocument (org.bson.BsonDocument)153 BsonInt32 (org.bson.BsonInt32)55 BsonArray (org.bson.BsonArray)48 Test (org.junit.Test)36 Document (org.bson.Document)32 BsonValue (org.bson.BsonValue)31 BsonInt64 (org.bson.BsonInt64)28 ArrayList (java.util.ArrayList)23 Test (org.junit.jupiter.api.Test)18 Map (java.util.Map)14 BsonDouble (org.bson.BsonDouble)14 MongoClientSettings (com.mongodb.MongoClientSettings)13 BsonBinary (org.bson.BsonBinary)13 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)12 HashMap (java.util.HashMap)12 MongoNamespace (com.mongodb.MongoNamespace)11 List (java.util.List)10 Before (org.junit.Before)10 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)9