Document.prototype.$session()

Parameters
  • [session] «ClientSession» overwrite the current session
Returns:
  • «ClientSession»

Getter/setter around the session associated with this document. Used to automatically set session if you save() a doc that you got from a query with an associated session.

Example:

  1. const session = MyModel.startSession();
  2. const doc = await MyModel.findOne().session(session);
  3. doc.$session() === session; // true
  4. doc.$session(null);
  5. doc.$session() === null; // true

If this is a top-level document, setting the session propagates to all child docs.