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"));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class QueryAcceptanceTest method shouldUseFriendlyQueryType.
@Test
public void shouldUseFriendlyQueryType() {
collection.insertOne(new Document("product", "Book").append("numTimesOrdered", "some"));
collection.insertOne(new Document("product", "CD").append("numTimesOrdered", "6"));
collection.insertOne(new Document("product", "DVD").append("numTimesOrdered", 9));
collection.insertOne(new Document("product", "SomethingElse").append("numTimesOrdered", 10));
collection.insertOne(new Document("product", "VeryPopular").append("numTimesOrdered", 7843273657286478L));
List<Document> results = new ArrayList<Document>();
//TODO make BSON type serializable
Document filter = new Document("$or", asList(new Document("numTimesOrdered", new Document("$type", INT32.getValue())), new Document("numTimesOrdered", new Document("$type", INT64.getValue()))));
collection.find(filter).sort(new Document("numTimesOrdered", -1)).into(results);
assertThat(results.size(), is(3));
assertThat(results.get(0).get("product").toString(), is("VeryPopular"));
assertThat(results.get(1).get("product").toString(), is("SomethingElse"));
assertThat(results.get(2).get("product").toString(), is("DVD"));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class QueryAcceptanceTest method shouldBeAbleToFilterByType.
@Test
public void shouldBeAbleToFilterByType() {
collection.insertOne(new Document("product", "Book").append("numTimesOrdered", "some"));
collection.insertOne(new Document("product", "CD").append("numTimesOrdered", "6"));
collection.insertOne(new Document("product", "DVD").append("numTimesOrdered", 9));
collection.insertOne(new Document("product", "SomethingElse").append("numTimesOrdered", 10));
List<Document> results = new ArrayList<Document>();
collection.find(new Document("numTimesOrdered", new Document("$type", 16))).sort(new Document("numTimesOrdered", -1)).into(results);
assertThat(results.size(), is(2));
assertThat(results.get(0).get("product").toString(), is("SomethingElse"));
assertThat(results.get(1).get("product").toString(), is("DVD"));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class QueryAcceptanceTest method shouldBeAbleToSortAscending.
@Test
public void shouldBeAbleToSortAscending() {
collection.insertOne(new Document("product", "Book"));
collection.insertOne(new Document("product", "DVD"));
collection.insertOne(new Document("product", "CD"));
List<Document> results = new ArrayList<Document>();
collection.find().sort(new Document("product", 1)).into(results);
assertThat(results.size(), is(3));
assertThat(results.get(0).get("product").toString(), is("Book"));
assertThat(results.get(1).get("product").toString(), is("CD"));
assertThat(results.get(2).get("product").toString(), is("DVD"));
}
Aggregations