Search in sources :

Example 16 with ClientSession

use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.

the class SessionBoundMongoTemplateUnitTests method geoNearShouldUseProxiedDatabase.

// DATAMONGO-1880, DATAMONGO-2264
@Test
public void geoNearShouldUseProxiedDatabase() {
    when(database.runCommand(any(ClientSession.class), any(), eq(Document.class))).thenReturn(new Document("results", Collections.emptyList()));
    template.geoNear(NearQuery.near(new Point(0, 0), Metrics.NEUTRAL), Person.class);
    verify(collection).aggregate(eq(clientSession), anyList(), eq(Document.class));
}
Also used : ClientSession(com.mongodb.client.ClientSession) Point(org.springframework.data.geo.Point) Document(org.bson.Document) Test(org.junit.Test)

Example 17 with ClientSession

use of com.mongodb.client.ClientSession in project spring-data-mongodb by spring-projects.

the class SessionBoundMongoTemplateUnitTests method groupShouldUseProxiedDatabase.

// DATAMONGO-1880
@Test
public void groupShouldUseProxiedDatabase() {
    when(database.runCommand(any(ClientSession.class), any(), eq(Document.class))).thenReturn(new Document("retval", Collections.emptyList()));
    template.group(COLLECTION_NAME, GroupBy.key("firstName"), Person.class);
    verify(database).runCommand(eq(clientSession), any(), eq(Document.class));
}
Also used : ClientSession(com.mongodb.client.ClientSession) Document(org.bson.Document) Test(org.junit.Test)

Example 18 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class CausalConsistencyExamples method main.

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes an optional single argument for the connection string
 */
public static void main(final String[] args) {
    setupDatabase();
    MongoClient client = MongoClients.create();
    // Start Causal Consistency Example 1
    // Example 1: Use a causally consistent session to ensure that the update occurs before the insert.
    ClientSession session1 = client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
    Date currentDate = new Date();
    MongoCollection<Document> items = client.getDatabase("test").withReadConcern(ReadConcern.MAJORITY).withWriteConcern(WriteConcern.MAJORITY.withWTimeout(1000, TimeUnit.MILLISECONDS)).getCollection("test");
    items.updateOne(session1, eq("sku", "111"), set("end", currentDate));
    Document document = new Document("sku", "nuts-111").append("name", "Pecans").append("start", currentDate);
    items.insertOne(session1, document);
    // End Causal Consistency Example 1
    // Start Causal Consistency Example 2
    // Example 2: Advance the cluster time and the operation time to that of the other session to ensure that
    // this client is causally consistent with the other session and read after the two writes.
    ClientSession session2 = client.startSession(ClientSessionOptions.builder().causallyConsistent(true).build());
    session2.advanceClusterTime(session1.getClusterTime());
    session2.advanceOperationTime(session1.getOperationTime());
    items = client.getDatabase("test").withReadPreference(ReadPreference.secondary()).withReadConcern(ReadConcern.MAJORITY).withWriteConcern(WriteConcern.MAJORITY.withWTimeout(1000, TimeUnit.MILLISECONDS)).getCollection("items");
    for (Document item : items.find(session2, eq("end", BsonNull.VALUE))) {
        System.out.println(item);
    }
// End Causal Consistency Example 2
}
Also used : MongoClient(com.mongodb.client.MongoClient) ClientSession(com.mongodb.client.ClientSession) Document(org.bson.Document) Date(java.util.Date)

Example 19 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class TransactionExample method updateEmployeeInfo.

private void updateEmployeeInfo() {
    MongoCollection<Document> employeesCollection = client.getDatabase("hr").getCollection("employees");
    MongoCollection<Document> eventsCollection = client.getDatabase("reporting").getCollection("events");
    TransactionOptions txnOptions = TransactionOptions.builder().readPreference(ReadPreference.primary()).readConcern(ReadConcern.MAJORITY).writeConcern(WriteConcern.MAJORITY).build();
    try (ClientSession clientSession = client.startSession()) {
        clientSession.startTransaction(txnOptions);
        employeesCollection.updateOne(clientSession, Filters.eq("employee", 3), Updates.set("status", "Inactive"));
        eventsCollection.insertOne(clientSession, new Document("employee", 3).append("status", new Document("new", "Inactive").append("old", "Active")));
        commitWithRetry(clientSession);
    }
}
Also used : TransactionOptions(com.mongodb.TransactionOptions) ClientSession(com.mongodb.client.ClientSession) Document(org.bson.Document)

Example 20 with ClientSession

use of com.mongodb.client.ClientSession in project mongo-java-driver by mongodb.

the class UnifiedTest method executeEndSession.

private OperationResult executeEndSession(final BsonDocument operation) {
    ClientSession session = entities.getSession(operation.getString("object").getValue());
    session.close();
    return OperationResult.NONE;
}
Also used : ClientSession(com.mongodb.client.ClientSession)

Aggregations

ClientSession (com.mongodb.client.ClientSession)52 BsonDocument (org.bson.BsonDocument)19 BsonValue (org.bson.BsonValue)17 BsonString (org.bson.BsonString)16 Map (java.util.Map)15 Document (org.bson.Document)12 Test (org.junit.jupiter.api.Test)10 BsonArray (org.bson.BsonArray)6 SessionBoundMongoTemplate (org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate)4 TransactionOptions (com.mongodb.TransactionOptions)3 MongoClient (com.mongodb.client.MongoClient)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)3 Test (org.junit.Test)3 Point (org.springframework.data.geo.Point)3 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)3 DeleteOptions (com.mongodb.client.model.DeleteOptions)2 FindOneAndDeleteOptions (com.mongodb.client.model.FindOneAndDeleteOptions)2 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 NonNull (com.mongodb.lang.NonNull)2