mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Added count and aggregate operations to MongoDB In node
This commit is contained in:
parent
54a2923c65
commit
bc8acd24ae
@ -69,7 +69,7 @@
|
||||
<label for="node-input-collection"><i class="fa fa-briefcase"></i> Collection</label>
|
||||
<input type="text" id="node-input-collection" placeholder="collection">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<div class="form-row">
|
||||
<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;">
|
||||
<option value="store">save</option>
|
||||
@ -141,6 +141,14 @@
|
||||
<label for="node-input-collection"><i class="fa fa-briefcase"></i> Collection</label>
|
||||
<input type="text" id="node-input-collection" placeholder="collection">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<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;">
|
||||
<option value="find">find</option>
|
||||
<option value="count">count</option>
|
||||
<option value="aggregate">aggregate</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||
<input type="text" id="node-input-name" placeholder="Name">
|
||||
@ -148,9 +156,12 @@
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="mongodb in">
|
||||
<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.</p>
|
||||
<p>Calls a MongoDB collection method based on the selected operator.</p>
|
||||
<p>Find queries a collection using the <b>msg.payload</b> as the query statement as per the .find() function. Optionally, 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>Count returns a count of the number of documents in a collection or matching a query using the <b>msg.payload</b> as the query statement.</p>
|
||||
<p>Aggregate provides access to the aggregation pipeline using the <b>msg.payload</b> as the pipeline array.</p>
|
||||
<p>See the <a href="http://docs.mongodb.org/manual/reference/method/db.collection.find/" target="new"><i>MongoDB collection methods docs</i></a> for examples.</p>
|
||||
<p>The result is returned in <b>msg.payload</b>.</p>
|
||||
</script>
|
||||
|
||||
<script type="text/javascript">
|
||||
@ -160,7 +171,8 @@
|
||||
defaults: {
|
||||
mongodb: { type:"mongodb",required:true},
|
||||
name: {value:""},
|
||||
collection: {value:"",required:true}
|
||||
collection: {value:"",required:true},
|
||||
operation: {value:"find"}
|
||||
},
|
||||
inputs:1,
|
||||
outputs:1,
|
||||
|
@ -41,7 +41,7 @@ module.exports = function(RED) {
|
||||
password: {type: "password"}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
function MongoOutNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.collection = n.collection;
|
||||
@ -109,6 +109,7 @@ module.exports = function(RED) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.collection = n.collection;
|
||||
this.mongodb = n.mongodb;
|
||||
this.operation = n.operation || "find";
|
||||
this.mongoConfig = RED.nodes.getNode(this.mongodb);
|
||||
|
||||
if (this.mongoConfig) {
|
||||
@ -120,18 +121,41 @@ module.exports = function(RED) {
|
||||
node.clientDb = db;
|
||||
var coll = db.collection(node.collection);
|
||||
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);
|
||||
} else {
|
||||
msg.payload = items;
|
||||
delete msg.projection;
|
||||
delete msg.sort;
|
||||
delete msg.limit;
|
||||
node.send(msg);
|
||||
}
|
||||
});
|
||||
if (node.operation === "find") {
|
||||
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);
|
||||
} else {
|
||||
msg.payload = items;
|
||||
delete msg.projection;
|
||||
delete msg.sort;
|
||||
delete msg.limit;
|
||||
node.send(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (node.operation === "count") {
|
||||
coll.count(msg.payload, function(err, count) {
|
||||
if (err) {
|
||||
node.error(err);
|
||||
} else {
|
||||
msg.payload = count;
|
||||
node.send(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (node.operation === "aggregate") {
|
||||
msg.payload = (msg.payload instanceof Array) ? msg.payload : [];
|
||||
coll.aggregate(msg.payload, function(err, result) {
|
||||
if (err) {
|
||||
node.error(err);
|
||||
} else {
|
||||
msg.payload = result;
|
||||
node.send(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
12
red/util.js
12
red/util.js
@ -25,7 +25,19 @@ function ensureString(o) {
|
||||
return ""+o;
|
||||
}
|
||||
|
||||
function ensureBuffer(o) {
|
||||
if (Buffer.isBuffer(o)) {
|
||||
return o;
|
||||
} else if (typeof o === "object") {
|
||||
o = JSON.stringify(o);
|
||||
} else if (typeof o !== "string") {
|
||||
o = ""+o;
|
||||
}
|
||||
return new Buffer(o);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ensureString: ensureString,
|
||||
ensureBuffer: ensureBuffer,
|
||||
};
|
||||
|
||||
|
@ -37,5 +37,34 @@ describe("red/util", function() {
|
||||
s.should.equal('123');
|
||||
});
|
||||
});
|
||||
|
||||
describe('ensureBuffer', function() {
|
||||
it('Buffers are preserved', function() {
|
||||
var b = new Buffer('');
|
||||
util.ensureBuffer(b).should.equal(b);
|
||||
});
|
||||
it('string is converted', function() {
|
||||
var b = util.ensureBuffer('foo');
|
||||
var expected = new Buffer('foo');
|
||||
for (var i = 0; i < expected.length; i++) {
|
||||
b[i].should.equal(expected[i]);
|
||||
}
|
||||
Buffer.isBuffer(b).should.equal(true);
|
||||
});
|
||||
it('Object is converted to JSON', function() {
|
||||
var obj = {foo: "bar"}
|
||||
var b = util.ensureBuffer(obj);
|
||||
Buffer.isBuffer(b).should.equal(true);
|
||||
should.deepEqual(JSON.parse(b), obj);
|
||||
});
|
||||
it('stringifies other things', function() {
|
||||
var b = util.ensureBuffer(123);
|
||||
Buffer.isBuffer(b).should.equal(true);
|
||||
var expected = new Buffer('123');
|
||||
for (var i = 0; i < expected.length; i++) {
|
||||
b[i].should.equal(expected[i]);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user