Search in sources :

Example 36 with Document

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

the class FilterAcceptanceTest method shouldReturnTheExpectedFirstDocument.

@Test
public void shouldReturnTheExpectedFirstDocument() {
    int numberOfDocuments = 6;
    initialiseCollectionWithDocuments(numberOfDocuments);
    Document document = collection.find().skip(3).first();
    assertThat((Integer) document.get("_id"), is(3));
}
Also used : Document(org.bson.Document) Test(org.junit.Test)

Example 37 with Document

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

the class MapReduceAcceptanceTest method shouldMapIterableOfDocumentsIntoAnObjectOfYourChoice.

@Test
public void shouldMapIterableOfDocumentsIntoAnObjectOfYourChoice() {
    //given
    insertLabelData();
    //when
    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;" + "}").filter(//find all IDs greater than 1
    new Document("_id", new Document("$gt", 1))).sort(// sort by ID
    new Document("_id", 1)).limit(// limit to 6 of the remaining results
    6);
    // will perform Map Reduce on _ids 2-7
    //transforms Documents into LabelCount objects
    List<LabelCount> labelCounts = results.map(new Function<Document, LabelCount>() {

        @Override
        public LabelCount apply(final Document document) {
            return new LabelCount(document.getString("_id"), document.getDouble("value"));
        }
    }).into(new ArrayList<LabelCount>());
    //then
    assertThat("Transformed results should still be the same size as original results", labelCounts.size(), is(4));
    Collections.sort(labelCounts);
    assertThat("Should be LabelCount ordered by count ascending", labelCounts, contains(new LabelCount("d", 1), new LabelCount("a", 2), new LabelCount("c", 4), new LabelCount("b", 5)));
}
Also used : Function(com.mongodb.Function) Document(org.bson.Document) Test(org.junit.Test)

Example 38 with Document

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

the class CollectionAcceptanceTest method shouldUnwindDocumentsWhenUsingAggregate.

@Test
public void shouldUnwindDocumentsWhenUsingAggregate() {
    insertAggregationTestDocuments();
    List<Document> unwound = collection.aggregate(asList(new Document("$sort", new Document("_id", 1)), new Document("$project", new Document("_id", 0).append("tags", 1)), new Document("$unwind", "$tags"))).into(new ArrayList<Document>());
    assertEquals(asList(new Document("tags", "driver"), new Document("tags", "driver"), new Document("tags", "SA"), new Document("tags", "CE"), new Document("tags", "kernel"), new Document("tags", "driver"), new Document("tags", "SA"), new Document("tags", "CE")), unwound);
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 39 with Document

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

the class DatabaseAcceptanceTest method shouldListCollectionsFromDatabase.

@Test
public void shouldListCollectionsFromDatabase() {
    database.drop();
    List<Document> collections = database.listCollections().into(new ArrayList<Document>());
    assertThat(collections.size(), is(0));
    for (int i = 0; i < 20; i++) {
        database.createCollection("coll" + i);
    }
    collections = database.listCollections().into(new ArrayList<Document>());
    assertThat(collections.size(), is(greaterThanOrEqualTo(20)));
    collections = database.listCollections().filter(new Document("name", "coll1")).into(new ArrayList<Document>());
    assertThat(collections.size(), is(1));
}
Also used : ArrayList(java.util.ArrayList) Document(org.bson.Document) Test(org.junit.Test)

Example 40 with Document

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

the class InsertAcceptanceTest method shouldInsertSimpleUntypedDocument.

@Test
public void shouldInsertSimpleUntypedDocument() {
    // When
    Document simpleDocument = new Document("name", "Billy");
    collection.insertOne(simpleDocument);
    // Then
    assertThat(collection.count(), is(1L));
    Document insertedDocument = collection.find(new Document("name", "Billy")).first();
    assertThat(insertedDocument.getString("name"), is("Billy"));
}
Also used : Document(org.bson.Document) Test(org.junit.Test)

Aggregations

Document (org.bson.Document)2386 Test (org.junit.jupiter.api.Test)900 Test (org.junit.Test)472 ArrayList (java.util.ArrayList)209 BsonDocument (org.bson.BsonDocument)188 List (java.util.List)160 Bson (org.bson.conversions.Bson)133 MongoDatabase (com.mongodb.client.MongoDatabase)119 Update (org.springframework.data.mongodb.core.query.Update)116 ObjectId (org.bson.types.ObjectId)113 Map (java.util.Map)92 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)83 Test (org.testng.annotations.Test)82 HashMap (java.util.HashMap)79 BasicDBObject (com.mongodb.BasicDBObject)75 Query (org.springframework.data.mongodb.core.query.Query)67 MongoCollection (com.mongodb.client.MongoCollection)54 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)50 MongoClient (com.mongodb.MongoClient)48 Date (java.util.Date)47