Class MongoDbPersistence<T>

java.lang.Object
org.pipservices3.mongodb.persistence.MongoDbPersistence<T>
All Implemented Interfaces:
org.pipservices3.commons.config.IConfigurable, org.pipservices3.commons.config.IReconfigurable, org.pipservices3.commons.refer.IReferenceable, org.pipservices3.commons.run.ICleanable, org.pipservices3.commons.run.IClosable, org.pipservices3.commons.run.IOpenable
Direct Known Subclasses:
IdentifiableMongoDbPersistence

public class MongoDbPersistence<T>
extends Object
implements org.pipservices3.commons.refer.IReferenceable, org.pipservices3.commons.config.IReconfigurable, org.pipservices3.commons.run.IOpenable, org.pipservices3.commons.run.ICleanable
Abstract persistence component that stores data in MongoDB.

This is the most basic persistence component that is only able to store data items of any type. Specific CRUD operations over the data items must be implemented in child classes by accessing this._collection or this._model properties.

### Configuration parameters ###

  • collection: (optional) MongoDB collection name
  • connection(s):
    • discovery_key: (optional) a key to retrieve the connection from IDiscovery
    • host: host name or IP address
    • port: port number (default: 27017)
    • uri: resource URI or connection string with all parameters in it
  • credential(s):
    • store_key: (optional) a key to retrieve the credentials from ICredentialStore
    • username: (optional) user name
    • password: (optional) user password
  • options:
    • max_pool_size: (optional) maximum connection pool size (default: 2)
    • keep_alive: (optional) enable connection keep alive (default: true)
    • connect_timeout: (optional) connection timeout in milliseconds (default: 5 sec)
    • auto_reconnect: (optional) enable auto reconnection (default: true)
    • max_page_size: (optional) maximum page size (default: 100)
    • debug: (optional) enable debug output (default: false).

### References ###

  • *:logger:*:*:1.0 (optional) ILogger components to pass log messages
  • *:discovery:*:*:1.0 (optional) IDiscovery services
  • *:credential-store:*:*:1.0 (optional) Credential stores to resolve credentials

### Example ###

 
 class MyMongoDbPersistence extends MongoDbPersistence<MyData> {
    
   public MyMongoDbPersistence() {
       super("mydata", MyData.class);
   }
 
   public MyData getByName(String correlationId, String name) {
   	Bson filter = Filters.eq("name", name);
   	MyData item = _collection.find(filter).first();
   	return item;
   } 
 
   public MyData set(String correlatonId, MyData item) {
       Bson filter = Filters.eq("name", item.getName());
       
       FindOneAndReplaceOptions options = new FindOneAndReplaceOptions();
       options.returnDocument(ReturnDocument.AFTER);
       options.upsert(true);
       
       MyData result = _collection.findOneAndReplace(filter, item, options);
       return result;
   }
 
 }
 
 MyMongoDbPersistence persistence = new MyMongoDbPersistence();
 persistence.configure(ConfigParams.fromTuples(
     "host", "localhost",
     "port", 27017
 ));
 
 persitence.open("123");
 MyData mydata = new MyData("ABC");
 persistence.set("123", mydata); 
 persistence.getByName("123", "ABC");
 System.out.println(item);                   // Result: { name: "ABC" }
 
 
  • Field Summary

    Fields
    Modifier and Type Field Description
    protected com.mongodb.client.MongoCollection<T> _collection
    The MongoDB colleciton object.
    protected String _collectionName
    The collection name.
    protected com.mongodb.MongoClient _connection
    The MongoDB connection object.
    protected MongoDbConnectionResolver _connectionResolver
    The connection resolver.
    protected com.mongodb.client.MongoDatabase _database
    The MongoDB database name.
    protected Class<T> _documentClass
    The default class to cast any documents returned from the database into
    protected Object _lock  
    protected org.pipservices3.components.log.CompositeLogger _logger
    The logger.
    protected org.pipservices3.commons.config.ConfigParams _options
    The configuration options.
  • Constructor Summary

    Constructors
    Constructor Description
    MongoDbPersistence​(String collectionName, Class<T> documentClass)
    Creates a new instance of the persistence component.
  • Method Summary

    Modifier and Type Method Description
    protected void checkOpened​(String correlationId)
    Checks if the component is opened.
    void clear​(String correlationId)
    Clears component state.
    void close​(String correlationId)
    Closes component and frees used resources.
    void configure​(org.pipservices3.commons.config.ConfigParams config)
    Configures component by passing configuration parameters.
    boolean isOpen()
    Checks if the component is opened.
    void open​(String correlationId)
    Opens the component.
    void setReferences​(org.pipservices3.commons.refer.IReferences references)
    Sets references to dependent components.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • _collectionName

      protected String _collectionName
      The collection name.
    • _connectionResolver

      protected MongoDbConnectionResolver _connectionResolver
      The connection resolver.
    • _options

      protected org.pipservices3.commons.config.ConfigParams _options
      The configuration options.
    • _lock

      protected Object _lock
    • _connection

      protected com.mongodb.MongoClient _connection
      The MongoDB connection object.
    • _database

      protected com.mongodb.client.MongoDatabase _database
      The MongoDB database name.
    • _collection

      protected com.mongodb.client.MongoCollection<T> _collection
      The MongoDB colleciton object.
    • _documentClass

      protected Class<T> _documentClass
      The default class to cast any documents returned from the database into
    • _logger

      protected org.pipservices3.components.log.CompositeLogger _logger
      The logger.
  • Constructor Details

    • MongoDbPersistence

      public MongoDbPersistence​(String collectionName, Class<T> documentClass)
      Creates a new instance of the persistence component.
      Parameters:
      collectionName - (optional) a collection name.
      documentClass - the default class to cast any documents returned from the database into
  • Method Details

    • configure

      public void configure​(org.pipservices3.commons.config.ConfigParams config)
      Configures component by passing configuration parameters.
      Specified by:
      configure in interface org.pipservices3.commons.config.IConfigurable
      Parameters:
      config - configuration parameters to be set.
    • setReferences

      public void setReferences​(org.pipservices3.commons.refer.IReferences references)
      Sets references to dependent components.
      Specified by:
      setReferences in interface org.pipservices3.commons.refer.IReferenceable
      Parameters:
      references - references to locate the component dependencies.
    • isOpen

      public boolean isOpen()
      Checks if the component is opened.
      Specified by:
      isOpen in interface org.pipservices3.commons.run.IOpenable
      Returns:
      true if the component has been opened and false otherwise.
    • checkOpened

      protected void checkOpened​(String correlationId) throws org.pipservices3.commons.errors.InvalidStateException
      Checks if the component is opened.
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      Throws:
      org.pipservices3.commons.errors.InvalidStateException - when operation cannot be performed.
    • open

      public void open​(String correlationId) throws org.pipservices3.commons.errors.ApplicationException
      Opens the component.
      Specified by:
      open in interface org.pipservices3.commons.run.IOpenable
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • close

      public void close​(String correlationId) throws org.pipservices3.commons.errors.ApplicationException
      Closes component and frees used resources.
      Specified by:
      close in interface org.pipservices3.commons.run.IClosable
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • clear

      public void clear​(String correlationId) throws org.pipservices3.commons.errors.ApplicationException
      Clears component state.
      Specified by:
      clear in interface org.pipservices3.commons.run.ICleanable
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.