Sign In Register

How can we help you today?

Start a new topic
Answered

Collections Like Operator

Hello,


I'm trying to figure out how to use the like operator in a collection and the following works to find any levels that contain "Aw" in them:

var Levels = Spark.runtimeCollection('levels').find({"LevelName" : /Aw/});


However, how do I do this while passing in a variable?  If I change it to

var Levels = Spark.runtimeCollection('levels').find({"LevelName" : '/'+ 'Aw' + '/'});

or

var Levels = Spark.runtimeCollection('levels').find({"LevelName" : VariableLevelName});


it doesn't work.


Thanks

Andy


Best Answer

Hi Andy,


If you pass this in as a string variable you can convert it to a Regular Expression. For example if you use the following it should work.

    

//create an event with a string attribute called "value", the string is what you want to search for

//here myValue would = "Aw"
var myValue = Spark.getData().value;

//convert it to a RegExp
var re = new RegExp(myValue);   //re = /Aw/

//then use it in the search
var Levels = Spark.runtimeCollection('levels').find({"LevelName" : re});

    

Try that out and let me know how you get on, if you have any other question just let me know.


Thanks,

Liam


Answer

Hi Andy,


If you pass this in as a string variable you can convert it to a Regular Expression. For example if you use the following it should work.

    

//create an event with a string attribute called "value", the string is what you want to search for

//here myValue would = "Aw"
var myValue = Spark.getData().value;

//convert it to a RegExp
var re = new RegExp(myValue);   //re = /Aw/

//then use it in the search
var Levels = Spark.runtimeCollection('levels').find({"LevelName" : re});

    

Try that out and let me know how you get on, if you have any other question just let me know.


Thanks,

Liam

Awesome - that did it.  Thank you.

Login to post a comment