public class

MongoCloudConnector

extends Object
java.lang.Object
   ↳ org.mule.module.mongo.MongoCloudConnector
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

MongoDB is an open source, high-performance, schema-free, document-oriented database that manages collections of BSON documents.

Summary

Constants
String CAPPED_DEFAULT_VALUE
String WRITE_CONCERN_DEFAULT_VALUE
Fields
private Boolean autoConnectRetry This controls whether the system retries automatically on connection errors.
private MongoClient client
private Integer connectTimeout The connection timeout in milliseconds; this is for establishing the socket connections (open).
public Integer connectionsPerHost The number of connections allowed per host (the pool size, per host)
private String database The database name of the Mongo server
public Boolean fsync Sets the fsync value of WriteConcern for the connection.
private String host The host of the Mongo server, it can also be a list of comma separated hosts for replicas
public Integer maxWaitTime The max wait time for a blocking thread for a connection from the pool in ms.
private int port The port of the Mongo server
public Boolean safe If the driver sends a getLastError command after every update to ensure it succeeded.
private Boolean slaveOk Specifies if the driver is allowed to read from secondaries or slaves.
private Integer socketTimeout The socket timeout.
public Integer threadsAllowedToBlockForConnectionMultiplier Multiplier for connectionsPerHost for # of threads that can block
public Integer w If set, the w value of WriteConcern for the connection is set to this.
public Integer wtimeout If set, the wtimeout value of WriteConcern for the connection is set to this.
Public Constructors
MongoCloudConnector()
Public Methods
static String bsonListToJson(BasicBSONList input)
Convert a BasicBSONList into Json.
void connect(String username, String password)
Method invoked when a MongoSession needs to be created.
String connectionId()
long countObjects(String collection, DBObject query)
Counts the number of objects that match the given query.
long countObjectsUsingQueryMap(String collection, Map<String, Object> queryAttributes)
Counts the number of objects that match the given query.
void createCollection(String collection, boolean capped, Integer maxObjects, Integer size)
Creates a new collection.
DBObject createFileFromPayload(Object payload, String filename, String contentType, DBObject metadata)
Creates a new GridFSFile in the database, saving the given content, filename, contentType, and extraData, and answers it.
void createIndex(String collection, String field, IndexOrder order)
Creates a new index

static Map dbObjectToMap(DBObject input)
Convert a DBObject into Map.
static String dbobjectToJson(DBObject input)
Convert DBObject to Json.
void disconnect()
Method invoked when the MongoSession is to be destroyed.
void dropCollection(String collection)
Deletes a collection and all the objects it contains.
void dropIndex(String collection, String index)
Drops an existing index

DBObject executeCommand(String commandName, String commandValue)
Executes a command on the database

boolean existsCollection(String collection)
Answers if a collection exists given its name

Iterable<DBObject> findFiles(DBObject query)
Lists all the files that match the given query

Iterable<DBObject> findFilesUsingQueryMap(Map<String, Object> queryAttributes)
Lists all the files that match the given query

Iterable<DBObject> findObjects(String collection, DBObject query, List<String> fields, Integer numToSkip, Integer limit)
Finds all objects that match a given query.
Iterable<DBObject> findObjectsUsingQueryMap(String collection, Map<String, Object> queryAttributes, List<String> fields, Integer numToSkip, Integer limit)
Finds all objects that match a given query.
DBObject findOneFile(DBObject query)
Answers the first file that matches the given query.
DBObject findOneFileUsingQueryMap(Map<String, Object> queryAttributes)
Answers the first file that matches the given query.
DBObject findOneObject(String collection, DBObject query, List<String> fields)
Finds the first object that matches a given query.
DBObject findOneObjectUsingQueryMap(String collection, Map<String, Object> queryAttributes, List<String> fields)
Finds the first object that matches a given query.
Boolean getAutoConnectRetry()
Integer getConnectTimeout()
Integer getConnectionsPerHost()
String getDatabase()
InputStream getFileContent(DBObject query)
Answers an inputstream to the contents of the first file that matches the given query.
InputStream getFileContentUsingQueryMap(Map<String, Object> queryAttributes)
Answers an inputstream to the contents of the first file that matches the given queryAttributes.
Boolean getFsync()
String getHost()
Integer getMaxWaitTime()
int getPort()
Boolean getSafe()
Boolean getSlaveOk()
Integer getSocketTimeout()
Integer getThreadsAllowedToBlockForConnectionMultiplier()
Integer getW()
Integer getWtimeout()
String insertObject(String collection, DBObject dbObject, WriteConcern writeConcern)
Inserts an object in a collection, setting its id if necessary.
String insertObjectFromMap(String collection, Map<String, Object> elementAttributes, WriteConcern writeConcern)
Inserts an object in a collection, setting its id if necessary.
boolean isConnected()
static DBObject jsonToDbobject(String input)
Convert JSON to DBObject.
Collection<String> listCollections()
Lists names of collections available at this database

