mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Merge pull request #401 from anna2130/master
Added count and aggregate operations to MongoDB In node
This commit is contained in:
		@@ -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);
 | 
			
		||||
                                }
 | 
			
		||||
                            });
 | 
			
		||||
                        }
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user