Add fineOne, fix insertMany

This commit is contained in:
Brandon Herman 2021-01-25 12:11:39 -05:00
parent a829d66d7d
commit c7919501ca
5 changed files with 64 additions and 8 deletions

View File

@ -126,12 +126,12 @@
<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="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="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="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="deleteMany" data-i18n="mongodb.operation.deleteMany"></option>
</select>
@ -156,6 +156,7 @@
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
</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 type="text/html" data-help-name="mongodb out">
@ -190,15 +191,24 @@
$("#node-input-operation").change(function () {
var id = $("#node-input-operation option:selected").val();
if (id === "update" || id === "updateOne" || id === "updateMany") {
if (id === "update") {
$(".node-input-payonly").hide();
$(".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") {
$(".node-input-payonly, .node-input-upsert, .node-input-multi").hide();
} else {
$(".node-input-payonly").show();
$(".node-input-upsert, .node-input-multi").hide();
}
if (id === "insertMany") {
$("#node-insertMany-warning").show();
} else {
$("#node-insertMany-warning").hide();
}
});
$("#node-input-collection").change(function () {
@ -250,6 +260,7 @@
<div class="form-row">
<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;">
<option value="findOne" data-i18n="mongodb.operation.findOne"></option>
<option value="find" data-i18n="mongodb.operation.find"></option>
<option value="count" data-i18n="mongodb.operation.count"></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.
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>
<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
<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>

View File

@ -154,7 +154,7 @@ module.exports = function(RED) {
else if (node.operation === "insertMany") {
const options = {}
coll.insertMany(msg.payload, options, function(err,item) {
coll.insertMany(msg.payload.values, options, function(err,items) {
if (err) {
node.error(err,msg);
}
@ -215,8 +215,7 @@ module.exports = function(RED) {
var query = msg.query || {};
var payload = msg.payload || {};
var options = {
upsert: node.upsert,
multi: node.multi
upsert: node.upsert
};
if (ObjectID.isValid(msg.query._id)) {
msg.query._id = new ObjectID(msg.query._id);
@ -231,8 +230,7 @@ module.exports = function(RED) {
var query = msg.query || {};
var payload = msg.payload || [];
var options = {
upsert: node.upsert,
multi: node.multi
upsert: node.upsert
};
if (ObjectID.isValid(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") {
selector = ensureValidSelectorObject(msg.payload);
coll.count(selector, function(err, count) {

View File

@ -48,6 +48,14 @@ Optionally, you may also (via a function) set
- a `msg.limit` 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
query using the `msg.payload` as the query statement.

View File

@ -21,9 +21,12 @@
"insertOne": "insertOne",
"insertMany": "insertMany",
"update": "update",
"updateOne": "updateOne",
"updateMany": "updateMany",
"remove": "remove",
"deleteOne": "deleteOne",
"deleteMany": "deleteMany",
"findOne": "findOne",
"find": "find",
"count": "count",
"aggregate": "aggregate"
@ -34,6 +37,7 @@
"error": "error"
},
"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": {
"nocollection": "No collection defined",
"missingconfig": "missing mongodb configuration"

View File

@ -21,9 +21,12 @@
"insertOne": "insertOne",
"insertMany": "insertMany",
"update": "update",
"updateOne": "updateOne",
"updateMany": "updateMany",
"remove": "remove",
"deleteOne": "deleteOne",
"deleteMany": "deleteMany",
"findOne": "findOne",
"find": "find",
"count": "count",
"aggregate": "aggregate"
@ -34,6 +37,7 @@
"error": "エラー"
},
"tip": "<b> Tip:</b> コレクションが設定されていない場合、 <code>msg.collection</code>がコレクション名として使われます。",
"insertMany-tips": "<b> Tip:</b> 使用する場合 <code>insertMany</code> あなたの <code>msg.payload.values</code> 挿入するオブジェクトの配列です。",
"errors": {
"nocollection": "コレクションが定義されていません",
"missingconfig": "mongodbの設定がみつかりません"