Iterable<DBObject> listFiles(DBObject query)
Lists all the files that match the given query, sorting them by filename.
Iterable<DBObject> listFilesUsingQueryMap(Map<String, Object> queryAttributes)
Lists all the files that match the given query, sorting them by filename.
Collection<DBObject> listIndices(String collection)
List existent indices in a collection

Iterable<DBObject> mapReduceObjects(String collection, String mapFunction, String reduceFunction, String outputCollection)
Transforms a collection into a collection of aggregated groups, by applying a supplied element-mapping function to each element, that transforms each one into a key-value pair, grouping the resulting pairs by key, and finally reducing values in each group applying a suppling 'reduce' function.
static String mongoCollectionToJson(MongoCollection input)
Convert a BasicBSONList into Json.
void removeFiles(DBObject query)
Removes all the files that match the given query.
void removeFilesUsingQueryMap(Map<String, Object> queryAttributes)
Removes all the files that match the given query.
void removeObjects(String collection, DBObject query, WriteConcern writeConcern)
Removes all the objects that match the a given optional query.
void removeUsingQueryMap(String collection, Map<String, Object> queryAttributes, WriteConcern writeConcern)
Removes all the objects that match the a given optional query.
void saveObject(String collection, DBObject element, WriteConcern writeConcern)
Inserts or updates an object based on its object _id.
void saveObjectFromMap(String collection, Map<String, Object> elementAttributes, WriteConcern writeConcern)
Inserts or updates an object based on its object _id.
void setAutoConnectRetry(Boolean autoConnectRetry)
void setConnectTimeout(Integer connectTimeout)
void setConnectionsPerHost(Integer connectionsPerHost)
void setDatabase(String database)
void setFsync(Boolean fsync)
void setHost(String host)
void setMaxWaitTime(Integer maxWaitTime)
void setPort(int port)
void setSafe(Boolean safe)
void setSlaveOk(Boolean slaveOk)
void setSocketTimeout(Integer socketTimeout)
void setThreadsAllowedToBlockForConnectionMultiplier(Integer threadsAllowedToBlockForConnectionMultiplier)
void setW(Integer w)
void setWtimeout(Integer wtimeout)
void updateObjects(String collection, DBObject query, DBObject element, boolean upsert, boolean multi, WriteConcern writeConcern)
Updates objects that matches the given query.
void updateObjectsByFunction(String collection, String function, DBObject query, DBObject element, boolean upsert, boolean multi, WriteConcern writeConcern)
Update objects using a mongo function

void updateObjectsByFunctionUsingMap(String collection, String function, Map<String, Object> queryAttributes, Map<String, Object> elementAttributes, boolean upsert, boolean multi, WriteConcern writeConcern)
Update objects using a mongo function

void updateObjectsUsingMap(String collection, Map<String, Object> queryAttributes, Map<String, Object> elementAttributes, boolean upsert, boolean multi, WriteConcern writeConcern)
Updates objects that matches the given query.
void updateObjectsUsingQueryMap(String collection, Map<String, Object> queryAttributes, DBObject element, boolean upsert, boolean multi, WriteConcern writeConcern)
Updates objects that matches the given query.
Protected Methods
MongoClient adaptClient(MongoClient client)
[Expand]
Inherited Methods
From class java.lang.Object

