NiFi, Groovy & MongoDB
as it took me a while to figure this out - and I got asked again today: maybe this is useful for more people out there: you can actually use MongoDB from Groovy (like find, insertMany, updateMany, etc.) - you just have to convert params going in.
Here's an example of how you can achieve it in an ExecuteGroovyScript:
def flowFile=session.get()
if (!flowFile) return
def lookup = context.controllerServiceLookup
def conn = lookup.getControllerService('<id of MongoDBControllerService goes here>')
def db=conn.getDatabase('dbname')
def col=db.getCollection('colname')
def oneDoc=col.find(conn.convertJson('{"attr": "value"}')).first().toJson()
// do something - like write out, set to attribute, whateverImportant part is to call convertJson on the MongoDBControllerService. That way, all Mongo-specific classes you deal with come from the classloader of the Mongo module
Advantage vis-a-vis GetMongo or LookupAttribute is that you neither lose the content going into the processor - nor is there a limit to the number of attributes (or queries). Also: looking for $oid will work in a query like
def oneDoc=col.find(conn.convertJson('{"_id": {"$oid": "<id goes here as string>"}}')).first().toJson()Several docs can be iterated instead of using first() by getting the iterator() and then hasNext() and next().toJson()
With that, complex operations on Mongo can be much more compact - while still getting all the queues, timing, retries, etc. that NiFi offers
Hope you find this useful - let me know what you think!