Contact Us 1-800-596-4880

Mongo DB Connector API Reference

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

Additional Info

Requires Mule Enterprise License

Yes  

Requires Entitlement

No  

Mule Version

3.6.0 or higher

Configs


Configuration

<mongo:config>

Connection Management

Attributes

Name Java Type Description Default Value Required

name

String

The name of this configuration. With this name can be later referenced.

x 

username

String

the username to use for authentication.

 

password

String

the password to use for authentication. If the password is null or whitespaces only, the connector won't use authentication and username must be empty too.

 

database

String

Name of the database

x 

host

String

A list of MongoDB instance, with the format host:port, separated by commas.

Example: 127.0.0.1:27017, 192.168.1.2:27017

localhost:27017

 

connectionsPerHost

Integer

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

 

threadsAllowedToBlockForConnectionMultiplier

Integer

Multiplier for connectionsPerHost for # of threads that can block.

 

maxWaitTime

Integer

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

 

connectTimeout

Integer

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

30000

 

socketTimeout

Integer

The socket timeout. 0 is default and infinite.

 

ssl

boolean

This is for enabling an SSL connection. It is disabled by default.

false

 

authenticationDatabase

String

 


Connection String

<mongo:config-connection-string>

Connection Management

Attributes

Name Java Type Description Default Value Required

name

String

The name of this configuration. With this name can be later referenced.

x 

connectionString

String

x 

Processors


Add user

<mongo:add-user>

Adds a new user for this db

XML Sample

<mongo:add-user config-ref="Mongo_DB__Configuration" newUsername="myUser" newPassword="myPassword"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

newUsername

String

Name of the user

x 

newPassword

String

Password that will be used for authentication

x 

Returns

Return Java Type Description

Document

Result of the operation


Drop database

<mongo:drop-database>

Drop the current database

XML Sample

<mongo:drop-database config-ref="Mongo_DB__Configuration"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 


List collections

<mongo:list-collections>

Lists names of collections available at this database

XML Sample

<mongo:list-collections config-ref="Mongo_DB__Configuration"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

Returns

Return Java Type Description

Iterable<String>

the list of names of collections available at this database


Exists collection

<mongo:exists-collection>

Answers if a collection exists given its name

XML Sample

<mongo:exists-collection config-ref="Mongo_DB__Configuration" collection="aColllection"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the name of the collection

#[payload]

 

Returns

Return Java Type Description

boolean

if the collection exists


Drop collection

<mongo:drop-collection>

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

XML Sample

<mongo:drop-collection config-ref="Mongo_DB__Configuration" collection="aCollection"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the name of the collection to drop

#[payload]

 


Create collection

<mongo:create-collection>

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

XML Sample

<mongo:create-collection config-ref="Mongo_DB__Configuration" collection="aCollection" capped="true"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the name of the collection to create

x 

capped

boolean

if the collection will be capped

false

 

maxObjects

Integer

the maximum number of documents the new collection is able to contain

 

size

Integer

the maximum size of the new collection

 


Insert document

<mongo:insert-document>

Inserts a document in a collection, setting its id if necessary.

XML Sample

<mongo:insert-document config-ref="Mongo_DB__Configuration" collection="Employees">
        </mongo:insert-document>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the name of the collection where to insert the given document.

x 

document

Document

a Document instance.

#[payload]

 

Returns

Return Java Type Description

String

the id that was just inserted


Insert documents

<mongo:insert-documents>

Inserts a list of documents in a collection, setting its id if necessary.

XML Sample

<mongo:insert-documents config-ref="Mongo_DB__Configuration" collection="Employees">
        </mongo:insert-documents>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the name of the collection where to insert the given document.

x 

documents

List<Document>

a List of Document.

#[payload]

 

ordered

boolean

indicates if the list of write operations is ordered or unordered. By default, ff an error occurs during the processing of one of the write operations, MongoDB will return without processing any remaining write operations in the list.

true

 

Returns

Return Java Type Description

Document

a Document with the operation result


Update documents

<mongo:update-documents>

Updates documents that matches the given query. If query is not specified, all documents are retrieved. 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.

XML Sample

<mongo:update-documents config-ref="Mongo_DB__Configuration" collection="myCollection" query-ref="#[payload]">
        </mongo:update-documents>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the name of the collection to update

x 

query

Document

the Document query object used to detect the element to update.

 

element

Document

the Document mandatory object that will replace that one which matches the query.

#[payload]

 

multi

boolean

if all or just the first document matching the query will be updated

true

 

Returns

Return Java Type Description

Document

a Document with the update status


Update documents by function

<mongo:update-documents-by-function>

Update documents using a mongo function. If query is not specified, all documents are retrieved.

XML Sample

<mongo:update-documents-by-function config-ref="Mongo_DB__Configuration" collection="myCollection" element-ref="#[payload]" upsert="true" function="$set">
        </mongo:update-documents-by-function>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the name of the collection to update

x 

