use of org.bson.BsonArray 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));
}
use of org.bson.BsonArray 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);
}
}
});
}
};
}
use of org.bson.BsonArray in project mongo-java-driver by mongodb.
the class GridFSTest method arrangeGridFS.
private void arrangeGridFS(final BsonDocument arrange) {
if (arrange.isEmpty()) {
return;
}
for (BsonValue fileToArrange : arrange.getArray("data", new BsonArray())) {
final BsonDocument document = fileToArrange.asDocument();
if (document.containsKey("delete") && document.containsKey("deletes")) {
for (BsonValue toDelete : document.getArray("deletes")) {
final BsonDocument query = toDelete.asDocument().getDocument("q");
int limit = toDelete.asDocument().getInt32("limit").getValue();
final MongoCollection<BsonDocument> collection;
if (document.getString("delete").getValue().equals("fs.files")) {
collection = filesCollection;
} else {
collection = chunksCollection;
}
if (limit == 1) {
new MongoOperation<DeleteResult>() {
@Override
public void execute() {
collection.deleteOne(query, getCallback());
}
}.get();
} else {
new MongoOperation<DeleteResult>() {
@Override
public void execute() {
collection.deleteMany(query, getCallback());
}
}.get();
}
}
} else if (document.containsKey("insert") && document.containsKey("documents")) {
if (document.getString("insert").getValue().equals("fs.files")) {
new MongoOperation<Void>() {
@Override
public void execute() {
filesCollection.insertMany(processFiles(document.getArray("documents"), new ArrayList<BsonDocument>()), getCallback());
}
}.get();
} else {
new MongoOperation<Void>() {
@Override
public void execute() {
chunksCollection.insertMany(processChunks(document.getArray("documents"), new ArrayList<BsonDocument>()), getCallback());
}
}.get();
}
} else if (document.containsKey("update") && document.containsKey("updates")) {
final MongoCollection<BsonDocument> collection;
if (document.getString("update").getValue().equals("fs.files")) {
collection = filesCollection;
} else {
collection = chunksCollection;
}
for (BsonValue rawUpdate : document.getArray("updates")) {
final BsonDocument query = rawUpdate.asDocument().getDocument("q");
final BsonDocument update = rawUpdate.asDocument().getDocument("u");
update.put("$set", parseHexDocument(update.getDocument("$set")));
new MongoOperation<UpdateResult>() {
@Override
public void execute() {
collection.updateMany(query, update, getCallback());
}
}.get();
}
} else {
throw new IllegalArgumentException("Unsupported arrange: " + document);
}
}
}
use of org.bson.BsonArray in project mongo-java-driver by mongodb.
the class ListCollectionsOperation method asQueryDocument.
private BsonDocument asQueryDocument(final ConnectionDescription connectionDescription, final ReadPreference readPreference) {
BsonDocument document = new BsonDocument();
BsonDocument transformedFilter = null;
if (filter != null) {
if (filter.containsKey("name")) {
if (!filter.isString("name")) {
throw new IllegalArgumentException("When filtering collections on MongoDB versions < 3.0 the name field " + "must be a string");
}
transformedFilter = new BsonDocument();
transformedFilter.putAll(filter);
transformedFilter.put("name", new BsonString(format("%s.%s", databaseName, filter.getString("name").getValue())));
} else {
transformedFilter = filter;
}
}
BsonDocument indexExcludingRegex = new BsonDocument("name", new BsonRegularExpression("^[^$]*$"));
BsonDocument query = transformedFilter == null ? indexExcludingRegex : new BsonDocument("$and", new BsonArray(asList(indexExcludingRegex, transformedFilter)));
document.put("$query", query);
if (connectionDescription.getServerType() == SHARD_ROUTER && !readPreference.equals(primary())) {
document.put("$readPreference", readPreference.toDocument());
}
if (maxTimeMS > 0) {
document.put("$maxTimeMS", new BsonInt64(maxTimeMS));
}
return document;
}
use of org.bson.BsonArray in project mongo-java-driver by mongodb.
the class UpdateProtocol method getAsWriteCommand.
@Override
protected BsonDocument getAsWriteCommand(final ByteBufferBsonOutput bsonOutput, final int firstDocumentPosition) {
List<ByteBufBsonDocument> documents = ByteBufBsonDocument.create(bsonOutput, firstDocumentPosition);
BsonDocument updateDocument = new BsonDocument("q", documents.get(0)).append("u", documents.get(1));
if (updates.get(0).isMulti()) {
updateDocument.append("multi", BsonBoolean.TRUE);
}
if (updates.get(0).isUpsert()) {
updateDocument.append("upsert", BsonBoolean.TRUE);
}
return getBaseCommandDocument("update").append("updates", new BsonArray(singletonList(updateDocument)));
}
Aggregations