use of org.bson.BsonDocument 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.BsonDocument in project mongo-java-driver by mongodb.
the class GridFSTest method processFiles.
private List<BsonDocument> processFiles(final BsonArray bsonArray, final List<BsonDocument> documents) {
for (BsonValue rawDocument : bsonArray.getValues()) {
if (rawDocument.isDocument()) {
BsonDocument document = rawDocument.asDocument();
if (document.get("length").isInt32()) {
document.put("length", new BsonInt64(document.getInt32("length").getValue()));
}
if (document.containsKey("metadata") && document.getDocument("metadata").isEmpty()) {
document.remove("metadata");
}
if (document.containsKey("aliases") && document.getArray("aliases").getValues().size() == 0) {
document.remove("aliases");
}
if (document.containsKey("contentType") && document.getString("contentType").getValue().length() == 0) {
document.remove("contentType");
}
documents.add(document);
}
}
return documents;
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class QueryProtocol method sendQuerySucceededEvent.
private void sendQuerySucceededEvent(final ConnectionDescription connectionDescription, final long startTimeNanos, final QueryMessage message, final boolean isExplainEvent, final ResponseBuffers responseBuffers, final QueryResult<T> queryResult) {
if (commandListener != null) {
BsonDocument response = asFindCommandResponseDocument(responseBuffers, queryResult, isExplainEvent);
sendCommandSucceededEvent(message, getCommandName(isExplainEvent), response, connectionDescription, startTimeNanos, commandListener);
}
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class Projections method combine.
private static Bson combine(final List<String> fieldNames, final BsonValue value) {
BsonDocument document = new BsonDocument();
for (String fieldName : fieldNames) {
document.remove(fieldName);
document.append(fieldName, value);
}
return document;
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class SimpleExpression method toBsonDocument.
@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
writer.writeStartDocument();
writer.writeName(name);
BuildersHelper.encodeValue(writer, expression, codecRegistry);
writer.writeEndDocument();
return writer.getDocument();
}
Aggregations