mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
fix(MongoDB Node): Stringify response ObjectIDs (#6990)
Github issue / Community forum post (link here to close automatically):
This commit is contained in:
@@ -11,15 +11,6 @@ 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,
|
||||
@@ -27,11 +18,40 @@ import type {
|
||||
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: {
|
||||
@@ -65,7 +85,7 @@ export class MongoDb implements INodeType {
|
||||
} catch (error) {
|
||||
return {
|
||||
status: 'Error',
|
||||
message: error.message,
|
||||
message: (error as Error).message,
|
||||
};
|
||||
}
|
||||
return {
|
||||
@@ -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) {
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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 } },
|
||||
|
||||
Reference in New Issue
Block a user