Constants

private static final String CAPPED_DEFAULT_VALUE

Constant Value: "false"

private static final String WRITE_CONCERN_DEFAULT_VALUE

Constant Value: "DATABASE_DEFAULT"

Fields

private Boolean autoConnectRetry

This controls whether the system retries automatically on connection errors.

private MongoClient client

private Integer connectTimeout

The connection timeout in milliseconds; this is for establishing the socket connections (open). 0 is default and infinite.

public Integer connectionsPerHost

The number of connections allowed per host (the pool size, per host)

private String database

The database name of the Mongo server

public Boolean fsync

Sets the fsync value of WriteConcern for the connection.

private String host

The host of the Mongo server, it can also be a list of comma separated hosts for replicas

public Integer maxWaitTime

The max wait time for a blocking thread for a connection from the pool in ms.

private int port

The port of the Mongo server

public Boolean safe

If the driver sends a getLastError command after every update to ensure it succeeded.

private Boolean slaveOk

Specifies if the driver is allowed to read from secondaries or slaves.

private Integer socketTimeout

The socket timeout. 0 is default and infinite.

public Integer threadsAllowedToBlockForConnectionMultiplier

Multiplier for connectionsPerHost for # of threads that can block

public Integer w

If set, the w value of WriteConcern for the connection is set to this.

public Integer wtimeout

If set, the wtimeout value of WriteConcern for the connection is set to this.

Public Constructors

public MongoCloudConnector ()

Public Methods

public static String bsonListToJson (BasicBSONList input)

Convert a BasicBSONList into Json.

Parameters
input The input for this transformer
Returns
  • the converted string representation

public void connect (String username, String password)

Method invoked when a MongoSession needs to be created.

Parameters
username The username to use for authentication. NOTE: Please use a dummy user if you have disabled Mongo authentication
password The password to use for authentication. NOTE: Please use a dummy password if you have disabled Mongo authentication
Returns
Throws
ConnectionException

public String connectionId ()

public long countObjects (String collection, DBObject query)

Counts the number of objects that match the given query. If no query is passed, returns the number of elements in the collection

Parameters
collection The target collection
query The optional DBObject query for counting objects. Only objects matching it will be counted. If unspecified, all objects are counted.
Returns
  • the amount of objects that matches the query

public long countObjectsUsingQueryMap (String collection, Map<String, Object> queryAttributes)

Counts the number of objects that match the given query. If no query is passed, returns the number of elements in the collection

Parameters
collection The target collection
queryAttributes The optional query for counting objects. Only objects matching it will be counted. If unspecified, all objects are counted.
Returns
  • the amount of objects that matches the query

public void createCollection (String collection, boolean capped, Integer maxObjects, Integer size)

Creates a new collection. If the collection already exists, a MongoException will be thrown.

Parameters
collection The name of the collection to create
capped If the collection will be capped
maxObjects The maximum number of documents the new collection is able to contain
size The maximum size of the new collection

public DBObject createFileFromPayload (Object payload, String filename, String contentType, DBObject metadata)

Creates a new GridFSFile in the database, saving the given content, filename, contentType, and extraData, and answers it.

Parameters
payload The mandatory content of the new gridfs file. It may be a java.io.File, a byte[] or an InputStream.
filename The mandatory name of new file.
contentType The optional content type of the new file
metadata The optional DBObject metadata of the new content type
Returns
  • the new GridFSFile DBObject
Throws
IOException

public void createIndex (String collection, String field, IndexOrder order)

Creates a new index

Parameters
collection The name of the collection where the index will be created
field The name of the field which will be indexed
order The indexing order

public static Map dbObjectToMap (DBObject input)

Convert a DBObject into Map.

Parameters
input The input for this transformer
Returns
  • the converted Map representation

public static String dbobjectToJson (DBObject input)

Convert DBObject to Json.

Parameters
input The input for this transformer
Returns
  • the converted string representation

public void disconnect ()

Method invoked when the MongoSession is to be destroyed.

public void dropCollection (String collection)

Deletes a collection and all the objects it contains. If the collection does not exist, does nothing.

Parameters
collection The name of the collection to drop

