Skip to content

Commit

Permalink
fix(MongoDB Node): Stringify response ObjectIDs (#6990)
Browse files Browse the repository at this point in the history
Github issue / Community forum post (link here to close automatically):
  • Loading branch information
maspio authored Aug 29, 2023
1 parent 8a01d06 commit 9ca990b
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 307 deletions.
17 changes: 14 additions & 3 deletions packages/nodes-base/nodes/MongoDb/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import type {
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';

import get from 'lodash/get';
import set from 'lodash/set';
import { ObjectId } from 'mongodb';
import type {
IMongoCredentials,
IMongoCredentialsType,
IMongoParametricCredentials,
} from './mongoDb.types';

import get from 'lodash/get';
import set from 'lodash/set';

/**
* Standard way of building the MongoDB connection string, unless overridden with a provided string
*
Expand Down Expand Up @@ -129,3 +129,14 @@ export function prepareFields(fields: string) {
.map((field) => field.trim())
.filter((field) => !!field);
}

export function stringifyObjectIDs(items: IDataObject[]) {
items.forEach((item) => {
if (item._id instanceof ObjectId) {
item._id = item._id.toString();
}
if (item.id instanceof ObjectId) {
item.id = item.id.toString();
}
});
}
62 changes: 44 additions & 18 deletions packages/nodes-base/nodes/MongoDb/MongoDb.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,47 @@ import type {
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';

import { nodeDescription } from './MongoDbDescription';

import {
buildParameterizedConnString,
prepareFields,
prepareItems,
validateAndResolveMongoCredentials,
} from './GenericFunctions';

import type {
FindOneAndReplaceOptions,
FindOneAndUpdateOptions,
UpdateOptions,
Sort,
} from 'mongodb';
import { MongoClient, ObjectId } from 'mongodb';
import { nodeProperties } from './MongoDbProperties';

import {
buildParameterizedConnString,
prepareFields,
prepareItems,
stringifyObjectIDs,
validateAndResolveMongoCredentials,
} from './GenericFunctions';

import type { IMongoParametricCredentials } from './mongoDb.types';

export class MongoDb implements INodeType {
description: INodeTypeDescription = nodeDescription;
description: INodeTypeDescription = {
displayName: 'MongoDB',
name: 'mongoDb',
icon: 'file:mongodb.svg',
group: ['input'],
version: 1,
description: 'Find, insert and update documents in MongoDB',
defaults: {
name: 'MongoDB',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'mongoDb',
required: true,
testedBy: 'mongoDbCredentialTest',
},
],
properties: nodeProperties,
};

methods = {
credentialTest: {
Expand Down Expand Up @@ -65,7 +85,7 @@ export class MongoDb implements INodeType {
} catch (error) {
return {
status: 'Error',
message: error.message,
message: (error as Error).message,
};
}
return {
Expand Down Expand Up @@ -98,15 +118,17 @@ export class MongoDb implements INodeType {
// ----------------------------------

try {
const queryParameter = JSON.parse(this.getNodeParameter('query', 0) as string);
const queryParameter = JSON.parse(
this.getNodeParameter('query', 0) as string,
) as IDataObject;

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

const query = mdb
.collection(this.getNodeParameter('collection', 0) as string)
.aggregate(queryParameter as Document[]);
.aggregate(queryParameter as unknown as Document[]);

responseData = await query.toArray();
} catch (error) {
Expand Down Expand Up @@ -140,20 +162,22 @@ export class MongoDb implements INodeType {
// ----------------------------------

try {
const queryParameter = JSON.parse(this.getNodeParameter('query', 0) as string);
const queryParameter = JSON.parse(
this.getNodeParameter('query', 0) as string,
) as IDataObject;

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

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

const options = this.getNodeParameter('options', 0);
const limit = options.limit as number;
const skip = options.skip as number;
const sort: Sort = options.sort && JSON.parse(options.sort as string);
const sort = options.sort && (JSON.parse(options.sort as string) as Sort);
if (skip > 0) {
query = query.skip(skip);
}
Expand Down Expand Up @@ -339,6 +363,8 @@ export class MongoDb implements INodeType {

await client.close();

stringifyObjectIDs(responseData);

const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData),
{ itemData: { item: 0 } },
Expand Down
Loading

0 comments on commit 9ca990b

Please sign in to comment.