【问题标题】:using Mule to connect MongoDB使用Mule连接MongoDB
【发布时间】:2017-04-09 23:56:39
【问题描述】:

我已经使用 Mule 获取了一些我的 SQLServer 数据库表,现在我需要在 mongoDB 中创建一个集合。

我能够创建成功的连接,但无法将集合创建到 MongoDB。

下面是我使用的 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:db="http://www.mulesoft.org/schema/mule/db" 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.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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/mongo http://www.mulesoft.org/schema/mule/mongo/current/mule-mongo.xsd">
<db:generic-config name="MicrosoftSQLServerDB"         url="jdbc:sqlserver://localhost:1433;DatabaseName=TESTNAV;user=sa;password=xxxxx" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" doc:name="Generic Database Configuration"/>
<mongo:config name="mymongoconfig"   database="mydb1" doc:name="Mongo DB"/>
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="replicateFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
    <db:select config-ref="MicrosoftSQLServerDB" doc:name="Database">
        <db:parameterized-query><![CDATA[Select * from [Electoral-Mechanicals P Ltd$Customer]]]></db:parameterized-query>
    </db:select>
    <logger message="No. of Records fetched from Table:#[payload.size()]" level="INFO" doc:name="Logger"/>
    <mongo:insert-object  collection="customer" config-ref="mymongoconfig" doc:name="Mongo DB"/>
</flow>
</mule>

在运行应用程序后,即使应用程序状态已部署,但 MongoDB 中没有集合


        • 应用程序 + - - * - - + 域 + - - * - - + 状态 + - - *
  • 复制 * 默认 * 已部署 *


【问题讨论】:

    标签: sql-server mongodb collections mule anypoint-studio


    【解决方案1】:

    根据您拥有的版本,您需要使用 InsertObject 或 InsertDocument。大多数操作名称已更改为符合 Mongo Driver v3,因此如果您有一个 InsertObject 操作,它现在显示为 InsertDocument。

    无论如何,要调用 insertDocument 操作,请在 Mule 中执行以下步骤:

    1. 找到 HTTP 连接器、转换消息和 MongoDB 连接器并将其拖放到 Anypoint Studio 画布上。

    2. 通过选择连接器配置来配置 MongoDB 连接器并选择操作“InsertDocument”来调用它

    3. 单击转换消息并输入要插入的键值

    【讨论】:

    • 嗨@andrescortes,我用于 Anypoint 和 mongoDB 驱动程序的版本没有“InsertDocument”选项。我只能使用“插入对象”选项。请指教。
    【解决方案2】:

    要使用 mule 将文档插入到 mongodb 中的集合,您需要

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

    无论你在使用什么

    <mongo:insert-object> which i guess is not a valid API entity,
    

    【讨论】:

      【解决方案3】:

      您需要显式创建一个集合。您可以使用以下详细信息/示例来创建新集合。

      创建收藏

      '<mongo:create-collection>'
      

      创建一个新集合。如果集合已经存在,则 抛出 MongoException。

      XML 示例

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

      或者,

      如果您决定首先检查是否存在同名的集合,并且您不想搞砸任何事情。您可以使用以下内容:

      现有收藏

      <mongo:exists-collection>
      

      如果集合存在,则给出其名称的答案 XML 示例

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

      【讨论】:

      • 嗨 Satish,感谢您的回复....我已经手动将数据库和集合创建到 mongoDB 中,其名称与我在代码中使用的名称相同。但事情还是一样.. customer 集合 下没有反映到 mongoDB 的数据
      • 您能否使用 来确保您的代码能够轮询/访问您创建的集合。
      • 您是否遇到任何错误,或者您的代码执行良好但没有任何内容插入到集合中。
      • 要插入一个文档,我猜你需要用户插入文档标签。 请使用它,看看这是否有什么不同。
      • 实际上我没有看到任何名为 的 API 将文档插入到您需要使用的 mongodb
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2015-05-06
      相关资源
      最近更新 更多