Page 1 of 3

Combine multiple where clauses referencing component values

Posted: Tue Jul 30, 2013 3:23 pm
by Shaun Summers

Hello,

I would like to restrict the number of results returned by a service by adding where clauses. I have tried the following:

  • created 'where' request parameter

  • mapped it to my component

  • added this javascript: codereturn '{"Brand":"'+value+'"}';/code

    This works just fine and the list displayed in the app is only for the selected brand.

    I have then added a second 'where' request parameter (once again it references data in a component for the query). I thought this would combine with the first to create an AND but it is ignored.

    It seems that I am not doing the correct thing for constructing a Where this AND Where this query.

    I read another post explaining how to combine multiple wheres in one query but it was based on static data. Not data coming from components.

    Would you help please.

    Thanks, Shaun.


Combine multiple where clauses referencing component values

Posted: Tue Jul 30, 2013 4:53 pm
by Tommy B

To start off with I am a novice but just dangerous enough to figure it out. :)

I used this code to search for dates that were greater than and less than the dates selected in my date pickers. You will notice that I separated the greater than and less than statements with a comma. Here are some links that might help.

codevar s = $('div[dsid="startDate"] input').val();
var e = $('div[dsid="endDate"] input').val();
var s1= s+'T00:00:00.00Z';
var e1= e+'T00:00:00.00Z';
return '{"_createdAt":{"$gte":{"$date":"' + s1 + '"},"$lte":{"$date":"' + e1 + '"}}}'/code

Link


Combine multiple where clauses referencing component values

Posted: Tue Jul 30, 2013 5:17 pm
by Shaun Summers

Thanks Tommy. I am also a novice and struggle sometimes.

If I've understood correctly you map the value from one component to the Where clause. Then do some fancy stuff with jscript to query your database.

I would like to map two separate components to two separate Where clauses then combine them with AND.

I'll have a full read through the link you sent me. Maybe I've misunderstood- completely possible :)


Combine multiple where clauses referencing component values

Posted: Tue Jul 30, 2013 6:07 pm
by Kateryna Grynko

Hi Shaun,

You can use the following:
precodereturn '{'+
'$and: ' +
'[ ' +
'{' +
'"Brand": "' + value1 + '"'+
'},' +
'{' +
'"Brand": "' + value2 + '"'+
'}' +
']' +
'}';/code/pre
Details and possibilities:
Logical Query Operators
Comparison Query Operators

It is important to note that all the work is done through the platform layer, can therefore be restricted.


Combine multiple where clauses referencing component values

Posted: Tue Jul 30, 2013 7:02 pm
by Shaun Summers

Hi,

Thanks for this. But wouldn't this just let me specify the combination of two values to query for the field Brand?

I'm looking to query on one value for Brand AND on one value for the field User_ID. The two combined determine the results coming from the database.

The values need to come from a component in my page and a local storage component.

I excuse myself in advance if I'm missing something in your response.

Regards, Shaun.


Combine multiple where clauses referencing component values

Posted: Tue Jul 30, 2013 7:44 pm
by Kateryna Grynko

Shaun,

Add the following JavaScript for where parameter:
codereturn '{ $and: [ { "Brand": "' + localStorage.getItem("brand") + '" }, { "User_ID": "' + localStorage.getItem("user_id") + '" } ] }';/code
There should be 2 localStorage variables described in code. Mapping is not necessary then. You can map one of the variables. Then you can get itsvalue by accessing "value" variable.


Combine multiple where clauses referencing component values

Posted: Wed Jul 31, 2013 9:17 am
by Shaun Summers

Absolutely perfect. Thanks very much.
I did as suggested and mapped Brand only. User ID was already local storage.

Final code, which works a treat:

code
return '{ $and: [ { "Brand": "'+value+'" }, { "User_ID": "' + localStorage.getItem("user_id") + '" } ] }';
/code


Combine multiple where clauses referencing component values

Posted: Thu Aug 08, 2013 11:21 am
by vipul

I am also using same query
this is my JS
var d = new Date();

var month = d.getMonth()+1;
var day = d.getDate();

var output = d.getFullYear()+'-'+((''+month).length.

But when I fired this query from stand alone service it is working fine.
Can you pls help


Combine multiple where clauses referencing component values

Posted: Thu Aug 08, 2013 11:36 am
by vipul

I am also using this query
This is my JS
var d = new Date();

var month = d.getMonth()+1;
var day = d.getDate();

var output = d.getFullYear()+'-'+((''+month).length that means problem is with Date only,
But when I run this service stand alone with Date parameter it is working fine.

Can you pls help..!!


Combine multiple where clauses referencing component values

Posted: Thu Aug 08, 2013 5:45 pm
by Oleg Danchenkov

Your code looks corrupted. Row
codevar output = d.getFullYear()+'-'+((''+month).length./code
causes error
Please send us your code again and use tags <code></code> (https://getsatisfaction.com/some.html)
And please clarify what problem do you have.