1
0
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:
Anna Thomas 2014-09-10 14:13:59 +01:00
parent 54a2923c65
commit bc8acd24ae
4 changed files with 95 additions and 18 deletions

View File

@ -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,

View File

@ -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,6 +121,7 @@ module.exports = function(RED) {
node.clientDb = db;
var coll = db.collection(node.collection);
node.on("input",function(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) {
@ -132,6 +134,28 @@ module.exports = function(RED) {
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);
}
});
}
});
}
});

View File

@ -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,
};

View File

@ -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]);
}
});
});
});