Search in sources :

Example 11 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 12 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)

Example 13 with Document

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

Example 14 with Document

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

Example 15 with Document

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"));
}
Also used : ArrayList(java.util.ArrayList) 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