mirror of
				https://github.com/node-red/node-red-nodes.git
				synced 2025-03-01 10:37:43 +00:00 
			
		
		
		
	Add fineOne, fix insertMany
This commit is contained in:
		@@ -126,12 +126,12 @@
 | 
				
			|||||||
        <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" data-i18n="mongodb.operation.save"></option>
 | 
					            <option value="store" data-i18n="mongodb.operation.save"></option>
 | 
				
			||||||
            <option value="insert" data-i18n="mongodb.operation.insert"></option>
 | 
					            <option value="insert" data-i18n="mongodb.operation.insert"></option>
 | 
				
			||||||
            <option value="update" data-i18n="mongodb.operation.update"></option>
 | 
					 | 
				
			||||||
            <option value="delete" data-i18n="mongodb.operation.remove"></option>
 | 
					 | 
				
			||||||
            <option value="insertOne" data-i18n="mongodb.operation.insertOne"></option>
 | 
					            <option value="insertOne" data-i18n="mongodb.operation.insertOne"></option>
 | 
				
			||||||
            <option value="insertMany" data-i18n="mongodb.operation.insertMany"></option>
 | 
					            <option value="insertMany" data-i18n="mongodb.operation.insertMany"></option>
 | 
				
			||||||
 | 
					            <option value="update" data-i18n="mongodb.operation.update"></option>
 | 
				
			||||||
            <option value="updateOne" data-i18n="mongodb.operation.updateOne"></option>
 | 
					            <option value="updateOne" data-i18n="mongodb.operation.updateOne"></option>
 | 
				
			||||||
            <option value="updateMany" data-i18n="mongodb.operation.updateMany"></option>
 | 
					            <option value="updateMany" data-i18n="mongodb.operation.updateMany"></option>
 | 
				
			||||||
 | 
					            <option value="delete" data-i18n="mongodb.operation.remove"></option>
 | 
				
			||||||
            <option value="deleteOne" data-i18n="mongodb.operation.deleteOne"></option>
 | 
					            <option value="deleteOne" data-i18n="mongodb.operation.deleteOne"></option>
 | 
				
			||||||
            <option value="deleteMany" data-i18n="mongodb.operation.deleteMany"></option>
 | 
					            <option value="deleteMany" data-i18n="mongodb.operation.deleteMany"></option>
 | 
				
			||||||
        </select>
 | 
					        </select>
 | 
				
			||||||
@@ -156,6 +156,7 @@
 | 
				
			|||||||
        <input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
 | 
					        <input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
    <div class="form-tips" id="node-warning" style="display: none"><span data-i18n="[html]mongodb.tip"></span></div>
 | 
					    <div class="form-tips" id="node-warning" style="display: none"><span data-i18n="[html]mongodb.tip"></span></div>
 | 
				
			||||||
 | 
					    <div class="form-tips" id="node-insertMany-warning" style="display: none"><span data-i18n="[html]mongodb.insertMany-tips"></span></div>
 | 
				
			||||||
</script>
 | 
					</script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<script type="text/html" data-help-name="mongodb out">
 | 
					<script type="text/html" data-help-name="mongodb out">
 | 
				
			||||||
