use of org.bson.Document in project mongo-java-driver by mongodb.
the class QueryAcceptanceTest method shouldBeAbleToQueryTypedCollectionWithDocument.
@Test
public void shouldBeAbleToQueryTypedCollectionWithDocument() {
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new PersonCodecProvider()));
MongoCollection<Person> collection = database.getCollection(getCollectionName(), Person.class).withCodecRegistry(codecRegistry);
collection.insertOne(new Person("Bob"));
MongoCursor<Person> results = collection.find(new Document("name", "Bob")).iterator();
assertThat(results.next().name, is("Bob"));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class FindAndUpdateAcceptanceTest method shouldFindAndReplaceWithDocumentRequiringACustomEncoder.
@Test
public void shouldFindAndReplaceWithDocumentRequiringACustomEncoder() {
Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 7);
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new WorkerCodecProvider()));
MongoCollection<Worker> collection = database.getCollection(getCollectionName(), Worker.class).withCodecRegistry(codecRegistry);
collection.insertOne(pat);
assertThat(collection.count(), is(1L));
Document updateOperation = new Document("$inc", new Document("numberOfJobs", 1));
Worker updatedDocument = collection.findOneAndUpdate(new Document("name", "Pat"), updateOperation, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
assertThat("Worker returned from updateOneAndGet should have the", updatedDocument.getNumberOfJobs(), equalTo(8));
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class FindAndUpdateAcceptanceTest method shouldUpdateDocumentAndReturnNew.
@Test
public void shouldUpdateDocumentAndReturnNew() {
Document documentInserted = new Document(KEY, VALUE_TO_CARE_ABOUT).append("someNumber", 11);
collection.insertOne(documentInserted);
assertThat(collection.count(), is(1L));
Document updateOperation = new Document("$inc", new Document("someNumber", 1));
Document updatedDocument = collection.findOneAndUpdate(new Document(KEY, VALUE_TO_CARE_ABOUT), updateOperation, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
assertThat("Document returned from updateOneAndGet should be the updated document", (Integer) updatedDocument.get("someNumber"), equalTo(12));
}
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());
}
Aggregations