Skip to content

Aggregate pipeline query doesn't accepts $and operator #1263

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

Closed
allanext opened this issue Nov 28, 2020 · 2 comments
Closed

Aggregate pipeline query doesn't accepts $and operator #1263

allanext opened this issue Nov 28, 2020 · 2 comments

Comments

@allanext
Copy link

allanext commented Nov 28, 2020

It looks like I can use $unwind, $match, $group, $count, $sum, $multiply etc with an aggregate pipeline but the $and operator doesn't seems to be allowed?

const query = new Parse.Query("applications");
pipeline = [
        {'$and': [{'$text': {'$search': request.params.keyword, '$caseSensitive': false, '$diacriticSensitive': false}},
                  {program_id: request.params.program},
                  {state: {'$nin': ["Archived", "Completed", "Declined"]}}
                  ]}
      ];
 await query.aggregate(pipeline).then((res_apps) => {
      apps = res_apps
    }).catch((error) => {console.log(error)});

I'm getting an Error: Invalid parameter for query: $and.

The same query works on Rails and on a mongoDB terminal.

Am I making a mistake? Does using the "and" operators implies using the query.find() with equalTo(), notContainedIn() etc functions?

For a simple query I guess I could do something like:

query.fullText('*', request.params.keyword);
query.equalTo("program_id", request.params.program);
query.notContainedIn("state", ["Archived", "Completed", "Declined"]);

I've looked at the API on the fullText function and it doesn't look I can use the wildcard operator, how can I index/search across all fields of a document? I'm getting an Error: Invalid key name: * (i guess this is covered here: parse-community/parse-server#3747)

Unfortunately I have to use various operators together like $and, $regex, $text, $search, $literal, $match, $exist, $gte, $lte, etc and then group them with an aggregate query to count the $sum, are these operators supported by parse?

Is it possible to run native MongoDB queries from within Parse?

Thank you!

@davimacedo
Copy link
Member

When you do

query.fullText('*', request.params.keyword);
query.equalTo("program_id", request.params.program);
query.notContainedIn("state", ["Archived", "Completed", "Declined"]);

The $and operator is automatically used in order to ensure all the three constraints. You can also use a $and operator in the aggregate function, but it'd need to be inside a $match stage.

@allanext
Copy link
Author

allanext commented Dec 11, 2020

Thanks @davimacedo,

in fact this works:

const query = new Parse.Query("m_apps");
pipeline = [{
          match: { '$and': [
            {'$text': {'$search': request.params.keyword, '$caseSensitive': false, '$diacriticSensitive': false}},
            {program_id: request.params.program},
            {state: {'$nin': ["Archived", "Completed", "Declined"]}}
          ]}
        }];
await query.aggregate(pipeline).then((res_apps) => {
      apps = res_apps
    }).catch((error) => {console.log(error)});

Note that in this $text case you'll need to create an index on from the mongo shell:
db.m_apps.createIndex({ '$**': 'text' },{ background: true, name: 'Index_apps_text' })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants