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