2013-10-28 89 views
0

我正在玩mongodb(2.4.7 64位Linux操作系統)來弄清楚它是否會替代現有的mysql數據庫。漂亮整潔是upsert功能 - 但我在這裏有一個問題。有人可以解釋爲什麼鑰匙「identification.YAHOO」=>「DTE.DE」不會附加到數組,但會被「DETGY」相關嗎?嵌入式陣列的upsert缺失值

<?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); 

打印

[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 
         ) 

       ) 

     ) 

正如你所看到的關鍵DTE.DE丟失。

回答

0

PHP中的關聯數組每個鍵只能有一個條目。所以,當你創建一個PHP數組是這樣的:

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

您使用的關鍵"identification.YAHOO"第二次它取代了數組中的第一個值。

幸運的是,MongoDB中的$addToSet運算符可以與$each運算符結合使用,該運算符允許您爲一個鍵傳遞整個數組值,而不僅僅是一個鍵值。我的PHP有點生疏,但這應該工作:

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

哦,對我很恥辱,我監督了不同數組鍵的事實......這工作相當好謝謝! '$ addToSet'=> array( 「identification.WKN」=>「555750」, 「identification.ISIN」=>「DE005557504」, 「identification.YAHOO」=> array('$ each'=> array( 「DTE.DE」,「DTEGY」)) ) – KIC