Add limit, skip, sort, upsert in MongoDB node (#1439)

* mongodb find command improvements: limit, skip, sort

* mongodb update command improvement: upsert

*  improve mongo node

* 🎨 add missing semicolons

Co-authored-by: ahsan-virani <ahsan.virani@gmail.com>
This commit is contained in:
Anton Romanov 2021-04-14 14:41:56 +03:00 committed by GitHub
parent f4916c7efb
commit 315d3b59f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 5 deletions

View file

@ -60,10 +60,24 @@ export class MongoDb implements INodeType {
// find // find
// ---------------------------------- // ----------------------------------
const queryResult = await mdb let query = mdb
.collection(this.getNodeParameter('collection', 0) as string) .collection(this.getNodeParameter('collection', 0) as string)
.find(JSON.parse(this.getNodeParameter('query', 0) as string)) .find(JSON.parse(this.getNodeParameter('query', 0) as string));
.toArray();
const options = this.getNodeParameter('options', 0) as IDataObject;
const limit = options.limit as number;
const skip = options.skip as number;
const sort = options.sort && JSON.parse(options.sort as string);
if (skip > 0) {
query = query.skip(skip);
}
if (limit > 0) {
query = query.limit(limit);
}
if (sort && Object.keys(sort).length !== 0 && sort.constructor === Object) {
query = query.sort(sort);
}
const queryResult = await query.toArray();
returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]); returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]);
} else if (operation === 'insert') { } else if (operation === 'insert') {
@ -112,6 +126,9 @@ export class MongoDb implements INodeType {
let updateKey = this.getNodeParameter('updateKey', 0) as string; let updateKey = this.getNodeParameter('updateKey', 0) as string;
updateKey = updateKey.trim(); updateKey = updateKey.trim();
const updateOptions = (this.getNodeParameter('upsert', 0) as boolean)
? { upsert: true } : undefined;
if (!fields.includes(updateKey)) { if (!fields.includes(updateKey)) {
fields.push(updateKey); fields.push(updateKey);
} }
@ -136,7 +153,7 @@ export class MongoDb implements INodeType {
} }
await mdb await mdb
.collection(this.getNodeParameter('collection', 0) as string) .collection(this.getNodeParameter('collection', 0) as string)
.updateOne(filter, { $set: item }); .updateOne(filter, { $set: item }, updateOptions);
} }
returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]); returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]);
} else { } else {

View file

@ -90,6 +90,47 @@ export const nodeDescription: INodeTypeDescription = {
// ---------------------------------- // ----------------------------------
// find // find
// ---------------------------------- // ----------------------------------
{
displayName: 'Options',
name: 'options',
type: 'collection',
displayOptions: {
show: {
operation: ['find'],
},
},
default: {},
placeholder: 'Add options',
description: 'Add query options',
options: [
{
displayName: 'Limit',
name: 'limit',
type: "number",
default: 0,
description: 'Use limit to specify the maximum number of documents or 0 for unlimited documents.',
},
{
displayName: 'Skip',
name: 'skip',
type: "number",
default: 0,
description: 'The number of documents to skip in the results set.',
},
{
displayName: 'Sort (JSON format)',
name: 'sort',
type: 'json',
typeOptions: {
rows: 2,
},
default: '{}',
placeholder: '{ "field": -1 }',
required: true,
description: 'A json that defines the sort order of the result set.',
},
],
},
{ {
displayName: 'Query (JSON format)', displayName: 'Query (JSON format)',
name: 'query', name: 'query',
@ -109,7 +150,6 @@ export const nodeDescription: INodeTypeDescription = {
required: true, required: true,
description: 'MongoDB Find query.', description: 'MongoDB Find query.',
}, },
// ---------------------------------- // ----------------------------------
// insert // insert
// ---------------------------------- // ----------------------------------
@ -165,6 +205,18 @@ export const nodeDescription: INodeTypeDescription = {
description: description:
'Comma separated list of the fields to be included into the new document.', 'Comma separated list of the fields to be included into the new document.',
}, },
{
displayName: 'Upsert',
name: 'upsert',
type: 'boolean',
displayOptions: {
show: {
operation: ['update'],
},
},
default: false,
description: `Perform an insert if no documents match the update key`,
},
{ {
displayName: 'Options', displayName: 'Options',
name: 'options', name: 'options',