@@ -190,15 +191,24 @@
 | 
				
			|||||||
        $("#node-input-operation").change(function () {
 | 
					        $("#node-input-operation").change(function () {
 | 
				
			||||||
            var id = $("#node-input-operation option:selected").val();
 | 
					            var id = $("#node-input-operation option:selected").val();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (id === "update" || id === "updateOne" || id === "updateMany") {
 | 
					            if (id === "update") {
 | 
				
			||||||
                $(".node-input-payonly").hide();
 | 
					                $(".node-input-payonly").hide();
 | 
				
			||||||
                $(".node-input-upsert, .node-input-multi").show();
 | 
					                $(".node-input-upsert, .node-input-multi").show();
 | 
				
			||||||
 | 
					            } else if (id === "updateOne" || id === "updateMany") {
 | 
				
			||||||
 | 
					                $(".node-input-payonly, .node-input-multi").hide();
 | 
				
			||||||
 | 
					                $(".node-input-upsert").show();
 | 
				
			||||||
            } else if (id === "delete" || id === "deleteOne" || id === "deleteMany") {
 | 
					            } else if (id === "delete" || id === "deleteOne" || id === "deleteMany") {
 | 
				
			||||||
                $(".node-input-payonly, .node-input-upsert, .node-input-multi").hide();
 | 
					                $(".node-input-payonly, .node-input-upsert, .node-input-multi").hide();
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                $(".node-input-payonly").show();
 | 
					                $(".node-input-payonly").show();
 | 
				
			||||||
                $(".node-input-upsert, .node-input-multi").hide();
 | 
					                $(".node-input-upsert, .node-input-multi").hide();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (id === "insertMany") {
 | 
				
			||||||
 | 
					                $("#node-insertMany-warning").show();
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                $("#node-insertMany-warning").hide();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $("#node-input-collection").change(function () {
 | 
					        $("#node-input-collection").change(function () {
 | 
				
			||||||
@@ -250,6 +260,7 @@
 | 
				
			|||||||
    <div class="form-row">
 | 
					    <div class="form-row">
 | 
				
			||||||
        <label for="node-input-operation"><i class="fa fa-wrench"></i> <span data-i18n="mongodb.label.operation"></span></label>
 | 
					        <label for="node-input-operation"><i class="fa fa-wrench"></i> <span data-i18n="mongodb.label.operation"></span></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="findOne" data-i18n="mongodb.operation.findOne"></option>
 | 
				
			||||||
            <option value="find" data-i18n="mongodb.operation.find"></option>
 | 
					            <option value="find" data-i18n="mongodb.operation.find"></option>
 | 
				
			||||||
            <option value="count" data-i18n="mongodb.operation.count"></option>
 | 
					            <option value="count" data-i18n="mongodb.operation.count"></option>
 | 
				
			||||||
            <option value="aggregate" data-i18n="mongodb.operation.aggregate"></option>
 | 
					            <option value="aggregate" data-i18n="mongodb.operation.aggregate"></option>
 | 
				
			||||||
@@ -267,6 +278,9 @@
 | 
				
			|||||||
    <p>Find queries a collection using the <code>msg.payload</code> as the query statement as per the .find() function.
 | 
					    <p>Find queries a collection using the <code>msg.payload</code> as the query statement as per the .find() function.
 | 
				
			||||||
    Optionally, you may also (via a function) set a <code>msg.projection</code> object to constrain the returned
 | 
					    Optionally, you may also (via a function) set a <code>msg.projection</code> object to constrain the returned
 | 
				
			||||||
    fields, a <code>msg.sort</code> object, a <code>msg.limit</code> number and a <code>msg.skip</code> number.</p>
 | 
					    fields, a <code>msg.sort</code> object, a <code>msg.limit</code> number and a <code>msg.skip</code> number.</p>
 | 
				
			||||||
 | 
					    <p>FindOne queries a collection using the <code>msg.payload</code> as the query statement and returns a single object.
 | 
				
			||||||
 | 
					    Optionally, you may also (via a function) set a <code>msg.projection</code> object to constrain the returned
 | 
				
			||||||
 | 
					    fields, a <code>msg.sort</code> object, and a <code>msg.skip</code> number.</p>
 | 
				
			||||||
    <p>Count returns a count of the number of documents in a collection or matching a query using the
 | 
					    <p>Count returns a count of the number of documents in a collection or matching a query using the
 | 
				
			||||||
    <code>msg.payload</code> as the query statement.</p>
 | 
					    <code>msg.payload</code> as the query statement.</p>
 | 
				
			||||||
    <p>Aggregate provides access to the aggregation pipeline using the <code>msg.payload</code> as the pipeline array.</p>
 | 
					    <p>Aggregate provides access to the aggregation pipeline using the <code>msg.payload</code> as the pipeline array.</p>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -154,7 +154,7 @@ module.exports = function(RED) {
 | 
				
			|||||||
                        else if (node.operation === "insertMany") {
 | 
					                        else if (node.operation === "insertMany") {
 | 
				
			||||||
                            const options = {}
 | 
					                            const options = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            coll.insertMany(msg.payload, options, function(err,item) {
 | 
					                            coll.insertMany(msg.payload.values, options, function(err,items) {
 | 
				
			||||||
                                if (err) {
 | 
					                                if (err) {
 | 
				
			||||||
                                    node.error(err,msg);
 | 
					                                    node.error(err,msg);
 | 
				
			||||||
                                }
 | 
					                                }
 | 
				
			||||||
@@ -215,8 +215,7 @@ module.exports = function(RED) {
 | 
				
			|||||||
                            var query = msg.query || {};
 | 
					                            var query = msg.query || {};
 | 
				
			||||||
                            var payload = msg.payload || {};
 | 
					                            var payload = msg.payload || {};
 | 
				
			||||||
                            var options = {
 | 
					                            var options = {
 | 
				
			||||||
                                upsert: node.upsert,
 | 
					                                upsert: node.upsert
 | 
				
			||||||
                                multi: node.multi
 | 
					 | 
				
			||||||
                            };
 | 
					                            };
 | 
				
			||||||
                            if (ObjectID.isValid(msg.query._id)) {
 | 
					                            if (ObjectID.isValid(msg.query._id)) {
 | 
				
			||||||
                                msg.query._id = new ObjectID(msg.query._id);
 | 
					                                msg.query._id = new ObjectID(msg.query._id);
 | 
				
			||||||
@@ -231,8 +230,7 @@ module.exports = function(RED) {
 | 
				
			|||||||
                            var query = msg.query || {};
 | 
					                            var query = msg.query || {};
 | 
				
			||||||
                            var payload = msg.payload || [];
 | 
					                            var payload = msg.payload || [];
 | 
				
			||||||
                            var options = {
 | 
					                            var options = {
 | 
				
			||||||
                                upsert: node.upsert,
 | 
					                                upsert: node.upsert
 | 
				
			||||||
                                multi: node.multi
 | 
					 | 
				
			||||||
                            };
 | 
					                            };
 | 
				
			||||||
                            if (ObjectID.isValid(msg.query._id)) {
 | 
					                            if (ObjectID.isValid(msg.query._id)) {
 | 
				
			||||||
                                msg.query._id = new ObjectID(msg.query._id);
 | 
					                                msg.query._id = new ObjectID(msg.query._id);
 | 
				
			||||||
@@ -345,6 +343,34 @@ module.exports = function(RED) {
 | 
				
			|||||||
                                }
 | 
					                                }
 | 
				
			||||||
                            });
 | 
					                            });
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
 | 
					                        else if (node.operation === "findOne") {
 | 
				
			||||||
 | 
					                            selector = ensureValidSelectorObject(msg.payload);
 | 
				
			||||||
 | 
					                            var skip = msg.skip;
 | 
				
			||||||
 | 
					                            if (typeof skip === "string" && !isNaN(skip)) {
 | 
				
			||||||
 | 
					                                skip = Number(skip);
 | 
				
			||||||
 | 
					                            } else if (typeof skip === "undefined") {
 | 
				
			||||||
 | 
					                                skip = 0;
 | 
				
			||||||
 | 
					                            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            const options = {
 | 
				
			||||||
 | 
					                                projection: msg.projection || {},
 | 
				
			||||||
 | 
					                                sort: msg.sort,
 | 
				
			||||||
 | 
					                                skip,
 | 
				
			||||||
 | 
					                            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            coll.findOne(selector, options, function (err, item) {
 | 
				
			||||||
 | 
					                                if (err) {
 | 
				
			||||||
 | 
					                                    node.error(err);
 | 
				
			||||||
 | 
					                                }
 | 
				
			||||||
 | 
					                                else {
 | 
				
			||||||
 | 
					                                    msg.payload = item;
 | 
				
			||||||
 | 
					                                    delete msg.projection;
 | 
				
			||||||
 | 
					                                    delete msg.sort;
 | 
				
			||||||
 | 
					                                    delete msg.skip;
 | 
				
			||||||
 | 
					                                    node.send(msg);
 | 
				
			||||||
 | 
					                                }
 | 
				
			||||||
 | 
					                            });
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
                        else if (node.operation === "count") {
 | 
					                        else if (node.operation === "count") {
 | 
				
			||||||
                            selector = ensureValidSelectorObject(msg.payload);
 | 
					                            selector = ensureValidSelectorObject(msg.payload);
 | 
				
			||||||
                            coll.count(selector, function(err, count) {
 | 
					                            coll.count(selector, function(err, count) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -48,6 +48,14 @@ Optionally, you may also (via a function) set
 | 
				
			|||||||
- a `msg.limit` number,
 | 
					- a `msg.limit` number,
 | 
				
			||||||
- a `msg.skip` number.
 | 
					- a `msg.skip` number.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*FindOne* is similar to *Find* but only returns a singular object that matches the query from the `msg.payload`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Optionally, you may also (via a function) set
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- a `msg.projection` object to constrain the returned fields,
 | 
				
			||||||
 | 
					- a `msg.sort` object.
 | 
				
			||||||
 | 
					- a `msg.skip` number.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
*Count* returns a count of the number of documents in a collection or matching a
 | 
					*Count* returns a count of the number of documents in a collection or matching a
 | 
				
			||||||
query using the `msg.payload` as the query statement.
 | 
					query using the `msg.payload` as the query statement.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,9 +21,12 @@
 | 
				
			|||||||
            "insertOne": "insertOne",
 | 
					            "insertOne": "insertOne",
 | 
				
			||||||
            "insertMany": "insertMany",
 | 
					            "insertMany": "insertMany",
 | 
				
			||||||
            "update": "update",
 | 
					            "update": "update",
 | 
				
			||||||
 | 
					            "updateOne": "updateOne",
 | 
				
			||||||
 | 
					            "updateMany": "updateMany",
 | 
				
			||||||
            "remove": "remove",
 | 
					            "remove": "remove",
 | 
				
			||||||
            "deleteOne": "deleteOne",
 | 
					            "deleteOne": "deleteOne",
 | 
				
			||||||
            "deleteMany": "deleteMany",
 | 
					            "deleteMany": "deleteMany",
 | 
				
			||||||
 | 
					            "findOne": "findOne",
 | 
				
			||||||
            "find": "find",
 | 
					            "find": "find",
 | 
				
			||||||
            "count": "count",
 | 
					            "count": "count",
 | 
				
			||||||
            "aggregate": "aggregate"
 | 
					            "aggregate": "aggregate"
 | 
				
			||||||
@@ -34,6 +37,7 @@
 | 
				
			|||||||
            "error": "error"
 | 
					            "error": "error"
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        "tip": "<b> Tip:</b> If no collection is set, ensure <code>msg.collection</code> will contain the collection name",
 | 
					        "tip": "<b> Tip:</b> If no collection is set, ensure <code>msg.collection</code> will contain the collection name",
 | 
				
			||||||
 | 
					        "insertMany-tips": "<b> Tip:</b> When using <code>insertMany</code> ensure your <code>msg.payload.values</code> is an array of objects that you want to insert.",
 | 
				
			||||||
        "errors": {
 | 
					        "errors": {
 | 
				
			||||||
            "nocollection": "No collection defined",
 | 
					            "nocollection": "No collection defined",
 | 
				
			||||||
            "missingconfig": "missing mongodb configuration"
 | 
					            "missingconfig": "missing mongodb configuration"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,9 +21,12 @@
 | 
				
			|||||||
            "insertOne": "insertOne",
 | 
					            "insertOne": "insertOne",
 | 
				
			||||||
            "insertMany": "insertMany",
 | 
					            "insertMany": "insertMany",
 | 
				
			||||||
            "update": "update",
 | 
					            "update": "update",
 | 
				
			||||||
 | 
					            "updateOne": "updateOne",
 | 
				
			||||||
 | 
					            "updateMany": "updateMany",
 | 
				
			||||||
            "remove": "remove",
 | 
					            "remove": "remove",
 | 
				
			||||||
            "deleteOne": "deleteOne",
 | 
					            "deleteOne": "deleteOne",
 | 
				
			||||||
            "deleteMany": "deleteMany",
 | 
					            "deleteMany": "deleteMany",
 | 
				
			||||||
 | 
					            "findOne": "findOne",
 | 
				
			||||||
            "find": "find",
 | 
					            "find": "find",
 | 
				
			||||||
            "count": "count",
 | 
					            "count": "count",
 | 
				
			||||||
            "aggregate": "aggregate"
 | 
					            "aggregate": "aggregate"
 | 
				
			||||||
@@ -34,6 +37,7 @@
 | 
				
			|||||||
            "error": "エラー"
 | 
					            "error": "エラー"
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        "tip": "<b> Tip:</b> コレクションが設定されていない場合、 <code>msg.collection</code>がコレクション名として使われます。",
 | 
					        "tip": "<b> Tip:</b> コレクションが設定されていない場合、 <code>msg.collection</code>がコレクション名として使われます。",
 | 
				
			||||||
 | 
					        "insertMany-tips": "<b> Tip:</b> 使用する場合 <code>insertMany</code> あなたの <code>msg.payload.values</code> 挿入するオブジェクトの配列です。",
 | 
				
			||||||
        "errors": {
 | 
					        "errors": {
 | 
				
			||||||
            "nocollection": "コレクションが定義されていません",
 | 
					            "nocollection": "コレクションが定義されていません",
 | 
				
			||||||
            "missingconfig": "mongodbの設定がみつかりません"
 | 
					            "missingconfig": "mongodbの設定がみつかりません"
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user