Shaun Summers
Posts: 0
Joined: Thu Jul 25, 2013 3:49 pm

Combine multiple where clauses referencing component values

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.

Tommy B
Posts: 0
Joined: Sat Sep 15, 2012 3:39 am

Combine multiple where clauses referencing component values

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

Shaun Summers
Posts: 0
Joined: Thu Jul 25, 2013 3:49 pm

Combine multiple where clauses referencing component values

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 :)

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Combine multiple where clauses referencing component values

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.

Shaun Summers
Posts: 0
Joined: Thu Jul 25, 2013 3:49 pm

Combine multiple where clauses referencing component values

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.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Combine multiple where clauses referencing component values

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.

Shaun Summers
Posts: 0
Joined: Thu Jul 25, 2013 3:49 pm

Combine multiple where clauses referencing component values

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

vipul
Posts: 0
Joined: Wed Jul 31, 2013 8:59 am

Combine multiple where clauses referencing component values

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

vipul
Posts: 0
Joined: Wed Jul 31, 2013 8:59 am

Combine multiple where clauses referencing component values

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..!!

Oleg Danchenkov
Posts: 0
Joined: Tue Apr 30, 2013 5:51 pm

Combine multiple where clauses referencing component values

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.

Return to “Issues”