use of org.bson.Document in project mongo-java-driver by mongodb.
the class FindAndUpdateAcceptanceTest method shouldInsertDocumentWhenFilterDoesNotMatchAnyDocumentsAndUpsertSelected.
@Test
public void shouldInsertDocumentWhenFilterDoesNotMatchAnyDocumentsAndUpsertSelected() {
Document originalDocument = new Document(KEY, VALUE_TO_CARE_ABOUT).append("someNumber", 11);
collection.insertOne(originalDocument);
assertThat(collection.count(), is(1L));
String newValueThatDoesNotMatchAnythingInDatabase = "valueThatDoesNotMatch";
Document updateOperation = new Document("$inc", new Document("someNumber", 1));
Document document = collection.findOneAndUpdate(new Document(KEY, newValueThatDoesNotMatchAnythingInDatabase), updateOperation, new FindOneAndUpdateOptions().upsert(true).returnDocument(ReturnDocument.AFTER));
assertThat(collection.count(), is(2L));
assertThat("Document retrieved from updateOneAndGet and upsert true should have the new values", (Integer) document.get("someNumber"), equalTo(1));
assertThat("Document retrieved from updateOneAndGet and upsert true should have the new values", document.get(KEY).toString(), equalTo(newValueThatDoesNotMatchAnythingInDatabase));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class CollectionAcceptanceTest method shouldIterateOverAllDocumentsInCollection.
@Test
public void shouldIterateOverAllDocumentsInCollection() {
initialiseCollectionWithDocuments(10);
List<Document> iteratedDocuments = new ArrayList<Document>();
for (final Document cur : collection.find()) {
iteratedDocuments.add(cur);
}
assertEquals(10, iteratedDocuments.size());
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class CollectionAcceptanceTest method shouldGroupDocumentsWhenUsingAggregate.
@Test
public void shouldGroupDocumentsWhenUsingAggregate() {
insertAggregationTestDocuments();
List<Document> grouped = collection.aggregate(asList(new Document("$sort", new Document("_id", 1)), new Document("$project", new Document("_id", 0).append("tags", 1)), new Document("$unwind", "$tags"), new Document("$group", new Document("_id", "$tags")), new Document("$sort", new Document("_id", 1)))).into(new ArrayList<Document>());
assertEquals(asList(new Document("_id", "CE"), new Document("_id", "SA"), new Document("_id", "driver"), new Document("_id", "kernel")), grouped);
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class CollectionAcceptanceTest method shouldAcceptDocumentsWithAllValidValueTypes.
@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
Document doc = new Document();
doc.append("_id", new ObjectId());
doc.append("bool", true);
doc.append("int", 3);
doc.append("long", 5L);
doc.append("str", "Hello MongoDB");
doc.append("double", 1.1);
doc.append("date", new Date());
doc.append("ts", new BsonTimestamp(5, 1));
doc.append("pattern", new BsonRegularExpression("abc"));
doc.append("minKey", new MinKey());
doc.append("maxKey", new MaxKey());
doc.append("js", new Code("code"));
doc.append("jsWithScope", new CodeWithScope("code", new Document()));
doc.append("null", null);
doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
doc.append("list", Arrays.asList(7, 8, 9));
doc.append("doc list", Arrays.asList(new Document("x", 1), new Document("x", 2)));
collection.insertOne(doc);
Document found = collection.find().first();
assertNotNull(found);
assertEquals(ObjectId.class, found.get("_id").getClass());
assertEquals(Boolean.class, found.get("bool").getClass());
assertEquals(Integer.class, found.get("int").getClass());
assertEquals(Long.class, found.get("long").getClass());
assertEquals(String.class, found.get("str").getClass());
assertEquals(Double.class, found.get("double").getClass());
assertEquals(Date.class, found.get("date").getClass());
assertEquals(BsonTimestamp.class, found.get("ts").getClass());
assertEquals(BsonRegularExpression.class, found.get("pattern").getClass());
assertEquals(MinKey.class, found.get("minKey").getClass());
assertEquals(MaxKey.class, found.get("maxKey").getClass());
assertEquals(Code.class, found.get("js").getClass());
assertEquals(CodeWithScope.class, found.get("jsWithScope").getClass());
assertNull(found.get("null"));
assertEquals(Binary.class, found.get("binary").getClass());
assertTrue(found.get("list") instanceof List);
assertTrue(found.get("doc list") instanceof List);
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class CollectionAcceptanceTest method shouldProjectDocumentsWhenUsingAggregate.
@Test
public void shouldProjectDocumentsWhenUsingAggregate() {
insertAggregationTestDocuments();
List<Document> sorted = collection.aggregate(asList(new Document("$sort", new Document("_id", 1)), new Document("$project", new Document("_id", 0).append("zip", "$_id")))).into(new ArrayList<Document>());
assertEquals(asList(new Document("zip", "01778"), new Document("zip", "10012"), new Document("zip", "94301")), sorted);
}
Aggregations