Search code examples
mongodbmuleanypoint-studio

Can you update all Mongo documents in a collection at once in Mule?


I am trying to update the Effective date in my mongoDB collection with the current date. I want to update all documents in the collection where the Effective Date is null.

How do you configure the transformer to do this? The Query Reference field info message talks about an Id and the element field looks like it needs a payload containing a document that will replace the one being updated. I am just looking to update one field and currently getting an error with my query but it does work on the mongoDB command line.

This is my query that I want to implement:

db.stores.update({"EffectiveEndDateTime" : null}, {$set : {"EffectiveEndDateTime" : "2017-01-13T18:56:55.257Z"}})

My XML configure for Mongo in Mule so far:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:mongo="http://www.mulesoft.org/schema/mule/mongo" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json"
    xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 
    xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/mongo http://www.mulesoft.org/schema/mule/mongo/current/mule-mongo.xsd">
    <mongo:config name="Mongo_DB__Configuration" username="${mongodb.username}" password="${mongodb.password}" database="${mongodb.database}" host="${mongodb.host}" doc:name="Mongo DB: Configuration"/>
    <flow name="deactivateCurrentDocumentsFlow">
        <http:listener config-ref="httpListenerConfig" path="/mongo" allowedMethods="POST" doc:name="HTTP"/>
        <mongo:update-documents-by-function config-ref="Mongo_DB__Configuration" collection="stores" function="$set : {&quot;EffectiveEndDateTime&quot;: &quot;2017-01-13T17:51:08.153Z&quot; }" doc:name="Mongo DB"/>
        <object-to-string-transformer doc:name="Object to String"/>
        <logger level="INFO" doc:name="Logger"/>
    </flow>
</mule>

Error I am getting:

Message               : Failed to invoke updateDocumentsByFunction.
Payload               : {NullPayload}
Payload Type          : org.mule.transport.NullPayload
Element               : /deactivateCurrentDocumentsFlow/processors/0 @ test    --------------------------------------------------------------------------------
Root Exception stack trace:
com.mongodb.MongoWriteException: Unknown modifier: $set : {"EffectiveEndDateTime": "2017-01-13T17:51:08.153Z" }

Thanks


Solution

  • You have to use update documents by Functions variant.

    <mongo:update-documents-by-functions config-ref="Mongo_DB"
      collection="myCollection" functions="$set,{"key":123}">
    </mongo:update-documents-by-functions>