use of org.bson.Document in project mongo-java-driver by mongodb.
the class MongoCollectionTest method testFindAndUpdateWithGenerics.
@Test
public void testFindAndUpdateWithGenerics() {
CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new ConcreteCodecProvider()));
MongoCollection<Concrete> collection = database.getCollection(getCollectionName()).withDocumentClass(Concrete.class).withCodecRegistry(codecRegistry).withReadPreference(ReadPreference.primary()).withWriteConcern(WriteConcern.ACKNOWLEDGED);
Concrete doc = new Concrete(new ObjectId(), "str", 5, 10L, 4.0, 3290482390480L);
collection.insertOne(doc);
Concrete newDoc = collection.findOneAndUpdate(new Document("i", 5), new Document("$set", new Document("i", 6)));
assertNotNull(newDoc);
assertEquals(doc, newDoc);
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class MongoCollectionTest method testAggregationToACollection.
@Test
public void testAggregationToACollection() {
assumeTrue(serverVersionAtLeast(2, 6));
// given
List<Document> documents = asList(new Document("_id", 1), new Document("_id", 2));
getCollectionHelper().insertDocuments(new DocumentCodec(), documents);
MongoCollection<Document> collection = database.getCollection(getCollectionName());
// when
List<Document> result = collection.aggregate(Collections.singletonList(new Document("$out", "outCollection"))).into(new ArrayList<Document>());
// then
assertEquals(documents, result);
}
use of org.bson.Document in project mongo-java-driver by mongodb.
the class CommandMonitoringTest method setUp.
@Before
public void setUp() {
ServerVersion serverVersion = ClusterFixture.getServerVersion();
if (definition.containsKey("ignore_if_server_version_less_than")) {
assumeFalse(serverVersion.compareTo(getServerVersion("ignore_if_server_version_less_than")) < 0);
}
if (definition.containsKey("ignore_if_server_version_greater_than")) {
assumeFalse(serverVersion.compareTo(getServerVersion("ignore_if_server_version_greater_than")) > 0);
}
if (definition.containsKey("ignore_if_topology_type")) {
BsonArray topologyTypes = definition.getArray("ignore_if_topology_type");
for (BsonValue type : topologyTypes) {
String typeString = type.asString().getValue();
if (typeString.equals("sharded")) {
assumeFalse(isSharded());
} else if (typeString.equals("replica_set")) {
assumeFalse(isDiscoverableReplicaSet());
} else if (typeString.equals("standalone")) {
assumeFalse(isSharded());
}
}
}
List<BsonDocument> documents = new ArrayList<BsonDocument>();
for (BsonValue document : data) {
documents.add(document.asDocument());
}
CollectionHelper<Document> collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
collectionHelper.drop();
collectionHelper.insertDocuments(documents);
commandListener.reset();
collection = mongoClient.getDatabase(databaseName).getCollection(collectionName, BsonDocument.class);
if (definition.getDocument("operation").containsKey("read_preference")) {
collection = collection.withReadPreference(ReadPreference.valueOf(definition.getDocument("operation").getDocument("read_preference").getString("mode").getValue()));
}
helper = new JsonPoweredCrudTestHelper(description, collection);
}
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));
}
Aggregations