Search in sources :

Example 1 with Document

use of org.bson.Document in project zeppelin by apache.

the class MongoNotebookRepo method save.

@Override
public void save(Note note, AuthenticationInfo subject) throws IOException {
    Document doc = noteToDocument(note);
    coll.replaceOne(eq("_id", note.getId()), doc, new UpdateOptions().upsert(true));
}
Also used : Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions)

Example 2 with Document

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

the class GridFSDownloadStreamImpl method checkAndFetchResults.

private void checkAndFetchResults(final int amountRead, final ByteBuffer dst, final SingleResultCallback<Integer> callback) {
    if (currentPosition == fileInfo.getLength() || dst.remaining() == 0) {
        callback.onResult(amountRead, null);
    } else if (hasResultsToProcess()) {
        processResults(amountRead, dst, callback);
    } else if (cursor == null) {
        chunksCollection.find(new Document("files_id", fileInfo.getId()).append("n", new Document("$gte", chunkIndex))).batchSize(batchSize).sort(new Document("n", 1)).batchCursor(new SingleResultCallback<AsyncBatchCursor<Document>>() {

            @Override
            public void onResult(final AsyncBatchCursor<Document> result, final Throwable t) {
                if (t != null) {
                    callback.onResult(null, t);
                } else {
                    cursor = result;
                    checkAndFetchResults(amountRead, dst, callback);
                }
            }
        });
    } else {
        cursor.next(new SingleResultCallback<List<Document>>() {

            @Override
            public void onResult(final List<Document> result, final Throwable t) {
                if (t != null) {
                    callback.onResult(null, t);
                } else if (result == null || result.isEmpty()) {
                    callback.onResult(null, chunkNotFound(chunkIndex));
                } else {
                    resultsQueue.addAll(result);
                    if (batchSize == 1) {
                        discardCursor();
                    }
                    processResults(amountRead, dst, callback);
                }
            }
        });
    }
}
Also used : AsyncBatchCursor(com.mongodb.async.AsyncBatchCursor) List(java.util.List) Document(org.bson.Document)

Example 3 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)

Example 4 with Document

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

the class Fixture method initializeCollection.

public static MongoCollection<Document> initializeCollection(final MongoNamespace namespace) {
    MongoDatabase database = getMongoClient().getDatabase(namespace.getDatabaseName());
    try {
        FutureResultCallback<Document> futureResultCallback = new FutureResultCallback<Document>();
        database.runCommand(new Document("drop", namespace.getCollectionName()), futureResultCallback);
        futureResultCallback.get(60, SECONDS);
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().startsWith("ns not found")) {
            throw e;
        }
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
    return database.getCollection(namespace.getCollectionName());
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) MongoCommandException(com.mongodb.MongoCommandException) Document(org.bson.Document)

Example 5 with Document

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

the class Fixture method dropDatabase.

public static void dropDatabase(final String name) {
    if (name == null) {
        return;
    }
    try {
        FutureResultCallback<Document> futureResultCallback = new FutureResultCallback<Document>();
        getMongoClient().getDatabase(name).runCommand(new Document("dropDatabase", 1), futureResultCallback);
        futureResultCallback.get(60, SECONDS);
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().startsWith("ns not found")) {
            throw e;
        }
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) MongoCommandException(com.mongodb.MongoCommandException) Document(org.bson.Document)

Aggregations

Document (org.bson.Document)2386 Test (org.junit.jupiter.api.Test)900 Test (org.junit.Test)472 ArrayList (java.util.ArrayList)209 BsonDocument (org.bson.BsonDocument)188 List (java.util.List)160 Bson (org.bson.conversions.Bson)133 MongoDatabase (com.mongodb.client.MongoDatabase)119 Update (org.springframework.data.mongodb.core.query.Update)116 ObjectId (org.bson.types.ObjectId)113 Map (java.util.Map)92 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)83 Test (org.testng.annotations.Test)82 HashMap (java.util.HashMap)79 BasicDBObject (com.mongodb.BasicDBObject)75 Query (org.springframework.data.mongodb.core.query.Query)67 MongoCollection (com.mongodb.client.MongoCollection)54 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)50 MongoClient (com.mongodb.MongoClient)48 Date (java.util.Date)47