Search in sources :

Example 61 with BsonDocument

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);
}
Also used : BsonDocument(org.bson.BsonDocument) Index(org.mongodb.morphia.annotations.Index)

Example 62 with BsonDocument

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;
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) Field(org.mongodb.morphia.annotations.Field) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) MappingException(org.mongodb.morphia.mapping.MappingException) MappingException(org.mongodb.morphia.mapping.MappingException)

Example 63 with BsonDocument

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);
            }
        }
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonString(org.bson.BsonString) MongoGridFSException(com.mongodb.MongoGridFSException) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) BsonObjectId(org.bson.BsonObjectId) BsonValue(org.bson.BsonValue)

Example 64 with BsonDocument

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());
}
Also used : BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 65 with BsonDocument

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());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBoolean(org.bson.BsonBoolean) Test(org.junit.Test)

Aggregations

BsonDocument (org.bson.BsonDocument)169 BsonString (org.bson.BsonString)53 BsonValue (org.bson.BsonValue)37 Test (org.junit.Test)36 BsonArray (org.bson.BsonArray)29 BsonInt32 (org.bson.BsonInt32)28 ArrayList (java.util.ArrayList)24 BsonDocumentReader (org.bson.BsonDocumentReader)17 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)14 BsonDocumentWriter (org.bson.BsonDocumentWriter)14 BsonInt64 (org.bson.BsonInt64)14 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 BsonDouble (org.bson.BsonDouble)8 Document (org.bson.Document)7 MongoNamespace (com.mongodb.MongoNamespace)6 Before (org.junit.Before)6 MongoGridFSException (com.mongodb.MongoGridFSException)5 BsonObjectId (org.bson.BsonObjectId)5 BsonWriter (org.bson.BsonWriter)5 ObjectId (org.bson.types.ObjectId)5