Sign In Register

How can we help you today?

Start a new topic

Foreach using Find

 Need to iterate through every single log in a runtime collection and add 1 to the variable "acti"



 

 var progressCollection = Spark.runtimeCollection("Tribes");

         
         foreach(var d in progressCollection.find({"tribe.name": ""}))
         {
            doc.acti ++;
         }

 

Getting errors on the "foreach" line. I'm not sure any of it is correct.


You shouldn't use toArray() for performance reasons.

Loop over the result like this:

 

var progressCollection = Spark.runtimeCollection("Tribes");
var cursor = progressCollection.find({"tribe.name": ""});
var doc;
while(cursor.hasNext())
{
    doc = cursor.next();
    doc.acti++;
}

 

SparkMongoCollectionReadWrite.find() does not return an array - it returns a SparkMongoCursor.    Instead try progressCollection.find({"tribe.name": ""}).toArray()
Login to post a comment