function

String

the function used to execute the update

x 

query

Document

the Document query document used to detect the element to update.

 

element

Document

the Document mandatory document that will replace that one which matches the query.

#[payload]

 

upsert

boolean

if the database should create the element if it does not exist.

false

 

multi

boolean

if all or just the first document matching the query will be updated.

true

 

Returns

Return Java Type Description

Document

a Document with the update status.


Update documents by functions

<mongo:update-documents-by-functions>

Update documents using one or more mongo function(s). If query is not specified, all documents are retrieved. If there are duplicated operators. Only the last one will be executed.

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the name of the collection to update

x 

query

Document

the Document query document used to detect the element to update

 

functions

Document

the Document of functions used to execute the update. e.g. <$set,{"key":123}>

#[payload]

 

upsert

boolean

whether the database should create the element if it does not exist

false

 

multi

boolean

if all or just the first document matching the query will be updated

true

 

Returns

Return Java Type Description

Document

a Document with the update status.


Save document

<mongo:save-document>

Inserts or updates a document based on its object _id.

XML Sample

<mongo:save-document config-ref="Mongo_DB__Configuration" collection="myCollection">
        </mongo:save-document>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the collection where to insert the object

x 

document

Document

the mandatory Document document to insert.

#[payload]

 


Remove documents

<mongo:remove-documents>

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

XML Sample

<mongo:remove-documents config-ref="Mongo_DB__Configuration" collection="myCollection">
        </mongo:remove-documents>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the collection whose elements will be removed

x 

query

Document

the optional Document query object. Documents that match it will be removed.

#[payload]

 


Map reduce objects

<mongo:map-reduce-objects>

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.

XML Sample

<mongo:map-reduce-objects config-ref="Mongo_DB__Configuration" collection="myCollection" mapFunction="myMapFunction" reduceFunction="myReduceFunction"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the name of the collection to map and reduce

x 

mapFunction

String

a JavaScript encoded mapping function

x 

reduceFunction

String

a JavaScript encoded reducing function

x 

outputCollection

String

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

Return Java Type Description

Iterable<Document>

an iterable that retrieves the resulting collection of Document


Count documents

<mongo:count-documents>

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

XML Sample

<mongo:count-documents config-ref="Mongo_DB__Configuration" collection="myCollection" query-ref="dbObject"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the target collection

x 

query

Bson

the optional Document query for counting documents. Only documents matching it will be counted. If unspecified, all documents are counted.

#[payload]

 

Returns

Return Java Type Description

long

the amount of documents that matches the query


Find documents

<mongo:find-documents>

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

XML Sample

<mongo:find-documents config-ref="Mongo_DB__Configuration" collection="myCollection" limit="5" numToSkip="10" query-ref="#[payload]" sortBy-ref="#[new org.bson.Document()]">
            <mongo:fields>
                <mongo:field>field1</mongo:field>
                <mongo:field>field2</mongo:field>
            </mongo:fields>
        </mongo:find-documents>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the target collection

x 

query

Document

the optional Document query document. If unspecified, all documents are returned.

#[payload]

 

fields

List<String>

alternative way of passing fields as a literal List

 

numToSkip

Integer

number of documents skip (offset)

 

limit

Integer

limit of documents to return

 

sortBy

Document

indicates the Document used to sort the results

 

Returns

Return Java Type Description

Iterable<Document>

an iterable of Document


Find one document

<mongo:find-one-document>

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

XML Sample

<mongo:find-one-document config-ref="Mongo_DB__Configuration" collection="myCollection">
            <mongo:fields>
                <mongo:field>field1</mongo:field>
                <mongo:field>field2</mongo:field>
            </mongo:fields>
        </mongo:find-one-document>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the target collection

x 

query

Document

the mandatory Document query document that the returned object matches.

#[payload]

 

fields

List<String>

alternative way of passing fields as a literal List

 

failOnNotFound

Boolean

Flag to specify if an exception will be thrown when no object is found. For backward compatibility the default value is true.

true

 

Returns

Return Java Type Description

Document

a Document that matches the query. If nothing matches and the failOnNotFound is set to false, null will be returned


Find one and update document

<mongo:find-one-and-update-document>

Finds and update the first document that matches a given query. When returnNew = true, returns the updated document instead of the original document.

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the target collection

x 

query

Document

the Document query that the returned object matches.

 

element

Document

the Document mandatory object that will replace that one which matches the query

#[payload]

 

fields

List<String>

alternative way of passing fields as a literal List

 

returnNewDocument

Boolean

Flag to specify if the returning Document should be the updated document instead of the original. Defaults to false, returning the document before modifications

false

 

sortBy

Document

indicates the Document used to sort the results.

 

remove

boolean

removes the Document specified in the query field. Defaults to false

false

 

upsert

boolean

whether the database should create the element if it does not exist

false

 

bypassDocumentValidation

boolean

lets you update documents that do not meet the validation requirements. Defaults to false

