Search in sources :

Example 66 with Document

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;
}
Also used : GridFSFile(com.mongodb.client.gridfs.model.GridFSFile) MongoGridFSException(com.mongodb.MongoGridFSException) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

Example 67 with Document

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;
    }
}
Also used : IndexOptions(com.mongodb.client.model.IndexOptions) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

Example 68 with Document

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

Example 69 with Document

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

Example 70 with Document

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

Aggregations

Document (org.bson.Document)1104 Test (org.junit.Test)795 ArrayList (java.util.ArrayList)74 Update (org.springframework.data.mongodb.core.query.Update)71 List (java.util.List)55 BsonDocument (org.bson.BsonDocument)53 ObjectId (org.bson.types.ObjectId)41 MongoDatabase (com.mongodb.client.MongoDatabase)40 Query (org.springframework.data.mongodb.core.query.Query)40 BasicDBObject (com.mongodb.BasicDBObject)39 MongoClient (com.mongodb.MongoClient)32 Bson (org.bson.conversions.Bson)32 ReturnDocument (com.mongodb.client.model.ReturnDocument)31 DBObject (com.mongodb.DBObject)27 DBRef (com.mongodb.DBRef)25 UnknownHostException (java.net.UnknownHostException)25 HashMap (java.util.HashMap)24 FullDocument (com.mongodb.client.model.changestream.FullDocument)23 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)21 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)21