Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐞MongoDb: Fix to ensure all items get processed in all operations #3808

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 61 additions & 56 deletions packages/nodes-base/nodes/MongoDb/MongoDb.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ export class MongoDb implements INodeType {
await this.getCredentials('mongoDb'),
);

const client: MongoClient = await MongoClient.connect(connectionString, {
const client = await MongoClient.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
});

const mdb = client.db(database as string);

let returnItems = [];
let returnItems: INodeExecutionData[] = [];

const items = this.getInputData();
const operation = this.getNodeParameter('operation', 0) as string;
Expand All @@ -53,83 +53,88 @@ export class MongoDb implements INodeType {
// aggregate
// ----------------------------------

try {
const queryParameter = JSON.parse(this.getNodeParameter('query', 0) as string);
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
const queryParameter = JSON.parse(this.getNodeParameter('query', itemIndex) as string);

if (queryParameter._id && typeof queryParameter._id === 'string') {
queryParameter._id = new ObjectID(queryParameter._id);
}
if (queryParameter._id && typeof queryParameter._id === 'string') {
queryParameter._id = new ObjectID(queryParameter._id);
}

const query = mdb
.collection(this.getNodeParameter('collection', 0) as string)
.aggregate(queryParameter);
const query = mdb
.collection(this.getNodeParameter('collection', itemIndex) as string)
.aggregate(queryParameter);

const queryResult = await query.toArray();
const queryResult = await query.toArray();

returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]);
} catch (error) {
if (this.continueOnFail()) {
returnItems = this.helpers.returnJsonArray({ error: (error as JsonObject).message } );
} else {
throw error;
returnItems.push(...this.helpers.returnJsonArray(queryResult as IDataObject[]));
} catch (error) {
if (this.continueOnFail()) {
returnItems.push(...this.helpers.returnJsonArray({ error: (error as JsonObject).message } ));
} else {
throw error;
}
}
}
} else if (operation === 'delete') {
// ----------------------------------
// delete
// ----------------------------------

try {
const { deletedCount } = await mdb
.collection(this.getNodeParameter('collection', 0) as string)
.deleteMany(JSON.parse(this.getNodeParameter('query', 0) as string));
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
const { deletedCount } = await mdb
.collection(this.getNodeParameter('collection', itemIndex) as string)
.deleteMany(JSON.parse(this.getNodeParameter('query', itemIndex) as string));

returnItems = this.helpers.returnJsonArray([{ deletedCount }]);
} catch (error) {
if (this.continueOnFail()) {
returnItems = this.helpers.returnJsonArray({ error: (error as JsonObject).message });
} else {
throw error;
returnItems = this.helpers.returnJsonArray([{ deletedCount }]);
} catch (error) {
if (this.continueOnFail()) {
returnItems = this.helpers.returnJsonArray({ error: (error as JsonObject).message });
} else {
throw error;
}
}
}

} else if (operation === 'find') {
// ----------------------------------
// find
// ----------------------------------

try {
const queryParameter = JSON.parse(this.getNodeParameter('query', 0) as string);
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
const queryParameter = JSON.parse(this.getNodeParameter('query', itemIndex) as string);

if (queryParameter._id && typeof queryParameter._id === 'string') {
queryParameter._id = new ObjectID(queryParameter._id);
}
if (queryParameter._id && typeof queryParameter._id === 'string') {
queryParameter._id = new ObjectID(queryParameter._id);
}

let query = mdb
.collection(this.getNodeParameter('collection', 0) as string)
.find(queryParameter);
let query = mdb
.collection(this.getNodeParameter('collection', itemIndex) as string)
.find(queryParameter);

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();
const options = this.getNodeParameter('options', itemIndex) 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[]);
} catch (error) {
if (this.continueOnFail()) {
returnItems = this.helpers.returnJsonArray({ error: (error as JsonObject).message } );
} else {
throw error;
returnItems.push(...this.helpers.returnJsonArray(queryResult as IDataObject[]));
} catch (error) {
if (this.continueOnFail()) {
returnItems.push(...this.helpers.returnJsonArray({ error: (error as JsonObject).message } ));
} else {
throw error;
}
}
}
} else if (operation === 'insert') {
Expand Down