public void dropIndex (String collection, String index)

Drops an existing index

Parameters
collection The name of the collection where the index is
index The name of the index to drop

public DBObject executeCommand (String commandName, String commandValue)

Executes a command on the database

Parameters
commandName The command to execute on the database
commandValue The value for the command
Returns
  • The result of the command

public boolean existsCollection (String collection)

Answers if a collection exists given its name

Parameters
collection The name of the collection
Returns
  • if the collection exists

public Iterable<DBObject> findFiles (DBObject query)

Lists all the files that match the given query

Parameters
query A DBObject query the optional query
Returns
  • a DBObject files iterable

public Iterable<DBObject> findFilesUsingQueryMap (Map<String, Object> queryAttributes)

Lists all the files that match the given query

Parameters
queryAttributes The optional query attributes
Returns
  • a DBObject files iterable

public Iterable<DBObject> findObjects (String collection, DBObject query, List<String> fields, Integer numToSkip, Integer limit)

Finds all objects that match a given query. If no query is specified, all objects of the collection are retrieved. If no fields object is specified, all fields are retrieved.

Parameters
collection The target collection
query The optional DBObject query object. If unspecified, all documents are returned.
fields Alternative way of passing fields as a literal List
numToSkip Number of objects skip (offset)
limit Limit of objects to return
Returns
  • an iterable of DBObject

public Iterable<DBObject> findObjectsUsingQueryMap (String collection, Map<String, Object> queryAttributes, List<String> fields, Integer numToSkip, Integer limit)

Finds all objects that match a given query. If no query is specified, all objects of the collection are retrieved. If no fields object is specified, all fields are retrieved.

Parameters
collection The target collection
queryAttributes The optional query object. If unspecified, all documents are returned.
fields Alternative way of passing fields as a literal List
numToSkip Number of objects skip (offset)
limit Limit of objects to return
Returns
  • an iterable of DBObject

public DBObject findOneFile (DBObject query)

Answers the first file that matches the given query. If no object matches it, a MongoException is thrown.

Parameters
query The DBObject mandatory query
Returns
  • a DBObject

public DBObject findOneFileUsingQueryMap (Map<String, Object> queryAttributes)

Answers the first file that matches the given query. If no object matches it, a MongoException is thrown.

Parameters
queryAttributes The mandatory query
Returns
  • a DBObject

public DBObject findOneObject (String collection, DBObject query, List<String> fields)

Finds the first object that matches a given query. Throws a MongoException if no one matches the given query

Parameters
collection The target collection
query The mandatory DBObject query object that the returned object matches.
fields Alternative way of passing fields as a literal List
Returns
  • a non-null DBObject that matches the query.

public DBObject findOneObjectUsingQueryMap (String collection, Map<String, Object> queryAttributes, List<String> fields)

Finds the first object that matches a given query. Throws a MongoException if no one matches the given query

Parameters
collection The target collection
queryAttributes The mandatory query object that the returned object matches.
fields Alternative way of passing fields as a literal List
Returns
  • a non-null DBObject that matches the query.

public Boolean getAutoConnectRetry ()

public Integer getConnectTimeout ()

public Integer getConnectionsPerHost ()

public String getDatabase ()

public InputStream getFileContent (DBObject query)

Answers an inputstream to the contents of the first file that matches the given query. If no object matches it, a MongoException is thrown.

Parameters
query The DBObject mandatory query
Returns
  • an InputStream to the file contents

public InputStream getFileContentUsingQueryMap (Map<String, Object> queryAttributes)

Answers an inputstream to the contents of the first file that matches the given queryAttributes. If no object matches it, a MongoException is thrown.

Parameters
queryAttributes The mandatory query attributes
Returns
  • an InputStream to the file contents

public Boolean getFsync ()

public String getHost ()

public Integer getMaxWaitTime ()

public int getPort ()

public Boolean getSafe ()

public Boolean getSlaveOk ()

public Integer getSocketTimeout ()

public Integer getThreadsAllowedToBlockForConnectionMultiplier ()

public Integer getW ()

public Integer getWtimeout ()

public String insertObject (String collection, DBObject dbObject, WriteConcern writeConcern)

