Page 1 of 1

Querys to 2 or more collections

Posted: Wed May 07, 2014 3:10 pm
by Jordi Niubo

Hi, i have 1 database with some collections. In one i have user information. In other one have the photo of this user.

I know how to get the user and then the photo. There are any form that with only the id of user, with only one Query, can get the information of the table user and the photo of the other table?

Thanks


Querys to 2 or more collections

Posted: Wed May 07, 2014 5:39 pm
by Kateryna Grynko

Hi Jordi,

If you have a collection with a field of pointer type that points at another collection, you can query both collections simultaneously. Add a request parameter 'include' where pass names of collections that you point at from the current collection. Find more here: http://docs.appery.io/documentation/b...


Querys to 2 or more collections

Posted: Wed May 07, 2014 9:52 pm
by Jordi Niubo

And for create this relation we need 2 querys?

One for create the first collection and get the id and other one for create the second collection with the pointer to the first?


Querys to 2 or more collections

Posted: Thu May 08, 2014 1:55 am
by Igor

Hello,

Yes, you need 2 queries.


Querys to 2 or more collections

Posted: Fri Jul 04, 2014 6:07 am
by adam griffin

If I have pointers from B - A and C - A. Can I query results from B and C? If so, how? Image


Querys to 2 or more collections

Posted: Fri Jul 04, 2014 8:29 am
by Evgene Karachevtsev

Hello Adam,

Please clarify, do you want in to get one query info from B supplemented with data from A and similarly in another query get info from C supplemented with data from A? Or something different?


Querys to 2 or more collections

Posted: Fri Jul 04, 2014 8:42 am
by Bad Addy

Adam, it does not work like that. With that example, you would need to tier it like this: A - B - C and in the include, you would need to reference them like this:

"include" : b, b.c

With this, you are collecting A, including objects from B, and within the B object, you would have another object C.


Querys to 2 or more collections

Posted: Sat Jul 05, 2014 8:13 am
by adam griffin

Collection A has all my users info -|id|Name|email|-, Collection B is all users messages-|user_id|message|date|-, Collection C has user networks kinda like this -|follow_id|follower_id|date|-. Im trying to query all 3 at once like this. I get Collection C: created date when someone join the another user network, i will also get the user id from Collection C. Then I will take that user id and compare it to collection A id to get the user info. I will then check all the users id from Collection B with its created date. I want to then display the results from when the user first join another user network and that user messages from collection B all on the same list component.


Querys to 2 or more collections

Posted: Sat Jul 05, 2014 8:16 am
by adam griffin

I want the response to be like ulliJoe join your network 3days ago/lilitom sent a message hour ago/li/ul


Querys to 2 or more collections

Posted: Sat Jul 05, 2014 6:48 pm
by Bad Addy

I am not sure you can do that via the app, because Pointers only really work with static _id's. Because you have many _id's in both Collection B and C, you would need to do this in server Code and create a new object and then pass it to the app.

To save calls, you could add a pointer from Collection B that points to Collection A, and then make another call to collect all rows in Collection C with a 'WHERE' clause that equals the user_id. In this case, you would call all messages with a user_id, and within each result would hold another object, providing you include it, that has the users info.

Then with a simple FOR statement, push them together and send it to the app in one go.

You could also do all the sorting via server code, so the device has less to do, but print the result.