mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Added update functionality to mongodb out node
This commit is contained in:
parent
abd3d752f5
commit
4f2e4b58e4
@ -72,41 +72,65 @@
|
|||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-operation"><i class="fa fa-wrench"></i> Operation</label>
|
<label for="node-input-operation"><i class="fa fa-wrench"></i> Operation</label>
|
||||||
<select type="text" id="node-input-operation" style="display: inline-block; vertical-align: top;">
|
<select type="text" id="node-input-operation" style="display: inline-block; vertical-align: top;">
|
||||||
<option value="store">save</option>
|
<option value="save">save</option>
|
||||||
<option value="insert">insert</option>
|
<option value="insert">insert</option>
|
||||||
<!-- <option value="update">update</option> -->
|
<option value="update">update</option>
|
||||||
<option value="delete">remove</option>
|
<option value="delete">remove</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row node-input-payonly">
|
<div class="form-row node-input-payonly">
|
||||||
<label> </label>
|
<label> </label>
|
||||||
<input type="checkbox" id="node-input-payonly" placeholder="Only" style="display: inline-block; width: auto; vertical-align: top;">
|
<input type="checkbox" id="node-input-payonly" placeholder="Only" style="display: inline-block; width: auto; vertical-align: top;">
|
||||||
<label for="node-input-payonly" style="width: 70%;">Only store msg.payload object ?</label>
|
<label for="node-input-payonly" style="width: 70%;">Only store msg.payload object</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-row node-input-upsert">
|
||||||
|
<label> </label>
|
||||||
|
<input type="checkbox" id="node-input-upsert" placeholder="Only" style="display: inline-block; width: auto; vertical-align: top;">
|
||||||
|
<label for="node-input-upsert" style="width: 70%;">Create a new document if no match found</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-row node-input-multi">
|
||||||
|
<label> </label>
|
||||||
|
<input type="checkbox" id="node-input-multi" placeholder="Only" style="display: inline-block; width: auto; vertical-align: top;;">
|
||||||
|
<label for="node-input-multi" style="width: 70%;">Update all matching documents</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||||
<input type="text" id="node-input-name" placeholder="Name">
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
</div>
|
</div>
|
||||||
<script>
|
|
||||||
$("#node-input-operation").change(function() {
|
|
||||||
var id = $("#node-input-operation option:selected").val();
|
|
||||||
if (id == "delete") $(".node-input-payonly").hide();
|
|
||||||
else $(".node-input-payonly").show();
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-help-name="mongodb out">
|
<script type="text/x-red" data-help-name="mongodb out">
|
||||||
<p>A simple MongoDB output node. Stores the <b>msg</b> object in a chosen collection.</p>
|
<p>A simple MongoDB output node. Can save, insert, update and remove objects from a chosen collection.</p>
|
||||||
|
<p>Save will update an existing object or insert a new object if one does not already exist.</p>
|
||||||
|
<p>Insert will insert a new object.</p>
|
||||||
|
<p>Save and insert either store <b>msg</b> or <b>msg.payload</b>.</p>
|
||||||
|
<p>Update will modify an existing object or objects. The query to select objects to update uses <b>msg.query</b> and the update to the element uses <b>msg.payload</b>.</p>
|
||||||
|
<p>Update can add a object if it does not exist or update multiple objects.</p>
|
||||||
|
<p>Remove will remove objects that match the query passed in on <b>msg.payload</b>. A blank query will delete <i>all of the objects</i> in the collection.</p>
|
||||||
<p>By default MongoDB creates an <i>_id</i> property as the primary key - so repeated injections of the same <b>msg</b> will result in many database entries.</p>
|
<p>By default MongoDB creates an <i>_id</i> property as the primary key - so repeated injections of the same <b>msg</b> will result in many database entries.</p>
|
||||||
<p>If this is NOT the desired behaviour - ie you want repeated entries to overwrite, then you must set the <b>msg._id</b> property to be a constant by the use of a previous function node.</p>
|
<p>If this is NOT the desired behaviour - ie. you want repeated entries to overwrite, then you must set the <b>msg._id</b> property to be a constant by the use of a previous function node.</p>
|
||||||
<p>This could be a unique constant or you could create one based on some other msg property.</p>
|
<p>This could be a unique constant or you could create one based on some other msg property.</p>
|
||||||
<p>Currently we do not limit or cap the collection size at all... this may well change.</p>
|
<p>Currently we do not limit or cap the collection size at all... this may well change.</p>
|
||||||
<p>You can also choose to <b>remove</b> items. To do so the <b>msg.payload</b> <i>MUST</i> contain an object that will select the items(s) to remove.
|
|
||||||
A blank object will delete <i>all of the objects</i> in the collection.</p>
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
|
||||||
|
function oneditprepare() {
|
||||||
|
$("#node-input-operation").change(function () {
|
||||||
|
var id = $("#node-input-operation option:selected").val();
|
||||||
|
|
||||||
|
if (id === "update") {
|
||||||
|
$(".node-input-payonly").hide();
|
||||||
|
$(".node-input-upsert, .node-input-multi").show();
|
||||||
|
} else if (id == "delete") {
|
||||||
|
$(".node-input-payonly, .node-input-upsert, .node-input-multi").hide();
|
||||||
|
} else {
|
||||||
|
$(".node-input-payonly").show();
|
||||||
|
$(".node-input-upsert, .node-input-multi").hide();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
RED.nodes.registerType('mongodb out',{
|
RED.nodes.registerType('mongodb out',{
|
||||||
category: 'storage-output',
|
category: 'storage-output',
|
||||||
color:"rgb(218, 196, 180)",
|
color:"rgb(218, 196, 180)",
|
||||||
@ -115,7 +139,9 @@
|
|||||||
name: {value: ""},
|
name: {value: ""},
|
||||||
collection: {value: "", required: true},
|
collection: {value: "", required: true},
|
||||||
payonly: {value: false},
|
payonly: {value: false},
|
||||||
operation: {value:"store"}
|
upsert: {value: false},
|
||||||
|
multi: {value: false},
|
||||||
|
operation: {value: "save"}
|
||||||
},
|
},
|
||||||
inputs:1,
|
inputs:1,
|
||||||
outputs:0,
|
outputs:0,
|
||||||
@ -127,7 +153,8 @@
|
|||||||
},
|
},
|
||||||
labelStyle: function() {
|
labelStyle: function() {
|
||||||
return this.name?"node_label_italic":"";
|
return this.name?"node_label_italic":"";
|
||||||
}
|
},
|
||||||
|
oneditprepare: oneditprepare
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -47,6 +47,8 @@ module.exports = function(RED) {
|
|||||||
this.collection = n.collection;
|
this.collection = n.collection;
|
||||||
this.mongodb = n.mongodb;
|
this.mongodb = n.mongodb;
|
||||||
this.payonly = n.payonly || false;
|
this.payonly = n.payonly || false;
|
||||||
|
this.upsert = n.upsert || false;
|
||||||
|
this.multi = n.multi || false;
|
||||||
this.operation = n.operation;
|
this.operation = n.operation;
|
||||||
this.mongoConfig = RED.nodes.getNode(this.mongodb);
|
this.mongoConfig = RED.nodes.getNode(this.mongodb);
|
||||||
|
|
||||||
@ -59,7 +61,7 @@ module.exports = function(RED) {
|
|||||||
node.clientDb = db;
|
node.clientDb = db;
|
||||||
var coll = db.collection(node.collection);
|
var coll = db.collection(node.collection);
|
||||||
node.on("input",function(msg) {
|
node.on("input",function(msg) {
|
||||||
if (node.operation == "store") {
|
if (node.operation === "save") {
|
||||||
delete msg._topic;
|
delete msg._topic;
|
||||||
if (node.payonly) {
|
if (node.payonly) {
|
||||||
if (typeof msg.payload !== "object") { msg.payload = {"payload":msg.payload}; }
|
if (typeof msg.payload !== "object") { msg.payload = {"payload":msg.payload}; }
|
||||||
@ -68,7 +70,7 @@ module.exports = function(RED) {
|
|||||||
coll.save(msg,function(err,item){if (err){node.error(err);}});
|
coll.save(msg,function(err,item){if (err){node.error(err);}});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (node.operation == "insert") {
|
else if (node.operation === "insert") {
|
||||||
delete msg._topic;
|
delete msg._topic;
|
||||||
if (node.payonly) {
|
if (node.payonly) {
|
||||||
if (typeof msg.payload !== "object") { msg.payload = {"payload":msg.payload}; }
|
if (typeof msg.payload !== "object") { msg.payload = {"payload":msg.payload}; }
|
||||||
@ -77,17 +79,26 @@ module.exports = function(RED) {
|
|||||||
coll.insert(msg,function(err,item){if (err){node.error(err);}});
|
coll.insert(msg,function(err,item){if (err){node.error(err);}});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (node.operation == "update") {
|
else if (node.operation === "update") {
|
||||||
delete msg._topic;
|
var query = msg.query || {};
|
||||||
if (node.payonly) {
|
var payload = msg.payload || {};
|
||||||
if (typeof msg.payload !== "object") { msg.payload = {"payload":msg.payload}; }
|
var options = {
|
||||||
coll.update(msg.payload,function(err,item){ if (err){node.error(err);} });
|
upsert: node.upsert,
|
||||||
} else {
|
multi: node.multi
|
||||||
coll.update(msg,function(err,item){if (err){node.error(err);}});
|
};
|
||||||
|
|
||||||
|
coll.update(query, payload, options, function(err, item) {
|
||||||
|
if (err) {
|
||||||
|
node.error(err);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (node.operation == "delete") {
|
if (node.operation == "delete") {
|
||||||
coll.remove(msg.payload, {w:1}, function(err, items){ if (err) { node.error(err); } });
|
coll.remove(msg.payload, function(err, items) {
|
||||||
|
if (err) {
|
||||||
|
node.error(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user