Inserts an object in a collection, setting its id if necessary.

Parameters
collection The name of the collection where to insert the given object
dbObject A DBObject instance.
writeConcern The optional write concern of insertion
Returns
  • the id that was just insterted

public String insertObjectFromMap (String collection, Map<String, Object> elementAttributes, WriteConcern writeConcern)

Inserts an object in a collection, setting its id if necessary.

A shallow conversion into DBObject is performed - that is, no conversion is performed to its values.

Parameters
collection The name of the collection where to insert the given object
elementAttributes Alternative way of specifying the element as a literal Map inside a Mule Flow
writeConcern The optional write concern of insertion
Returns
  • the id that was just insterted

public boolean isConnected ()

public static DBObject jsonToDbobject (String input)

Convert JSON to DBObject.

Parameters
input The input for this transformer
Returns
  • the converted DBObject

public Collection<String> listCollections ()

Lists names of collections available at this database

Returns
  • the list of names of collections available at this database

public Iterable<DBObject> listFiles (DBObject query)

Lists all the files that match the given query, sorting them by filename. If no query is specified, all files are listed.

Parameters
query The DBObject optional query
Returns
  • an iterable of DBObject

public Iterable<DBObject> listFilesUsingQueryMap (Map<String, Object> queryAttributes)

Lists all the files that match the given query, sorting them by filename. If no query is specified, all files are listed.

Parameters
queryAttributes The optional query
Returns
  • an iterable of DBObject

public Collection<DBObject> listIndices (String collection)

List existent indices in a collection

Parameters
collection The name of the collection
Returns
  • a collection of DBObject with indices information

public Iterable<DBObject> mapReduceObjects (String collection, String mapFunction, String reduceFunction, String outputCollection)

Transforms a collection into a collection of aggregated groups, by applying a supplied element-mapping function to each element, that transforms each one into a key-value pair, grouping the resulting pairs by key, and finally reducing values in each group applying a suppling 'reduce' function.

Each supplied function is coded in JavaScript.

Note that the correct way of writing those functions may not be obvious; please consult MongoDB documentation for writing them.

Parameters
collection The name of the collection to map and reduce
mapFunction A JavaScript encoded mapping function
reduceFunction A JavaScript encoded reducing function
outputCollection The name of the output collection to write the results, replacing previous collection if existed, mandatory when results may be larger than 16MB. If outputCollection is unspecified, the computation is performed in-memory and not persisted.
Returns
  • an iterable that retrieves the resulting collection of DBObject

public static String mongoCollectionToJson (MongoCollection input)

Convert a BasicBSONList into Json.

Parameters
input The input for this transformer
Returns
  • the converted string representation

public void removeFiles (DBObject query)

Removes all the files that match the given query. If no query is specified, all files are removed

Parameters
query The DBObject optional query

public void removeFilesUsingQueryMap (Map<String, Object> queryAttributes)

Removes all the files that match the given query. If no query is specified, all files are removed

Parameters
queryAttributes The optional query

public void removeObjects (String collection, DBObject query, WriteConcern writeConcern)

Removes all the objects that match the a given optional query. If query is not specified, all objects are removed. However, please notice that this is normally less performant that dropping the collection and creating it and its indices again

Parameters
collection The collection whose elements will be removed
query The optional DBObject query object. Objects that match it will be removed.
writeConcern The write concern used to remove the object

public void removeUsingQueryMap (String collection, Map<String, Object> queryAttributes, WriteConcern writeConcern)

Removes all the objects that match the a given optional query. If query is not specified, all objects are removed. However, please notice that this is normally less performant that dropping the collection and creating it and its indices again

Parameters
collection The collection whose elements will be removed
queryAttributes The query object. Objects that match it will be removed.
writeConcern The write concern used to remove the object

public void saveObject (String collection, DBObject element, WriteConcern writeConcern)

Inserts or updates an object based on its object _id.

Parameters
collection The collection where to insert the object
element The mandatory DBObject object to insert.
writeConcern The write concern used to persist the object

public void saveObjectFromMap (String collection, Map<String, Object> elementAttributes, WriteConcern writeConcern)

