Sign In Register

How can we help you today?

Start a new topic
Answered

Cloud Code Combine Query Results

I see in code examples how to return a query result (findOne) or many results (find). But how do I take two queries and combine them into one result?

 

var numLimit = Spark.getData().NumLimit; // get the limit from the client
var rand = Math.random();
var myData = Spark.runtimeCollection("myData"); // get the collection data

var resultsGt = myData .find({
    	"random": { $gte : rand }
    }).limit(numLimit).sort({"random":1}); // search the collection data for a random entry greater than or equal to the random number

var resultsLt = myData.find({
    	"random": { $lte : rand }
    }).limit(numLimit).sort({"random":-1}); // search the collection data for a random entry less than or equal to the random number

var resultCombined = // How do I combine resultsLt and resultsGt?
Spark.setScriptData("results_Data", resultCombined);

 I suppose I could just call Spark.setScriptData once for each result.

Spark.setScriptData("resultsGt_Data", resultGt);
Spark.setScriptData("resultsLt_Data", resultLt);

Would this be the recommended approach?


Best Answer
Hey Jason,

You could combine these into an array and send that array back in the response script-data. Does that work for you?

var results = [];
results.push(resultsGt);
results.push(resultsLt);
Spark.setScriptData("results_Data", results);

Let me know if this works.

Sean

 

1 Comment

Answer
Hey Jason,

You could combine these into an array and send that array back in the response script-data. Does that work for you?

var results = [];
results.push(resultsGt);
results.push(resultsLt);
Spark.setScriptData("results_Data", results);

Let me know if this works.

Sean

 

Login to post a comment