mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
Add updated mongodb functions
This commit is contained in:
parent
ea92f293e2
commit
a829d66d7d
@ -128,6 +128,12 @@
|
||||
<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="updateOne" data-i18n="mongodb.operation.updateOne"></option>
|
||||
<option value="updateMany" data-i18n="mongodb.operation.updateMany"></option>
|
||||
<option value="deleteOne" data-i18n="mongodb.operation.deleteOne"></option>
|
||||
<option value="deleteMany" data-i18n="mongodb.operation.deleteMany"></option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row node-input-payonly">
|
||||
@ -154,15 +160,21 @@
|
||||
|
||||
<script type="text/html" data-help-name="mongodb out">
|
||||
<p>A simple MongoDB output node. Can save, insert, update and remove objects from a chosen collection.</p>
|
||||
<p>Save will update an existing object or insert a new object if one does not already exist.</p>
|
||||
<p>Insert will insert a new object.</p>
|
||||
<p>Save and insert either store <code>msg</code> or <code>msg.payload</code>.</p>
|
||||
<p>Save will update an existing object or insert a new object if one does not already exist. (deprecated)</p>
|
||||
<p>Insert will insert a new object. (deprecated)</p>
|
||||
<p>InsertOne will insert a new object.</p>
|
||||
<p>InsertMany will insert an array of new objects.</p>
|
||||
<p>Save, insert, and insertOne will either store <code>msg</code> or <code>msg.payload</code>.</p>
|
||||
<p>Update will modify an existing object or objects. The query to select objects to update uses <code>msg.query</code>
|
||||
and the update to the element uses <code>msg.payload</code>. If <code>msg.query._id</code> is
|
||||
a valid mongo ObjectId string it will be converted to an ObjectId type.</p>
|
||||
<p>Update can add a object if it does not exist or update multiple objects.</p>
|
||||
<p>Update can add a object if it does not exist or update multiple objects. (deprecated)</p>
|
||||
<p>UpdateOne will modify an existing object that matches the query. UpdateOne can add a object if it does not exist or update multiple objects.</p>
|
||||
<p>UpdateMany will modify all objects that match the query. UpdateOne can add a object if it does not exist or update multiple objects.</p>
|
||||
<p>Remove will remove objects that match the query passed in on <code>msg.payload</code>. A blank query will delete
|
||||
<i>all of the objects</i> in the collection.</p>
|
||||
<i>all of the objects</i> in the collection. (deprecated)</p>
|
||||
<p>DeleteOne will remove a single object that match the query passed in on <code>msg.payload</code>.</p>
|
||||
<p>DeleteMany will remove all objects that match the query passed in on <code>msg.payload</code>.</p>
|
||||
<p>You can either set the collection method in the node config or on <code>msg.collection</code>. Setting it in the
|
||||
node will override <code>msg.collection</code>.</p>
|
||||
<p>By default MongoDB creates an <i>_id</i> property as the primary key - so repeated injections of the
|
||||
@ -178,10 +190,10 @@
|
||||
$("#node-input-operation").change(function () {
|
||||
var id = $("#node-input-operation option:selected").val();
|
||||
|
||||
if (id === "update") {
|
||||
if (id === "update" || id === "updateOne" || id === "updateMany") {
|
||||
$(".node-input-payonly").hide();
|
||||
$(".node-input-upsert, .node-input-multi").show();
|
||||
} else if (id === "delete") {
|
||||
} else if (id === "delete" || id === "deleteOne" || id === "deleteMany") {
|
||||
$(".node-input-payonly, .node-input-upsert, .node-input-multi").hide();
|
||||
} else {
|
||||
$(".node-input-payonly").show();
|
||||
|
@ -53,6 +53,21 @@ module.exports = function(RED) {
|
||||
}
|
||||
});
|
||||
|
||||
function getPayload(node, msg) {
|
||||
if (node.payonly) {
|
||||
if (typeof msg.payload !== "object") {
|
||||
msg.payload = {"payload": msg.payload};
|
||||
}
|
||||
if (msg.hasOwnProperty("_id") && !msg.payload.hasOwnProperty("_id")) {
|
||||
msg.payload._id = msg._id;
|
||||
}
|
||||
|
||||
return msg.payload;
|
||||
}
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
function ensureValidSelectorObject(selector) {
|
||||
if (selector != null && (typeof selector != 'object' || Buffer.isBuffer(selector))) {
|
||||
return {};
|
||||
@ -126,6 +141,25 @@ module.exports = function(RED) {
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (node.operation === "insertOne") {
|
||||
const options = {}
|
||||
const payload = getPayload(node, msg);
|
||||
|
||||
coll.insertOne(payload, options, function(err,item) {
|
||||
if (err) {
|
||||
node.error(err,msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (node.operation === "insertMany") {
|
||||
const options = {}
|
||||
|
||||
coll.insertMany(msg.payload, options, function(err,item) {
|
||||
if (err) {
|
||||
node.error(err,msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (node.operation === "insert") {
|
||||
if (node.payonly) {
|
||||
if (typeof msg.payload !== "object") {
|
||||
@ -174,6 +208,57 @@ module.exports = function(RED) {
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (node.operation === "updateOne") {
|
||||
if (typeof msg.payload !== "object") {
|
||||
msg.payload = {"payload": msg.payload};
|
||||
}
|
||||
var query = msg.query || {};
|
||||
var payload = msg.payload || {};
|
||||
var options = {
|
||||
upsert: node.upsert,
|
||||
multi: node.multi
|
||||
};
|
||||
if (ObjectID.isValid(msg.query._id)) {
|
||||
msg.query._id = new ObjectID(msg.query._id);
|
||||
}
|
||||
coll.updateOne(query, payload, options, function(err, item) {
|
||||
if (err) {
|
||||
node.error(err,msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (node.operation === "updateMany") {
|
||||
var query = msg.query || {};
|
||||
var payload = msg.payload || [];
|
||||
var options = {
|
||||
upsert: node.upsert,
|
||||
multi: node.multi
|
||||
};
|
||||
if (ObjectID.isValid(msg.query._id)) {
|
||||
msg.query._id = new ObjectID(msg.query._id);
|
||||
}
|
||||
coll.updateMany(query, payload, options, function(err, item) {
|
||||
if (err) {
|
||||
node.error(err,msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (node.operation === "deleteOne") {
|
||||
const options = {}
|
||||
coll.deleteOne(msg.payload, options, function(err, items) {
|
||||
if (err) {
|
||||
node.error(err,msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (node.operation === "deleteMany") {
|
||||
const options = {}
|
||||
coll.deleteMany(msg.payload, options, function(err, items) {
|
||||
if (err) {
|
||||
node.error(err,msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
@ -66,16 +66,32 @@ A simple MongoDB output node. Can save, insert, update and remove objects from a
|
||||
|
||||
MongoDB only accepts objects.
|
||||
|
||||
Save and insert can either store `msg` or `msg.payload`. If msg.payload is
|
||||
Save, insert, and insertOne can either store `msg` or `msg.payload`. If msg.payload is
|
||||
selected it should contain an object. If not it will be wrapped in an object with a name of payload.
|
||||
|
||||
*Save* will update an existing object or insert a new object if one does not already exist.
|
||||
*Save* will update an existing object or insert a new object if one does not already exist. (Deprecated)
|
||||
|
||||
*Insert* will insert a new object. (Deprecated)
|
||||
|
||||
*InsertOne* will insert a single new object.
|
||||
|
||||
*InsertMany* takes an array of objects in `msg.payload` and inserts them all into the collection.
|
||||
|
||||
*Insert* will insert a new object.
|
||||
|
||||
*Update* will modify an existing object or objects. The query to select objects
|
||||
to update uses `msg.query` and the update to the element uses `msg.payload`.
|
||||
Update can add an object if it does not exist or update multiple objects.
|
||||
Update can add an object if it does not exist or update multiple objects. (Deprecated)
|
||||
|
||||
*UpdateOne* will modify an existing object. The query to select objects
|
||||
to update uses `msg.query` and the update to the element uses `msg.payload`.
|
||||
Update can add an object if it does not exist.
|
||||
|
||||
*UpdateMany* will modify all objects that match the query. The query to select objects
|
||||
to update uses `msg.query` and the update to the element uses `msg.payload`.
|
||||
|
||||
*DeleteOne* will remove one object that matches the query passed in on `msg.payload`.
|
||||
|
||||
*DeleteMany* will remove all objects that match the query passed in on `msg.payload`.
|
||||
|
||||
*Remove* will remove objects that match the query passed in on `msg.payload`.
|
||||
A blank query will delete *all of the objects* in the collection.
|
||||
|
@ -18,8 +18,12 @@
|
||||
"operation": {
|
||||
"save": "save",
|
||||
"insert": "insert",
|
||||
"insertOne": "insertOne",
|
||||
"insertMany": "insertMany",
|
||||
"update": "update",
|
||||
"remove": "remove",
|
||||
"deleteOne": "deleteOne",
|
||||
"deleteMany": "deleteMany",
|
||||
"find": "find",
|
||||
"count": "count",
|
||||
"aggregate": "aggregate"
|
||||
|
@ -18,8 +18,12 @@
|
||||
"operation": {
|
||||
"save": "save",
|
||||
"insert": "insert",
|
||||
"insertOne": "insertOne",
|
||||
"insertMany": "insertMany",
|
||||
"update": "update",
|
||||
"remove": "remove",
|
||||
"deleteOne": "deleteOne",
|
||||
"deleteMany": "deleteMany",
|
||||
"find": "find",
|
||||
"count": "count",
|
||||
"aggregate": "aggregate"
|
||||
|
@ -3,7 +3,7 @@
|
||||
"version" : "0.2.4",
|
||||
"description" : "Node-RED nodes to talk to an Mongo database",
|
||||
"dependencies" : {
|
||||
"mongodb" : "^3.6.2"
|
||||
"mongodb" : "^3.6.3"
|
||||
},
|
||||
"repository" : {
|
||||
"type":"git",
|
||||
@ -25,6 +25,10 @@
|
||||
{
|
||||
"name": "Ross Cruickshank",
|
||||
"email": "ross@vnet.ibm.com"
|
||||
},
|
||||
{
|
||||
"name": "Brandon Herman",
|
||||
"email": "brandon.herman@outlook.com"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user