Sign In Register

How can we help you today?

Start a new topic
Answered

Unable to remove _id in Collection Query

As the title says, I'm not able to remove the _id from a query.


My cloud code is:

  

var unitsCollection = Spark.metaCollection('Units');
var unitsCursor = unitsCollection.find({ "_id" : 0 });
var unitsList = [];

while(unitsCursor.hasNext())
{
	var unit = unitsCursor.next();  //value is now a single json document
	unitsList.push(unit);
}

Spark.setScriptData("units_Data", unitsList);

  Each document under Units meta-collection has this Json format:


 

 _id:
         $oid: "5a3fab5bc9e77c000150a17a"
 ID: "minir_u"
 Name: "Mini Rider"
 Ammount: 2
 HP: "120"
 MainDmg: "41"
 MainAtckSpeed: "1.2"
 SecondaryDmg: 0
 SecondaryAtckSpeed: 0
 EnergyCost: 3
 Speed: "1.25"
 Range/Radius: 5
 Type: "Troop"
 Rarity: "Common"
 Arena: 1
 Skill: 0

The code returns me an empty list.

 

If I remove the "{ "_id" : 0 }" inside Find() it works fine but it returns everything including _id which I don't need and breaks my Json-To-UnityObject parser.


I've tried different things but I can't make it work. Am I missing something?


Best Answer

Hi Ivan,


The issue here is you're using the field limit as your query. Try the following and it should return the documents without the id.


var unitsCursor = unitsCollection.find({},{ "_id" : 0 });

 

Meta collections are read/write on preview so you'll be able to test them in preview. I was just warning you that if your code was updating a meta collection in preview it would not work in live. If the code itself is simply for setting up your meta collection before publishing to live then that should be fine.


Regards,

Liam


Hi Ivan, 


Is this in preview or on live ? Meta Collections are read only on live.


Regards,

Liam

Hello Liam.


The game is in Preview.


This is the Event Response I get when I try to filter the _id out:


https://gyazo.com/5e377a3150968ad6f37d4e344a8a36d6 )



And this when I remove the filter parameter in Find():


https://gyazo.com/6ec1d01bf683ffc5b22790c8065bbe51 )



It works fine but it also returns the annoying _id


About "Meta Collections are read only on live."


Does that mean that I'm forced to publish the game (go live) even when it's still in development to be able to read MetaCollections? It doesn't make sense to me. I want to make sure that all my metadata is parsed and cached correctly before going live.


Also, why using find() works and with find({ "_id" : 0 }) doesn't?


I have the feeling that I'm missing a paramenter or something.


Answer

Hi Ivan,


The issue here is you're using the field limit as your query. Try the following and it should return the documents without the id.


var unitsCursor = unitsCollection.find({},{ "_id" : 0 });

 

Meta collections are read/write on preview so you'll be able to test them in preview. I was just warning you that if your code was updating a meta collection in preview it would not work in live. If the code itself is simply for setting up your meta collection before publishing to live then that should be fine.


Regards,

Liam


1 person likes this

Ohh I see, I knew I was missing something...


Now it works fine. Also, good to know the live/preview thing.


Thanks Liam!

Login to post a comment