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(MongoDB Node): Add projection to query options on Find #9972

Merged
merged 4 commits into from
Aug 7, 2024
Merged
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
6 changes: 6 additions & 0 deletions packages/nodes-base/nodes/MongoDb/MongoDb.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ export class MongoDb implements INodeType {
const options = this.getNodeParameter('options', i);
const limit = options.limit as number;
const skip = options.skip as number;
const projection =
options.projection && (JSON.parse(options.projection as string) as Document);
const sort = options.sort && (JSON.parse(options.sort as string) as Sort);

if (skip > 0) {
Expand All @@ -208,6 +210,10 @@ export class MongoDb implements INodeType {
query = query.sort(sort);
}

if (projection && projection instanceof Document) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Joffcom, I'm sorry but it looks like this object is never imported and came from bson module (which is not a direct dependency).

I'm wondering why calling a projection throws a "Document is not defined" error, but not all the previous calls in this file : what do I have done wrong, here ? :/

query = query.project(projection);
}

const queryResult = await query.toArray();

for (const entry of queryResult) {
Expand Down
14 changes: 13 additions & 1 deletion packages/nodes-base/nodes/MongoDb/MongoDbProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ export const nodeProperties: INodeProperties[] = [
placeholder: '{ "field": -1 }',
description: 'A JSON that defines the sort order of the result set',
},
{
displayName: 'Projection (JSON Format)',
name: 'projection',
type: 'json',
typeOptions: {
rows: 4,
},
default: '{}',
placeholder: '{ "_id": 0, "field": 1 }',
description:
'A JSON that defines a selection of fields to retrieve or exclude from the result set',
},
],
},
{
Expand Down Expand Up @@ -248,7 +260,7 @@ export const nodeProperties: INodeProperties[] = [
name: 'dateFields',
type: 'string',
default: '',
description: 'Comma separeted list of fields that will be parsed as Mongo Date type',
description: 'Comma-separated list of fields that will be parsed as Mongo Date type',
},
{
displayName: 'Use Dot Notation',
Expand Down