Frage

I am playing with mongodb (2.4.7 64bit Linux OS) to figure out if it would be a great replacement for a existing mysql database. Pretty neat is the upsert functionallity - but I have a problem here. Can someone explain me why the key "identification.YAHOO" => "DTE.DE" do not get appended to the array but will be relaced by "DETGY"?

<?php

  $oMongoClient = new MongoClient();
  $oMongoDB = $oMongoClient->test;
  $oMongo = $oMongoDB->item;

  $oMongo->update(
  array('$or' => array(
                   array("identification.WKN" => "555750"),
                   array("identification.ISIN" => "DE005557504"),
                   array("identification.YAHOO" => "DTE.DE"),
                   array("identification.YAHOO" => "DTEGY"),
                   array("_id" => "lalala")
                 )
  ),
  array(
   '$set' => array(
      "shortname" => "DT_TELEKOM",
      "name" => array(
        "de" => "Deutsche Telekom AG",
        "en" => "Deutsche Telekom AG Inc."
      ),
      "type" => "STOCK",
      "web" => "http://deutschetelemom.com",
      "valid_from" => "1998-01-01",
      "valid_to" => "9999-12-31",
      "inactive" => false,
      "translate_all" => false,
      "is_provisory" => false,
      "touched" => "25.02.2013 17:11:54"
    ),
    '$addToSet' => array(
      "identification.WKN" => "555750",
      "identification.ISIN" => "DE005557504",
      "identification.YAHOO" => "DTE.DE",
      "identification.YAHOO" => "DTEGY"
    )
  ),
  array("upsert" => true)
);

//$oMongo->ensureIndex(array('$**' => "text"));
$oMongo->ensureIndex(array('$**' => "text"));

$result = $oMongoDB->command(
    array(
        'text' => 'item', //this is the name of the collection where we are searching
        'search' => 'DTEGY'
    )
);

print_r($result);

prints

[results] => Array
        (
            [0] => Array
                (
                    [score] => 1
                    [obj] => Array
                        (
                            [_id] => MongoId Object
                                (
                                    [$id] => 526e647b7ebd4252592cfe52
                                )

                            [identification] => Array
                                (
                                    [ISIN] => Array
                                        (
                                            [0] => DE005557504
                                        )

                                    [WKN] => Array
                                        (
                                            [0] => 555750
                                        )

                                    [YAHOO] => Array
                                        (
                                            [0] => DTEGY
                                        )

                                )

                            [inactive] =>
                            [is_provisory] =>
                            [name] => Array
                                (
                                    [de] => Deutsche Telekom AG
                                    [en] => Deutsche Telekom AG Inc.
                                )

                            [shortname] => DT_TELEKOM
                            [touched] => 25.02.2013 17:11:54
                            [translate_all] =>
                            [type] => STOCK
                            [valid_from] => 1998-01-01
                            [valid_to] => 9999-12-31
                            [web] => http://deutschetelemom.com
                        )

                )

        )

As you can see the key DTE.DE is missing.

War es hilfreich?

Lösung

An associative array in PHP can only have one entry per key. So when you create a PHP array like this:

array(
      "identification.WKN" => "555750",
      "identification.ISIN" => "DE005557504",
      "identification.YAHOO" => "DTE.DE",
      "identification.YAHOO" => "DTEGY"
)

the second time you use the key "identification.YAHOO" it replaces the first value in the array.

Fortunately, the $addToSet operator in MongoDB can be combined with the $each-operator which allows you to pass a whole array of values for a key, not just a single one. My PHP is a bit rusty, but this should work:

array(
      "identification.WKN" => "555750",
      "identification.ISIN" => "DE005557504",
      "identification.YAHOO" => array(
          "$each" => array("DTE.DE", "DTEGY")
      )
)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top