你能在Mule中一次更新集合中的所有Mongo文档吗?

时间:2017-01-13 19:05:30

标签: mongodb mule anypoint-studio

我正在尝试使用当前日期更新mongoDB集合中的生效日期。我想更新生效日期为空的集合中的所有文档。

如何配置变压器来执行此操作?查询引用字段信息消息讨论Id,元素字段看起来需要包含将替换正在更新的文档的文档。我只是想更新一个字段,当前我的查询出错,但它确实可以在mongoDB命令行上运行。

这是我想要实现的查询:

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

到目前为止,我的XML在Mule中为Mongo配置:

<?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>

我收到错误:

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" }

由于

1 个答案:

答案 0 :(得分:1)

您必须使用Functions变体的更新文档。

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