0

我正在使用 mongodb(2.4.7 64 位 Linux 操作系统)来确定它是否可以很好地替代现有的 mysql 数据库。upsert 功能非常简洁 - 但我在这里遇到了问题。有人能解释一下为什么键“identification.YAHOO”=>“DTE.DE”没有被附加到数组中,而是被“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);

印刷

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

4

1 回答 1

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")
      )
)
于 2013-10-28T14:14:38.027 回答