Page 1 of 1

Trying to find all matches between database collections

Posted: Wed Feb 04, 2015 8:46 pm
by Ted7364671

I'm trying to find all matches between database collections - that is, search Collection B for all values in a given column in Collection A.

Collection A:
A
B
D

Collection B:
A
C
D

Query Returns:
A
D

Currently I am using a list service to put the Collection A values into local storage but I think the problem lies with how to map it to the "where" in my Collection B query service.

I feel like this should be straightforward but I've poured over the documentation and only found examples with the where defined as a static/default value or mapped to a single input. How do I search for any match out of a list of items?

Thanks in advance!


Trying to find all matches between database collections

Posted: Thu Feb 05, 2015 3:27 am
by Yurii Orishchuk

Hi Ted,

Yes your way is correct.

To put all items from A collection to WHERE query of B collection you need to use: "$in" clause.

More details here: http://devcenter.appery.io/documentat...

Here is a example code:

pre

var arrayFromACollection = ["a", "b", "c"];

var whereObject = {"studentId": {"$in": arrayFromACollection } };

return JSON.stringify(whereObject);

/pre

Regards.


Trying to find all matches between database collections

Posted: Thu Feb 05, 2015 5:54 pm
by Ted7364671

Cool, thanks -

So how do I fetch the array from local storage (instead of defining it)?
I tried . . .

var Titlearray = localStorage.getItem("w_Title");
var whereObject = {"Title": {"$in": Titlearray } };
return JSON.stringify(whereObject);

but it didn't work. Do I need mapping?

Image


Trying to find all matches between database collections

Posted: Fri Feb 06, 2015 3:28 am
by Yurii Orishchuk

Hi Ted,

Not sure what you have in your LSV with name "w_Title".

But if you have there JSON array - you can use following JS code:

pre

var TitlearrayString = localStorage.getItem("w_Title");
var Titlearray = JSON.parse(TitlearrayString);
var whereObject = {"Title": {"$in": Titlearray } };
return JSON.stringify(whereObject);

/pre

Regards.


Trying to find all matches between database collections

Posted: Fri Feb 06, 2015 10:27 pm
by Ted7364671

I tweaked the list service to local storage mapping to make it a JSON array and that works great! Thank you very much, Yurii -


Trying to find all matches between database collections

Posted: Mon Apr 11, 2016 7:05 am
by John Chaudhry

Hi Yurri,

This is very useful and almost solves my problem but I am not sure how to get an array in a local storage variable into this.

The LSV array contains strings "1", "2", "3". I am not sure how to get them into the Titlearray variable above.

Cheers,

John


Trying to find all matches between database collections

Posted: Mon Apr 11, 2016 10:37 am
by Serhii Kulibaba

Hello John,

Let's continue our discussion here: https://getsatisfaction.com/apperyio/...


Trying to find all matches between database collections

Posted: Tue Apr 12, 2016 6:37 pm
by John Chaudhry

Fixed - thanks Sergiy.