0

I am trying to add a third level of data but I am not being able to add it and then retrieve the data using the identifier.

Here is the structure of the data:

db.rooms.find({"_id" : "1620888265"}).pretty();
{
    "_id" : "1620888265",
    "start" : 1620988265,
    "end" : null,

    "users" : [
                {
                "name" : "eric casanova",
                "ip" : "127.0.0.1",
                "uid" : "ShasmANxKLgP6aI5.q38SlhdsHrauxDin2tu-JtpRqnR2iRm",
                "score" : 0.9,
                "singin" : 1620895469
                },
                {
                "name" : "eric casanova",
                "ip" : "127.0.0.1",
                "uid" : "ShasmANxKLgP6aI5.q38SlhdsHrauxDin2tu-JtpRqnR2iRm",
                "score" : null,
                "singin" : 1620895469
                }
        ],
    "questions" : [
                1,
        ]
}

This is what I would like to achieve:

{
    "_id" : "1620888265hby",
    ...
    "questions" : [
        1: {
            "title" : "X",
            "released" : true
          }
    ]
}

or

{
    "_id" : "1620888265",
    ...
    "questions" : [
        {
            "id": 1
            "title" : "X",
            "released" : true
        }
    ]
}

The "1" would be the identifier of that object

I’ve tried so many things that I don’t even know what to use as a command example. I leave you the last thing I have on the console:

cursor =rooms_db->find(['_id' => nameRoom]); foreach (cursor as k =>row) {
   if (isset(row['questions'])) {counter += count(row['questions']);    } }question = [
   'ident'=>counter,    'title' =>_POST['title'],
   'released' => false
];
rooms_db->updateMany(array("_id" =>nameRoom), array('push' => array("questions" =>question)), array("upsert" => true));

Any help and ideas are welcome!

Anonymous Asked question May 13, 2021