false

 

Returns

Return Java Type Description

Document

a Document that matches the query. If nothing matches, null will be returned


Create index

<mongo:create-index>

Creates a new index

XML Sample

<mongo:create-index config-ref="Mongo_DB__Configuration" collection="myCollection" field="myField" order="DESC"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the name of the collection where the index will be created

x 

field

String

the name of the field which will be indexed

x 

order

IndexOrder

the indexing order

ASC

 


Drop index

<mongo:drop-index>

Drops an existing index

XML Sample

<mongo:drop-index config-ref="Mongo_DB__Configuration" collection="myCollection" index="myIndex"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the name of the collection where the index is

x 

index

String

the name of the index to drop

x 


List indices

<mongo:list-indices>

List existent indices in a collection

XML Sample

<mongo:list-indices config-ref="Mongo_DB__Configuration" collection="myCollection"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

collection

String

the name of the collection

x 

Returns

Return Java Type Description

Collection<Document>

a collection of Document with indices information


Create file from payload

<mongo:create-file-from-payload>

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

XML Sample

<mongo:create-file-from-payload config-ref="Mongo_DB__Configuration" filename="#[variable:filename]" metadata-ref="myDbObject"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

payload

Object

the mandatory content of the new gridfs file. It may be a java.io.File, a byte[] or an InputStream.

#[payload]

 

filename

String

the mandatory name of new file.

x 

metadata

Document

the optional Document metadata of the new content type

 

Returns

Return Java Type Description

GridFSFile

the new GridFSFile GridFSFile


Find files

<mongo:find-files>

Lists all the files that match the given query

XML Sample

<mongo:find-files config-ref="Mongo_DB__Configuration" query-ref="myDbObject" />

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

query

Document

a Document query

#[payload]

 

Returns

Return Java Type Description

Iterable<GridFSFile>

a GridFSFile files iterable


Find one file

<mongo:find-one-file>

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

XML Sample

<mongo:find-one-file config-ref="Mongo_DB__Configuration" query-ref="myDbObject" />

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

query

Document

the Document mandatory query

#[payload]

 

Returns

Return Java Type Description

GridFSFile

a GridFSFile


Get file content

<mongo:get-file-content>

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

XML Sample

<mongo:get-file-content config-ref="Mongo_DB__Configuration" query-ref="myDbObject" />

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

fileId

BsonObjectId

the BsonObjectId of the file to be deleted

#[payload]

 

Returns

Return Java Type Description

InputStream

an InputStream to the file contents


List files

<mongo:list-files>

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

XML Sample

<mongo:list-files config-ref="Mongo_DB__Configuration" query-ref="myDbObject"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

query

Document

the Document optional query

#[payload]

 

Returns

Return Java Type Description

Iterable<GridFSFile>

an iterable of GridFSFile


Remove files

<mongo:remove-files>

Removes the file that matches the given file id. If no file id is specified, all files are removed

XML Sample

<mongo:remove-files config-ref="Mongo_DB__Configuration" query-ref="myDbObject"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

fileId

BsonObjectId

the BsonObjectId of the file to be deleted

#[payload]

 


Execute command

<mongo:execute-command>

Executes a command on the database

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

commandName

String

The command to execute on the database

x 

commandValue

String

The value for the command

 

Returns

Return Java Type Description

Document

The result of the command


Dump

<mongo:dump>

Executes a dump of the database to the specified output directory. If no output directory is provided then the default /dump directory is used.

XML Sample

<mongo:dump config-ref="Mongo_DB__Configuration" oplog="true"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

outputDirectory

String

output directory path, if no output directory is provided the default /dump directory is assumed

dump

 

outputName

String

output file name, if it's not specified the database name is used

 

zip

boolean

whether to zip the created dump file or not

false

 

oplog

boolean

point in time backup (requires an oplog)

false

 

threads

int

amount of threads to execute the dump

5

 


Incremental dump

<mongo:incremental-dump>

Executes an incremental dump of the database

XML Sample

<mongo:incremental-dump config-ref="Mongo_DB__Configuration" />

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

outputDirectory

String

output directory path, if no output directory is provided the default /dump directory is assumed

dump

 

incrementalTimestampFile

String

file that keeps track of the last timestamp processed, if no file is provided one is created on the output directory

 


Restore

<mongo:restore>

Takes the output from the dump and restores it. Indexes will be created on a restore. It only does inserts with the data to restore, if existing data is there, it will not be replaced.

XML Sample

<mongo:restore config-ref="Mongo_DB__Configuration" inputPath="dump/test.zip" oplogReplay="true"/>

Attributes

Name Java Type Description Default Value Required

config-ref

String

Specify which config to use

x 

inputPath

String

input path to the dump files, it can be a directory, a zip file or just a bson file

dump

 

drop

boolean

whether to drop existing collections before restore

false

 

oplogReplay

boolean

replay oplog for point-in-time restore

false

 

View on GitHub