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);
}
}
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);
}
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();
}
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);
}
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;
}
Aggregations