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

feat: Add support for extended JSON in mongo node #12574

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions packages/nodes-base/nodes/MongoDb/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {EJSON} from 'bson';
import get from 'lodash/get';
import set from 'lodash/set';
import { MongoClient, ObjectId } from 'mongodb';
Expand Down Expand Up @@ -145,6 +146,10 @@ export function stringifyObjectIDs(items: INodeExecutionData[]) {
return items;
}

export function parseJsonToEjson(query: unknown) {
return EJSON.parse(JSON.stringify(query)) as Record<string, unknown>
}

export async function connectMongoClient(connectionString: string, credentials: IDataObject = {}) {
let client: MongoClient;

Expand Down
11 changes: 8 additions & 3 deletions packages/nodes-base/nodes/MongoDb/MongoDb.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type {
import {
buildParameterizedConnString,
connectMongoClient,
parseJsonToEjson,
prepareFields,
prepareItems,
stringifyObjectIDs,
Expand Down Expand Up @@ -134,7 +135,7 @@ export class MongoDb implements INodeType {

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

for (const entry of await query.toArray()) {
returnData.push({ json: entry, pairedItem: fallbackPairedItems ?? [{ item: i }] });
Expand All @@ -155,9 +156,13 @@ export class MongoDb implements INodeType {
if (operation === 'delete') {
for (let i = 0; i < itemsLength; i++) {
try {
const queryParameter = JSON.parse(
this.getNodeParameter('query', i) as string,
) as IDataObject;

const { deletedCount } = await mdb
.collection(this.getNodeParameter('collection', i) as string)
.deleteMany(JSON.parse(this.getNodeParameter('query', i) as string) as Document);
.deleteMany(parseJsonToEjson(queryParameter) as unknown as Document);

returnData.push({
json: { deletedCount },
Expand Down Expand Up @@ -189,7 +194,7 @@ export class MongoDb implements INodeType {

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

const options = this.getNodeParameter('options', i);
const limit = options.limit as number;
Expand Down