Inserts or updates an object based on its object _id.

Parameters
collection The collection where to insert the object
elementAttributes The mandatory object to insert.
writeConcern The write concern used to persist the object

public void setAutoConnectRetry (Boolean autoConnectRetry)

Parameters
autoConnectRetry

public void setConnectTimeout (Integer connectTimeout)

Parameters
connectTimeout

public void setConnectionsPerHost (Integer connectionsPerHost)

Parameters
connectionsPerHost

public void setDatabase (String database)

Parameters
database

public void setFsync (Boolean fsync)

Parameters
fsync

public void setHost (String host)

Parameters
host

public void setMaxWaitTime (Integer maxWaitTime)

Parameters
maxWaitTime

public void setPort (int port)

Parameters
port

public void setSafe (Boolean safe)

Parameters
safe

public void setSlaveOk (Boolean slaveOk)

Parameters
slaveOk

public void setSocketTimeout (Integer socketTimeout)

Parameters
socketTimeout

public void setThreadsAllowedToBlockForConnectionMultiplier (Integer threadsAllowedToBlockForConnectionMultiplier)

Parameters
threadsAllowedToBlockForConnectionMultiplier

public void setW (Integer w)

Parameters
w

public void setWtimeout (Integer wtimeout)

Parameters
wtimeout

public void updateObjects (String collection, DBObject query, DBObject element, boolean upsert, boolean multi, WriteConcern writeConcern)

Updates objects that matches the given query. If parameter multi is set to false, only the first document matching it will be updated. Otherwise, all the documents matching it will be updated.

Parameters
collection The name of the collection to update
query The DBObject query object used to detect the element to update.
element The DBObject mandatory object that will replace that one which matches the query.
upsert If the database should create the element if it does not exist
multi If all or just the first object matching the query will be updated
writeConcern The write concern used to update

public void updateObjectsByFunction (String collection, String function, DBObject query, DBObject element, boolean upsert, boolean multi, WriteConcern writeConcern)

Update objects using a mongo function

Parameters
collection The name of the collection to update
function The function used to execute the update
query The DBObject query object used to detect the element to update.
element The DBObject mandatory object that will replace that one which matches the query.
upsert If the database should create the element if it does not exist
multi If all or just the first object matching the query will be updated
writeConcern The write concern used to update

public void updateObjectsByFunctionUsingMap (String collection, String function, Map<String, Object> queryAttributes, Map<String, Object> elementAttributes, boolean upsert, boolean multi, WriteConcern writeConcern)

Update objects using a mongo function

Parameters
collection The name of the collection to update
function The function used to execute the update
queryAttributes The query object used to detect the element to update.
elementAttributes The mandatory object that will replace that one which matches the query.
upsert If the database should create the element if it does not exist
multi If all or just the first object matching the query will be updated
writeConcern The write concern used to update

public void updateObjectsUsingMap (String collection, Map<String, Object> queryAttributes, Map<String, Object> elementAttributes, boolean upsert, boolean multi, WriteConcern writeConcern)

Updates objects that matches the given query. If parameter multi is set to false, only the first document matching it will be updated. Otherwise, all the documents matching it will be updated.

Parameters
collection The name of the collection to update
queryAttributes The query object used to detect the element to update.
elementAttributes The mandatory object that will replace that one which matches the query.
upsert If the database should create the element if it does not exist
multi If all or just the first object matching the query will be updated
writeConcern The write concern used to update

public void updateObjectsUsingQueryMap (String collection, Map<String, Object> queryAttributes, DBObject element, boolean upsert, boolean multi, WriteConcern writeConcern)

Updates objects that matches the given query. If parameter multi is set to false, only the first document matching it will be updated. Otherwise, all the documents matching it will be updated.

Parameters
collection The name of the collection to update
queryAttributes The query object used to detect the element to update.
element The DBObject mandatory object that will replace that one which matches the query.
upsert If the database should create the element if it does not exist
multi If all or just the first object matching the query will be updated
writeConcern The write concern used to update

Protected Methods

protected MongoClient adaptClient (MongoClient client)

Parameters
client