Sign In Register

How can we help you today?

Start a new topic
Answered

.Count array from runtime

Hello everoye, it's a very simple question, how do i get the lenght from an array that i got from a runtime collection in cloud code?


var EXAMPLE = Spark.runtimeCollection("RuntimeCollection").findOne({"_id": Spark.getPlayer().getPlayerId()},{"EX":[]});


var COUNT = EXAMPLE.EX.Count //(this is not working)


Best Answer

Hi Alberto,


Count is an operation that you perform on a mongo cursor. A findOne query will only ever return one result. If you're trying to get the length of the array use EXAMPLE.EX.length.


Regards,

Liam



Answer

Hi Alberto,


Count is an operation that you perform on a mongo cursor. A findOne query will only ever return one result. If you're trying to get the length of the array use EXAMPLE.EX.length.


Regards,

Liam


Hello Liam,


var COUNT = EXAMPLE.EX.length; is not working

Hi Alberto,


Is the array "EX" inside of the object who's id is their playerId ? Your query looks wrong here. Currently its set up to find a document for that player that contains an empty array.


Regards,

Liam

In test harness, "EX" returns this:

image


and thats stored in a runtime collection like this:


"EX":[{"testValue":03},{"testValue":23},{"testValue":00},{"testValue":03}{"testValue":03},{"testValue":45}]


Hi Alberto,


In that case use this as your query to find the document.  


var EXAMPLE = Spark.runtimeCollection("RuntimeCollection").findOne({"_id": Spark.getPlayer().getPlayerId()});


Then use var COUNT = EXAMPLE.EX.length; to get the length.


Regards,

Liam

Thanks!

Login to post a comment