Page 1 of 1

Server Code DB query with criteria

Posted: Thu May 01, 2014 3:21 pm
by Slava Balabanov

Hi,
So i'm trying to create a certain query in the server code but i cant get it to work for some reason.
I have a collection of foods, that one of the columns is a pointer to the restaurant.
I want to get all the foods of a specific restaurant
I try to use a Collection.query function and provide the params as follows:
precode
{ sort: 'name', criteria: { foodType: { '$all': [Object] }, specialFoodType: { '$all': [Object] }, restId: { '$inQuery': [Object] } } }
/code/pre
This is the code that defines the params:
precode
Params.criteria = {
'foodType' : { "$all" : [foodType] },
'specialFoodType' : { "$all" : [ specialFoodType ] },
'restId' : { "$inQuery" : { "_id" : restaurantId } }
};
/code/pre

When i try to use the Collection.Query function i get an error:
[Error: invalid operator: $inQuery] code: '0'

This is my definition of the collection:
Image
When i use the where clause in appery.io builder then the inQuery works, but here it doesnt
What am i doing wrong?


Server Code DB query with criteria

Posted: Fri May 02, 2014 12:20 am
by Yurii Orishchuk

Hi Slava.

That's important to know Collection.query is not the same as collections REST API.

Collection.query from server code described here: http://docs.appery.io/documentation/b...

And there is note about "criteria" -

criteria - JSON – queries the object that was built in accordance with the mongo querying documentation. See the querying section in Collections REST API.

http://docs.mongodb.org/manual/refere...

For you know there is no "$inquery" clause in the mongo. It's an appery.io REST API implementation.

So you have two ways to do this:

1 Invoke through the XHR appery.io REST API. See details here: http://docs.appery.io/documentation/c...

2 Use mondo query criteria. http://docs.mongodb.org/manual/refere...

For your example: you should use:

pre
code

'restId._id': restaurantId

/code
/pre

instead of

pre
code

'restId' : { "$inQuery" : { "_id" : restaurantId } }

/code
/pre

See documentation: http://docs.mongodb.org/manual/refere...

Regards.