Sign In Register

How can we help you today?

Start a new topic
Answered

Insert JSON

Hi,

    I would like inserting informations into my metacollections. I would like this, but the data I receive it's not in this format


{idPlayer :

 {

    idGame :

      {

         "Questions" :

                 {

                    noQuestion :

                          {

                              "good" : true,

                              "time" : 1.5854,

                              "count" : 2

                          }

                     noQuestion :

                          {

                              "good" : false,

                              "time" : 2.9654,

                              "count" : 1

                          }

                  }

       }

    }

}


but when I receive the data and insert 

var doc = { playerID : { idGame : {"Questions" : { noQuestion : {}}}}};.... 

 gamesparks put quotes (") on "playerID", "idGame" and "noQuestion" 


Best Answer
oh!

My apologies, i misunderstood.
Of course you can do that, check out the snippet below...


 

var a = 1788;
var b = 400;
doc = {};
doc[b.toString()] =  a;

 

 


Thanks !

Answer
oh!

My apologies, i misunderstood.
Of course you can do that, check out the snippet below...


 

var a = 1788;
var b = 400;
doc = {};
doc[b.toString()] =  a;

 

 

What do you mean ?


Here some code


var idGame =  myArray["idGame"]  (where myArray["idGame"] = 1788)

var playerID = responses[item]["NoMembre"] (where  responses[item]["NoMembre"] = 400)


docInsert = { playerID : { idGame : {}}};

 Spark.metaCollection("QuestionsMembres").insert(docInsert);


gives me :

 

"playerID" : {

       "idGame" : {

        }

}


and I would like to have :


"400" : {

     "1788" : {

      }

}


Thanks again

Hey guys,

You can try db.collection.Insert() or db.collection.FindAndModify() if you like?
But unless you already have those in your form, you will have to use double quotes to insert them.

Sean


 

Hi,

    I think you misunderstand me.

idGame, idPlayer and noQuestion are variables

I send something like this :

{"idGame" : 1788, "reponses" : {"reponse0" : {"BonneReponse" : true,"timeQuestion" : 1.586113,"NoMembre" : 400,"NoQuestion" : 11},"reponse1" : {"BonneReponse" : false,"timeQuestion" : 2.191174,"NoMembre" : 229,"NoQuestion" : 2},"reponse2" : {"BonneReponse" : true,"timeQuestion" : 1.879101,"NoMembre" : 400,"NoQuestion" : 16},"reponse3" : {"BonneReponse" : true,"timeQuestion" : 3.341267,"NoMembre" : 229,"NoQuestion" : 10},"reponse4" : {"BonneReponse" : true,"timeQuestion" : 1.837307,"NoMembre" : 400,"NoQuestion" : 6},"reponse5" : {"BonneReponse" : true,"timeQuestion" : 2.163567,"NoMembre" : 229,"NoQuestion" : 14},"reponse6" : {"BonneReponse" : true,"timeQuestion" : 2.315758,"NoMembre" : 400,"NoQuestion" : 15}}}

}

I recover idPlayer et all responses.

var myArray = Spark.getData().theArray;

var idGame = myArray["idGame"];

var responses = myArray["reponses"];

for (var item in responses)

{

    var bIsInsert = false;

    var docInsert;

    var reponse = responses[item];

    var playerID = responses[item]["NoMembre"];

    var noQuestion = responses[item]["NoQuestion"];

   }

how can I do to insert idPlayer like 401 and not "idPlayer"

(same thing for NoQuestion and idGame)

Thnaks

Hey Guys,

This is standard behavior in order for the document to be parsed properly when preforming queries with mongoDB.
You can receive or query these element by wrapping them in double-quotes too.

Sean

 

Login to post a comment