use of org.bson.BsonValue 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.BsonValue 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.BsonValue 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();
}
use of org.bson.BsonValue in project mongo-java-driver by mongodb.
the class ConnectionStringTest method assertExpectedHosts.
private void assertExpectedHosts(final List<String> hosts) {
List<String> cleanedHosts = new ArrayList<String>();
for (String host : hosts) {
if (host.startsWith("[")) {
int idx = host.indexOf("]");
cleanedHosts.add(host.substring(1, idx) + host.substring(idx + 1));
} else {
cleanedHosts.add(host);
}
}
List<String> expectedHosts = new ArrayList<String>();
for (BsonValue rawHost : definition.getArray("hosts")) {
BsonDocument hostDoc = rawHost.asDocument();
String host = hostDoc.getString("host").getValue();
String port = "";
if (!hostDoc.get("port").isNull()) {
port = ":" + hostDoc.getInt32("port").getValue();
}
expectedHosts.add(host + port);
}
assertEquals(expectedHosts, cleanedHosts);
}
use of org.bson.BsonValue in project mongo-java-driver by mongodb.
the class WriteCommandResultHelper method getUpsertedItems.
@SuppressWarnings("unchecked")
private static List<BulkWriteUpsert> getUpsertedItems(final BsonDocument result) {
BsonValue upsertedValue = result.get("upserted");
if (upsertedValue == null) {
return Collections.emptyList();
} else {
List<BulkWriteUpsert> bulkWriteUpsertList = new ArrayList<BulkWriteUpsert>();
for (BsonValue upsertedItem : (BsonArray) upsertedValue) {
BsonDocument upsertedItemDocument = (BsonDocument) upsertedItem;
bulkWriteUpsertList.add(new BulkWriteUpsert(upsertedItemDocument.getNumber("index").intValue(), upsertedItemDocument.get("_id")));
}
return bulkWriteUpsertList;
}
}
Aggregations