【问题标题】:Can you update all Mongo documents in a collection at once in Mule?您可以在 Mule 中一次更新集合中的所有 Mongo 文档吗?
【发布时间】:2017-01-13 19:05:29
【问题描述】:

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

如何配置变压器来做到这一点? Query Reference 字段信息消息讨论了一个 Id,并且元素字段看起来需要一个包含将替换正在更新的文档的有效负载。我只是想更新一个字段,目前我的查询出现错误,但它确实适用于 mongoDB 命令行。

这是我要实现的查询:

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

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

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

谢谢

【问题讨论】:

    标签: mongodb mule anypoint-studio


    【解决方案1】:

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

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

    【讨论】:

    • 我无法使上述工作。我已经添加了我的代码并尝试了许多变体,但到目前为止还没有运气。你能看出我哪里出错了吗?我正在使用 mongoDB 4.1 连接器
    • 它的 update-documents-by-functions 不是 update-documents-by-function。看看这是否有帮助,我会同时看看。 args 也一样。从function 更改为functions
    • 设法让它与这个
    • 非常感谢您的帮助。我不会考虑看函数。
    猜你喜欢
    • 1970-01-01
    • 2018-12-31
    • 2021-08-16
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    相关资源
    最近更新 更多