wiitohzjeh
wiitohzjeh

Reputation: 347

How to update Object in mongo via PHP

I've asked to update an Object in mongo, (I am kinda new in NoSQL) so.. much I know is from guides and so, currently I am using this Codeigniter MongoDB library which simplifies the task.

So, this is the object I have stored in MongoDB:

{
    "_id": ObjectID("5cfdc59844d81560d407a2e3"),
    "equipo": "2bdca4c0-854d-4f73-bca8-cfb78a31f928",
    "jugadores": {
        "5361aa85-df9c-4099-8157-fd4d558622cc": {
            "tipo": "i",
            "estado": false,
            "nombre": "name",
            "apellido_st": "last_name",
            "apellido_nd": "second_last_name",
            "email": "[email protected]"
        }
    }
}

And this is the 'query' I am using to update it.

$this->mongo->where(array
(
    "equipo" => "2bdca4c0-854d-4f73-bca8-cfb78a31f928",
    "jugadores" => "5361aa85-df9c-4099-8157-fd4d558622cc"
))->set(array("estado" => true))->update("torneos_inscripciones");

Obviously.. isn't working and I can't find why is the problem exactly, the estado field isn't getting updated; probably is more simple than I though but can't get into it. The purpose is to update the estado field based on the jugadores uuid, in this case 5361aa85-df9c-4099-8157-fd4d558622cc


Based in B. Fleming answer, I made a change to the code:

$this->mongo->where(array(
    "equipo" => "2bdca4c0-854d-4f73-bca8-cfb78a31f928",
    "jugadores.5361aa85-df9c-4099-8157-fd4d558622cc" => array(
        '$exists' => true
)))->set(array("jugadores.5361aa85-df9c-4099-8157-fd4d558622cc" => array("estado" => true)))->update("torneos_inscripciones");

The problem now is making the array empty only showing the estado field.

Upvotes: 0

Views: 122

Answers (1)

B. Fleming
B. Fleming

Reputation: 7230

I'm not having much luck finding any good documentation for the CodeIgniter methods. Their documentation is terrible. From what I can find, however, I suspect that your problem is in your where query. Specifically, it looks like you're searching for the value 5361aa85-df9c-4099-8157-fd4d558622cc for the key jugadores. The problem with this lookup is that 5361aa85-df9c-4099-8157-fd4d558622cc is a field, not a value, so neither MongoDB nor CodeIgniter would know how to handle this lookup.

You might have more success by checking for the existence of the key 5361aa85-df9c-4099-8157-fd4d558622cc, rather than trying to match it as a value:

$this->mongo->where(array(
    "equipo" => "2bdca4c0-854d-4f73-bca8-cfb78a31f928",
    "jugadores.5361aa85-df9c-4099-8157-fd4d558622cc" => array(
        '$exists' => true
    )
))->set(array(
    "jugadores.5361aa85-df9c-4099-8157-fd4d558622cc.estado" => true
))->update("torneos_inscripciones");

Please note that I used a single-quoted string, i.e. '$exists' instead of "$exists". This is important, because in PHP if you use double-quoted strings, words prefixed with a $ will be treated as a variable and evaluate the result before inserting into the string. Using single-quoted strings ensures that the value is not interpreted as a variable.

Upvotes: 1

Related Questions