use of org.bson.BsonDocument in project morphia by mongodb.
the class IndexHelper method createIndex.
void createIndex(final MongoCollection collection, final MappedClass mc, final Index index, final boolean background) {
Index normalized = IndexBuilder.normalize(index);
BsonDocument keys = calculateKeys(mc, normalized);
com.mongodb.client.model.IndexOptions indexOptions = convert(normalized.options(), background);
calculateWeights(normalized, indexOptions);
collection.createIndex(keys, indexOptions);
}
use of org.bson.BsonDocument in project morphia by mongodb.
the class IndexHelper method calculateKeys.
BsonDocument calculateKeys(final MappedClass mc, final Index index) {
BsonDocument keys = new BsonDocument();
for (Field field : index.fields()) {
String path;
try {
path = findField(mc, index.options(), new ArrayList<String>(asList(field.value().split("\\."))));
} catch (Exception e) {
path = field.value();
String message = format("The path '%s' can not be validated against '%s' and may represent an invalid index", path, mc.getClazz().getName());
if (!index.options().disableValidation()) {
throw new MappingException(message);
}
LOG.warning(message);
}
keys.putAll(toBsonDocument(path, field.type().toIndexValue()));
}
return keys;
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class GridFSTest method doDelete.
private void doDelete(final BsonDocument arguments, final BsonDocument assertion) {
Throwable error = null;
try {
gridFSBucket.delete(arguments.getObjectId("id").getValue());
} catch (MongoGridFSException e) {
error = e;
}
if (assertion.containsKey("error")) {
assertNotNull("Should have thrown an exception", error);
} else {
assertNull("Should not have thrown an exception", error);
for (BsonValue rawDataItem : assertion.getArray("data")) {
BsonDocument dataItem = rawDataItem.asDocument();
for (BsonValue deletedItem : dataItem.getArray("deletes", new BsonArray())) {
String delete = dataItem.getString("delete", new BsonString("none")).getValue();
BsonObjectId id = new BsonObjectId(new ObjectId());
if (delete.equals("expected.files")) {
id = deletedItem.asDocument().getDocument("q").getObjectId("_id");
} else if (delete.equals("expected.chunks")) {
id = deletedItem.asDocument().getDocument("q").getObjectId("files_id");
}
assertEquals(filesCollection.count(new BsonDocument("_id", id)), 0);
assertEquals(chunksCollection.count(new BsonDocument("files_id", id)), 0);
}
}
}
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class CommandResultTest method shouldNotBeOkWhenOkFieldIsFalse.
@Test
public void shouldNotBeOkWhenOkFieldIsFalse() throws UnknownHostException {
CommandResult commandResult = new CommandResult(new BsonDocument());
commandResult.put("ok", false);
assertFalse(commandResult.ok());
}
use of org.bson.BsonDocument in project mongo-java-driver by mongodb.
the class CommandResultTest method shouldNotHaveExceptionWhenOkIsTrue.
@Test
public void shouldNotHaveExceptionWhenOkIsTrue() throws UnknownHostException {
CommandResult commandResult = new CommandResult(new BsonDocument("ok", new BsonBoolean(true)));
assertNull(commandResult.getException());
}
Aggregations