mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
added (some) query capability to MongoDB node.
This commit is contained in:
parent
065d47d173
commit
06930cb65c
@ -27,10 +27,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-help-name="mongodb out">
|
|
||||||
<p>A MongoDB output node.</p>
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
RED.nodes.registerType('mongodb',{
|
RED.nodes.registerType('mongodb',{
|
||||||
category: 'config',
|
category: 'config',
|
||||||
@ -47,11 +43,59 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script type="text/x-red" data-template-name="mongodb out">
|
<script type="text/x-red" data-template-name="mongodb out">
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-mongodb"><i class="icon-tag"></i> Server</label>
|
||||||
|
<input type="text" id="node-input-mongodb">
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-collection"><i class="icon-briefcase"></i> Collection</label>
|
||||||
|
<input type="text" id="node-input-collection" placeholder="collection">
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label> </label>
|
||||||
|
<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>
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
||||||
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
|
</div>
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<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>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>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>
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
RED.nodes.registerType('mongodb out',{
|
||||||
|
category: 'storage-output',
|
||||||
|
color:"rgb(218, 196, 180)",
|
||||||
|
defaults: {
|
||||||
|
mongodb: { type:"mongodb",required:true},
|
||||||
|
name: {value:""},
|
||||||
|
collection: {value:"",required:true},
|
||||||
|
payonly: {value:false}
|
||||||
|
},
|
||||||
|
inputs:1,
|
||||||
|
outputs:0,
|
||||||
|
icon: "mongodb.png",
|
||||||
|
align: "right",
|
||||||
|
label: function() {
|
||||||
|
return this.name||((this.mongodb)?RED.nodes.node(this.mongodb).label()+"//"+this.collection:"mongodb");
|
||||||
|
},
|
||||||
|
labelStyle: function() {
|
||||||
|
return this.name?"node_label_italic":"";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
<script type="text/x-red" data-template-name="mongodb in">
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-mongodb"><i class="icon-tag"></i> Server</label>
|
<label for="node-input-mongodb"><i class="icon-tag"></i> Server</label>
|
||||||
<input type="text" id="node-input-mongodb">
|
<input type="text" id="node-input-mongodb">
|
||||||
@ -64,16 +108,17 @@
|
|||||||
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
|
<label for="node-input-name"><i class="icon-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>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-help-name="mongodb out">
|
<script type="text/x-red" data-help-name="mongodb in">
|
||||||
<p>A MongoDB output node.</p>
|
<p>Queries a MongoDB collection by using the <b>msg.payload</b> to be a MongoDB query statement as per the .find() function.</p>
|
||||||
|
<p>You may also (via a function) set a <b>msg.projection</b> object to constrain the returned fields, a <b>msg.sort</b> object and a <b>msg.limit</b> object.</p>
|
||||||
|
<p>All are optional - see the <a href="http://docs.mongodb.org/manual/reference/method/db.collection.find/" target="new"><i>MongoDB find docs</i></a> for examples.
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
RED.nodes.registerType('mongodb out',{
|
RED.nodes.registerType('mongodb in',{
|
||||||
category: 'storage-output',
|
category: 'storage-input',
|
||||||
color:"rgb(218, 196, 180)",
|
color:"rgb(218, 196, 180)",
|
||||||
defaults: {
|
defaults: {
|
||||||
mongodb: { type:"mongodb",required:true},
|
mongodb: { type:"mongodb",required:true},
|
||||||
@ -81,11 +126,13 @@
|
|||||||
collection: {value:"",required:true},
|
collection: {value:"",required:true},
|
||||||
},
|
},
|
||||||
inputs:1,
|
inputs:1,
|
||||||
outputs:0,
|
outputs:1,
|
||||||
icon: "mongodb.png",
|
icon: "mongodb.png",
|
||||||
align: "right",
|
|
||||||
label: function() {
|
label: function() {
|
||||||
return this.name||this.collection||((this.mongodb)?RED.nodes.node(this.mongodb).label():"mongodb");
|
return this.name||((this.mongodb)?RED.nodes.node(this.mongodb).label()+"//"+this.collection:"mongodb");
|
||||||
|
},
|
||||||
|
labelStyle: function() {
|
||||||
|
return this.name?"node_label_italic":"";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
@ -30,25 +30,26 @@ function MongoOutNode(n) {
|
|||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.collection = n.collection;
|
this.collection = n.collection;
|
||||||
this.mongodb = n.mongodb;
|
this.mongodb = n.mongodb;
|
||||||
|
this.payonly = n.payonly || false;
|
||||||
this.mongoConfig = RED.nodes.getNode(this.mongodb);
|
this.mongoConfig = RED.nodes.getNode(this.mongodb);
|
||||||
|
|
||||||
if (this.mongoConfig) {
|
if (this.mongoConfig) {
|
||||||
var node = this;
|
var node = this;
|
||||||
|
|
||||||
this.clientDb = new mongo.Db(node.mongoConfig.db, new mongo.Server(node.mongoConfig.hostname, node.mongoConfig.port, {}), {w: 1});
|
this.clientDb = new mongo.Db(node.mongoConfig.db, new mongo.Server(node.mongoConfig.hostname, node.mongoConfig.port, {}), {w: 1});
|
||||||
this.clientDb.open(function(err,cli) {
|
this.clientDb.open(function(err,cli) {
|
||||||
if (err) { node.error(err); }
|
if (err) { node.error(err); }
|
||||||
else {
|
else {
|
||||||
node.clientDb.collection(node.collection,function(err,coll) {
|
node.clientDb.collection(node.collection,function(err,coll) {
|
||||||
if (err) { node.error(err); }
|
if (err) { node.error(err); }
|
||||||
else {
|
else {
|
||||||
node.on("input",function(msg) {
|
node.on("input",function(msg) {
|
||||||
delete msg._topic;
|
delete msg._topic;
|
||||||
coll.save(msg,function(err,item){if (err){node.error(err);}});
|
if (node.payonly) coll.save(msg.payload,function(err,item){if (err){node.error(err);}});
|
||||||
});
|
else coll.save(msg,function(err,item){if (err){node.error(err);}});
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.error("missing mongodb configuration");
|
this.error("missing mongodb configuration");
|
||||||
@ -63,4 +64,45 @@ MongoOutNode.prototype.close = function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function MongoInNode(n) {
|
||||||
|
RED.nodes.createNode(this,n);
|
||||||
|
this.collection = n.collection;
|
||||||
|
this.mongodb = n.mongodb;
|
||||||
|
this.mongoConfig = RED.nodes.getNode(this.mongodb);
|
||||||
|
|
||||||
|
if (this.mongoConfig) {
|
||||||
|
var node = this;
|
||||||
|
this.clientDb = new mongo.Db(node.mongoConfig.db, new mongo.Server(node.mongoConfig.hostname, node.mongoConfig.port, {}), {w: 1});
|
||||||
|
this.clientDb.open(function(err,cli) {
|
||||||
|
if (err) { node.error(err); }
|
||||||
|
else {
|
||||||
|
node.clientDb.collection(node.collection,function(err,coll) {
|
||||||
|
if (err) { node.error(err); }
|
||||||
|
else {
|
||||||
|
node.on("input",function(msg) {
|
||||||
|
msg.projection = msg.projection || {};
|
||||||
|
coll.find(msg.payload,msg.projection).sort(msg.sort).limit(msg.limit).toArray(function(err, items) {
|
||||||
|
if (err) { node.error(err); }
|
||||||
|
msg.payload = items;
|
||||||
|
delete msg.projection;
|
||||||
|
delete msg.sort;
|
||||||
|
delete msg.limit;
|
||||||
|
node.send(msg);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.error("missing mongodb configuration");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
RED.nodes.registerType("mongodb in",MongoInNode);
|
||||||
|
|
||||||
|
MongoInNode.prototype.close = function() {
|
||||||
|
if (this.clientDb) {
|
||||||
|
this.clientDb.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user