Search in sources :

Example 91 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class AggregateIterableImpl method execute.

@SuppressWarnings("deprecation")
private MongoIterable<TResult> execute() {
    List<BsonDocument> aggregateList = createBsonDocumentList(pipeline);
    BsonValue outCollection = getOutCollection(aggregateList);
    if (outCollection != null) {
        executor.execute(createAggregateToCollectionOperation(aggregateList));
        FindIterable<TResult> findOperation = new FindIterableImpl<TDocument, TResult>(new MongoNamespace(namespace.getDatabaseName(), outCollection.asString().getValue()), documentClass, resultClass, codecRegistry, readPreference, readConcern, executor, new BsonDocument(), new FindOptions().collation(collation));
        if (batchSize != null) {
            findOperation.batchSize(batchSize);
        }
        return findOperation;
    } else {
        return new OperationIterable<TResult>(new AggregateOperation<TResult>(namespace, aggregateList, codecRegistry.get(resultClass)).maxTime(maxTimeMS, MILLISECONDS).allowDiskUse(allowDiskUse).batchSize(batchSize).useCursor(useCursor).readConcern(readConcern).collation(collation), readPreference, executor);
    }
}
Also used : FindOptions(com.mongodb.client.model.FindOptions) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue)

Example 92 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class DBObjectCodec method getDocumentId.

@Override
public BsonValue getDocumentId(final DBObject document) {
    if (!documentHasId(document)) {
        throw new IllegalStateException("The document does not contain an _id");
    }
    Object id = document.get(ID_FIELD_NAME);
    if (id instanceof BsonValue) {
        return (BsonValue) id;
    }
    BsonDocument idHoldingDocument = new BsonDocument();
    BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
    writer.writeStartDocument();
    writer.writeName(ID_FIELD_NAME);
    writeValue(writer, EncoderContext.builder().build(), id);
    writer.writeEndDocument();
    return idHoldingDocument.get(ID_FIELD_NAME);
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonWriter(org.bson.BsonWriter) BSONObject(org.bson.BSONObject) BsonValue(org.bson.BsonValue)

Example 93 with BsonDocument

use of org.bson.BsonDocument in project morphia by mongodb.

the class IndexHelper method toBsonDocument.

@SuppressWarnings("unchecked")
private BsonDocument toBsonDocument(final String key, final Object value) {
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    writer.writeStartDocument();
    writer.writeName(key);
    ((Encoder) database.getCodecRegistry().get(value.getClass())).encode(writer, value, ENCODER_CONTEXT);
    writer.writeEndDocument();
    return writer.getDocument();
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) Encoder(org.bson.codecs.Encoder)

Example 94 with BsonDocument

use of org.bson.BsonDocument in project morphia by mongodb.

the class IndexHelperTest method calculateKeys.

@Test
public void calculateKeys() {
    MappedClass mappedClass = getMorphia().getMapper().getMappedClass(IndexedClass.class);
    BsonDocument keys = indexHelper.calculateKeys(mappedClass, new IndexBuilder().fields(new FieldBuilder().value("text").type(IndexType.TEXT).weight(1), new FieldBuilder().value("nest").type(IndexType.DESC)));
    assertEquals(new BsonDocument().append("text", new BsonString("text")).append("nest", new BsonInt32(-1)), keys);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MappedClass(org.mongodb.morphia.mapping.MappedClass) Test(org.junit.Test)

Example 95 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class WriteCommandResultHelper method getWriteErrors.

@SuppressWarnings("unchecked")
private static List<BulkWriteError> getWriteErrors(final BsonDocument result) {
    List<BulkWriteError> writeErrors = new ArrayList<BulkWriteError>();
    BsonArray writeErrorsDocuments = (BsonArray) result.get("writeErrors");
    if (writeErrorsDocuments != null) {
        for (BsonValue cur : writeErrorsDocuments) {
            BsonDocument curDocument = (BsonDocument) cur;
            writeErrors.add(new BulkWriteError(curDocument.getNumber("code").intValue(), curDocument.getString("errmsg").getValue(), curDocument.getDocument("errInfo", new BsonDocument()), curDocument.getNumber("index").intValue()));
        }
    }
    return writeErrors;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) BulkWriteError(com.mongodb.bulk.BulkWriteError) BsonValue(org.bson.BsonValue)

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