🎉 🎉 RAPIDMINER 9.10 IS OUT!!! 🎉🎉

Download the latest version helping analytics teams accelerate time-to-value for streaming and IIOT use cases.

CLICK HERE TO DOWNLOAD

"MongoDB append to document array (moved from Getting Started section)"

mrmikevmrmikev Member Posts: 13 Contributor I
edited June 2019 in Help
I'm attempting to add an item to an array within a document in MongoDB.  For example, here's the initial document.

{
    "_id" : ObjectId("55bfa1619b527d2ffb37439e"),
    "modelName": "Model Sample A1",
    "modelResults" : [
        {
            "resultID" : "1",
            "name" : "Result 1 Name",
            "resultStuff" : "1000"
        },
        {
            "resultID" : "2",
            "name" : "Result 2 Name",
            "resultStuff" : "2000"
        },
        {
            "resultID" : "3",
            "name" : "Result 3 Name",
            "resultStuff" : "3000"
        },
        {
            "resultID" : "4",
            "name" : "Result 4 Name",
            "resultStuff" : "4000"
        }
    ]
}
I want to add this item to the modelResults array:

{
    "resultID" : "6",
    "name" : "Result 6 Name",
    "resultStuff" : "6000"
}
The command in MongoDB (outside of RapidMiner) would be:

db.mvstuff.update({modelName:"Model Sample A1"},{$push : {modelResults : {
    "resultID" : "6",
    "name" : "Result 6 Name",
    "resultStuff" : "6000"
} }})
I want the result to look like this without having to read in the entire modelResults array and append the new item to it in RapidMiner and write the entire modelResults array back to MongoDB.:
{
    "_id" : ObjectId("55bfa1619b527d2ffb37439e"),
    "modelName": "Model Sample A1",
    "modelResults" : [
        {
            "resultID" : "1",
            "name" : "Result 1 Name",
            "resultStuff" : "1000"
        },
        {
            "resultID" : "2",
            "name" : "Result 2 Name",
            "resultStuff" : "2000"
        },
        {
            "resultID" : "3",
            "name" : "Result 3 Name",
            "resultStuff" : "3000"
        },
        {
            "resultID" : "4",
            "name" : "Result 4 Name",
            "resultStuff" : "4000"
        },
        {
            "resultID" : "6",
            "name" : "Result 6 Name",
            "resultStuff" : "6000"
        }
    ]
}
Here's an example process, but I've not been able to get the desired results:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<process version="6.4.000">
  <context>
    <input/>
    <output/>
    <macros/>
  </context>
  <operator activated="true" class="process" compatibility="6.4.000" expanded="true" name="Process">
    <process expanded="true">
      <operator activated="true" class="text:create_document" compatibility="6.4.001" expanded="true" height="60" name="Create Document" width="90" x="45" y="30">
        <parameter key="text" value="{&quot;modelName&quot;: &quot;Model Sample A1&quot;}"/>
        <description align="center" color="transparent" colored="false" width="126">set up criteria</description>
      </operator>
      <operator activated="true" class="multiply" compatibility="6.4.000" expanded="true" height="94" name="Multiply" width="90" x="179" y="30"/>
      <operator activated="true" class="nosql:mongodb_document_remover" compatibility="6.4.000" expanded="true" height="76" name="Delete MongoDB" width="90" x="313" y="120">
        <parameter key="mongodb_instance" value="aws_rms_dev"/>
        <parameter key="collection" value="mvstuff"/>
        <parameter key="require_id" value="false"/>
        <description align="center" color="transparent" colored="false" width="126">clean up the result of the previous try</description>
      </operator>
      <operator activated="true" class="text:create_document" compatibility="6.4.001" expanded="true" height="60" name="Create Document (3)" width="90" x="514" y="120">
        <parameter key="text" value="{    &quot;modelName&quot;: &quot;Model Sample A1&quot;,    &quot;modelResults&quot; : [        {            &quot;resultID&quot; : &quot;1&quot;,            &quot;name&quot; : &quot;Result 1 Name&quot;,            &quot;resultStuff&quot; : &quot;1000&quot;        },        {            &quot;resultID&quot; : &quot;2&quot;,            &quot;name&quot; : &quot;Result 2 Name&quot;,            &quot;resultStuff&quot; : &quot;2000&quot;        },        {            &quot;resultID&quot; : &quot;3&quot;,            &quot;name&quot; : &quot;Result 3 Name&quot;,            &quot;resultStuff&quot; : &quot;3000&quot;        },        {            &quot;resultID&quot; : &quot;4&quot;,            &quot;name&quot; : &quot;Result 4 Name&quot;,            &quot;resultStuff&quot; : &quot;4000&quot;        }    ]}"/>
      </operator>
      <operator activated="true" class="nosql:mongodb_document_writer" compatibility="6.4.000" expanded="true" height="76" name="Write MongoDB" width="90" x="648" y="120">
        <parameter key="mongodb_instance" value="aws_rms_dev"/>
        <parameter key="collection" value="mvstuff"/>
      </operator>
      <operator activated="true" class="text:create_document" compatibility="6.4.001" expanded="true" height="60" name="Create Document (2)" width="90" x="782" y="120">
        <parameter key="text" value="{&quot;modelResults&quot; :  [ { &quot;resultID&quot; : &quot;6&quot;, &quot;name&quot; : &quot;Result 6 Name&quot;, &quot;resultStuff&quot; : &quot;6000&quot;}]}"/>
        <description align="center" color="transparent" colored="false" width="126">set up item to add to array</description>
      </operator>
      <operator activated="true" class="nosql:mongodb_document_updater" compatibility="6.4.000" expanded="true" height="76" name="Update MongoDB" width="90" x="916" y="30">
        <parameter key="mongodb_instance" value="aws_rms_dev"/>
        <parameter key="collection" value="mvstuff"/>
      </operator>
      <operator activated="true" class="nosql:mongodb_document_reader" compatibility="6.4.000" expanded="true" height="94" name="Read MongoDB (2)" width="90" x="1050" y="30">
        <parameter key="mongodb_instance" value="aws_rms_dev"/>
        <parameter key="collection" value="mvstuff"/>
        <description align="center" color="transparent" colored="false" width="126">get the doc to review the results</description>
      </operator>
      <connect from_op="Create Document" from_port="output" to_op="Multiply" to_port="input"/>
      <connect from_op="Multiply" from_port="output 1" to_op="Update MongoDB" to_port="criteria"/>
      <connect from_op="Multiply" from_port="output 2" to_op="Delete MongoDB" to_port="documents 1"/>
      <connect from_op="Create Document (3)" from_port="output" to_op="Write MongoDB" to_port="documents 1"/>
      <connect from_op="Create Document (2)" from_port="output" to_op="Update MongoDB" to_port="update"/>
      <connect from_op="Update MongoDB" from_port="criteria_pass_through" to_op="Read MongoDB (2)" to_port="criteria"/>
      <connect from_op="Read MongoDB (2)" from_port="collection" to_port="result 1"/>
      <portSpacing port="source_input 1" spacing="0"/>
      <portSpacing port="sink_result 1" spacing="0"/>
      <portSpacing port="sink_result 2" spacing="0"/>
      <description align="center" color="green" colored="true" height="72" resized="true" width="270" x="496" y="215">insert the initial document</description>
    </process>
  </operator>
</process>
Thanks for your help and comments!

Sorry, I had accidentally posted this question under Getting Started.  Problems and Support is more appropriate.
Sign In or Register to comment.