"MongoDB append to document array (moved from Getting Started section)"
mrmikev
New Altair Community Member
I'm attempting to add an item to an array within a document in MongoDB. For example, here's the initial document.
Sorry, I had accidentally posted this question under Getting Started. Problems and Support is more appropriate.
I want to add this item to the modelResults array:
{
"_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"
}
]
}
The command in MongoDB (outside of RapidMiner) would be:
{
"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.:
db.mvstuff.update({modelName:"Model Sample A1"},{$push : {modelResults : {
"resultID" : "6",
"name" : "Result 6 Name",
"resultStuff" : "6000"
} }})
{Here's an example process, but I've not been able to get the desired results:
"_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"
}
]
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>Thanks for your help and comments!
<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="{"modelName": "Model Sample A1"}"/>
<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="{ "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" } ]}"/>
</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="{"modelResults" : [ { "resultID" : "6", "name" : "Result 6 Name", "resultStuff" : "6000"}]}"/>
<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>
Sorry, I had accidentally posted this question under Getting Started. Problems and Support is more appropriate.
Tagged:
0