ClientSession

public final class ClientSession

A MongoDB client session. This class represents a logical session used for ordering sequential operations.

To create a client session, use startSession or withSession on a MongoClient.

If causalConsistency is not set to false when starting a session, read and write operations that use the session will be provided causal consistency guarantees depending on the read and write concerns used. Using majority read and write preferences will provide the full set of guarantees. See https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#sessions for more details.

e.g.

   let opts = CollectionOptions(readConcern: ReadConcern(.majority), writeConcern: try WriteConcern(w: .majority))
   let collection = database.collection("mycoll", options: opts)
   try client.withSession { session in
       try collection.insertOne(["x": 1], session: session)
       try collection.find(["x": 1], session: session)
   }

To disable causal consistency, set causalConsistency to false in the ClientSessionOptions passed in to either withSession or startSession.

  • The client used to start this session.

    Declaration

    Swift

    public let client: MongoClient
  • id

    The session ID of this session.

    Declaration

    Swift

    public let id: Document
  • The most recent cluster time seen by this session. If no operations have been executed using this session and advanceClusterTime has not been called, this will be nil.

    Declaration

    Swift

    public var clusterTime: Document? { get }
  • The operation time of the most recent operation performed using this session.

    Declaration

    Swift

    public var operationTime: Timestamp? { get }
  • The options used to start this session.

    Declaration

    Swift

    public let options: ClientSessionOptions?
  • Advances the clusterTime for this session to the given time, if it is greater than the current clusterTime. If the provided clusterTime is less than the current clusterTime, this method has no effect.

    Declaration

    Swift

    public func advanceClusterTime(to clusterTime: Document)

    Parameters

    clusterTime

    The session’s new cluster time, as a Document like ["cluster time": Timestamp(...)]

  • Advances the operationTime for this session to the given time if it is greater than the current operationTime. If the provided operationTime is less than the current operationTime, this method has no effect.

    Declaration

    Swift

    public func advanceOperationTime(to operationTime: Timestamp)

    Parameters

    operationTime

    The session’s new operationTime