Skip to content

aggregate allows a stage type only once #4682

Closed
@oallouch

Description

@oallouch

We often need to use a stage type (specially project and match) several times in the same pipeline, but the JS-SDK's ParseQuery (the Node part of course) prevents it in its aggregate method:

if (Array.isArray(pipeline)) {
   pipeline.forEach((stage) => {
     for (let op in stage) {
       stages[op] = stage[op];
     }
   });
 }

Note that, unlike Map, an Object doesn't guaranty order.

For now, I use this workaround:

const Config = require('../../node_modules/parse-server/lib/Config');

async function aggregate(className, pipeline) {
	//---- $ adding in stage name ----//
	pipeline = pipeline.map(stage => {
		// stage is an object with a single key
		const stageName = Object.keys(stage)[0];
		return { ['$' + stageName]: stage[stageName] };
	})
	//---- direct execution ----//
	const config = Config.get(Parse.applicationId);
	return await config.database.adapter.aggregate('_User', { fields: {} }, pipeline, 'SECONDARY_PREFERRED');
}

Hope that helps.

Olivier, from Paris

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions