use of org.bson.Document in project mongo-java-driver by mongodb.
the class GridFSBucketImpl method getFileByName.
private GridFSFile getFileByName(final String filename, final GridFSDownloadOptions options) {
int revision = options.getRevision();
int skip;
int sort;
if (revision >= 0) {
skip = revision;
sort = 1;
} else {
skip = (-revision) - 1;
sort = -1;
}
GridFSFile fileInfo = find(new Document("filename", filename)).skip(skip).sort(new Document("uploadDate", sort)).first();
if (fileInfo == null) {
throw new MongoGridFSException(format("No file found with the filename: %s and revision: %s", filename, revision));
}
return fileInfo;
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class GridFSBucketImpl method checkCreateIndex.
private void checkCreateIndex() {
if (!checkedIndexes) {
if (filesCollection.withDocumentClass(Document.class).withReadPreference(primary()).find().projection(new Document("_id", 1)).first() == null) {
Document filesIndex = new Document("filename", 1).append("uploadDate", 1);
if (!hasIndex(filesCollection.withReadPreference(primary()), filesIndex)) {
filesCollection.createIndex(filesIndex);
}
Document chunksIndex = new Document("files_id", 1).append("n", 1);
if (!hasIndex(chunksCollection.withReadPreference(primary()), chunksIndex)) {
chunksCollection.createIndex(chunksIndex, new IndexOptions().unique(true));
}
}
checkedIndexes = true;
}
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class GridFSUploadStreamImpl method writeChunk.
private void writeChunk() {
if (bufferOffset > 0) {
chunksCollection.insertOne(new Document("files_id", fileId).append("n", chunkIndex).append("data", getData()));
md5.update(buffer);
chunkIndex++;
bufferOffset = 0;
}
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class MapReduceAcceptanceTest method shouldInsertMapReduceResultsIntoACollectionWhenOutputTypeIsNotInline.
@Test
public void shouldInsertMapReduceResultsIntoACollectionWhenOutputTypeIsNotInline() {
//given
insertLabelData();
//when
// perform Map Reduce on all data
MongoIterable<Document> results = collection.mapReduce(" function(){ " + " for ( var i=0; i < this.labels.length; i++ ){ " + " emit( this.labels[i] , 1 ); " + " }" + "}", " function(key,values){ " + " var sum=0; " + " for( var i=0; i < values.length; i++ ) " + " sum += values[i]; " + " return sum;" + "}").collectionName(getCollectionName() + "-output");
//then
List<Document> resultList = results.into(new ArrayList<Document>());
assertThat("There are four distinct labels, a b c d", resultList.size(), is(4));
assertThat("There are four 'a's in the data", resultList, hasItem(new Document("_id", "a").append("value", 4.0)));
assertThat("There are eight 'b's in the data", resultList, hasItem(new Document("_id", "b").append("value", 8.0)));
assertThat("There are six 'c's in the data", resultList, hasItem(new Document("_id", "c").append("value", 6.0)));
assertThat("There are two 'd's in the data", resultList, hasItem(new Document("_id", "d").append("value", 2.0)));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class QueryAcceptanceTest method shouldBeAbleToQueryWithDocument.
@Test
public void shouldBeAbleToQueryWithDocument() {
collection.insertOne(new Document("name", "Bob"));
Document query = new Document("name", "Bob");
MongoCursor<Document> results = collection.find(query).iterator();
assertThat(results.next().get("name").toString(), is("Bob"));
}
Aggregations