public class

MongoCloudConnector

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

Class Overview

A Mongo Connector Facade

Summary

Constants
String CAPPED_DEFAULT_VALUE
String WRITE_CONCERN_DEFAULT_VALUE
Fields
private MongoClient client
private String database The database name of the Mongo server
private String host The host of the Mongo server
private int port The port of the Mongo server
Public Constructors
MongoCloudConnector()
Public Methods
static String bsonListToJson(Object 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, String> 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(Object input)
Convert a DBObject into Map.
static String dbobjectToJson(Object 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

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, String> queryAttributes)
Lists all the files that match the given query

Iterable<DBObject> findObjects(String collection, DBObject query, List<String> fields)
Finds all objects that match a given query.
Iterable<DBObject> findObjectsUsingQueryMap(String collection, Map<String, String> queryAttributes, List<String> fields)
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, String> 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, String> queryAttributes, List<String> fields)
Finds the first object that matches a given query.
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, String> queryAttributes)
Answers an inputstream to the contents of the first file that matches the given queryAttributes.
String getHost()
int getPort()
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, String> elementAttributes, WriteConcern writeConcern)
Inserts an object in a collection, setting its id if necessary.
boolean isConnected()
static DBObject jsonToDbobject(Object 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, String> 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(Object input)
Convert a BasicBSONList into Json.
void removeFiles(DBObject query)
Removes all the files that match the given query.
void removeFilesUsingQueryMap(Map<String, String> 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, String> 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, String> elementAttributes, WriteConcern writeConcern)
Inserts or updates an object based on its object _id.
void setDatabase(String database)
void setHost(String host)
void setPort(int port)
void updateObjects(String collection, DBObject query, DBObject element, boolean upsert, boolean multi, WriteConcern writeConcern)
Updates objects that matches the given query.
void updateObjectsUsingMap(String collection, Map<String, String> queryAttributes, Map<String, String> elementAttributes, 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 MongoClient client

private String database

The database name of the Mongo server

private String host

The host of the Mongo server

private int port

The port of the Mongo server

Public Constructors

public MongoCloudConnector ()

Public Methods

public static String bsonListToJson (Object 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 in case authentication is required
password The password to use in case authentication is required, null if no authentication is desired
Returns
Throws
Exception
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, String> 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 (Object input)

Convert a DBObject into Map.

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

public static String dbobjectToJson (Object 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 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, String> 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)

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
Returns
  • an iterable of DBObject

public Iterable<DBObject> findObjectsUsingQueryMap (String collection, Map<String, String> queryAttributes, List<String> fields)

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
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, String> 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, String> 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 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, String> 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 String getHost ()

public int getPort ()

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, String> 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 (Object 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, String> 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 (Object 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, String> 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, String> 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, String> 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 setDatabase (String database)

Parameters
database

public void setHost (String host)

Parameters
host

public void setPort (int port)

Parameters
port

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 updateObjectsUsingMap (String collection, Map<String, String> queryAttributes, Map<String, String> 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

Protected Methods

protected MongoClient adaptClient (MongoClient client)

Parameters
client