2012-04-21 4 views
0

단일 차원 배열로 업데이트하지만 다차원 배열 (또는 중첩 배열)을 사용하면 업데이트되지 않습니다. 문서 데이터, BSON 필드 (찾기 키 역할을 함) 및 콜렉션이 반환됩니다. 어떤 생각을 잘못하고있는 걸까요?PHP는 중첩 된 배열을 mongoDB에 삽입합니다.

Public Static function updateDocument($collection, $BSONfield, $document){ 
    $dbcollection = $db->selectCollection($collection); 
    $sdata = $document[$BSONfield]; 
    $secureInnerDocument = array(); 
      $secureDocument = array(); 
    if($BSONfield == "_id"){ 
     $sdata = new MongoID($sdata); 
     unset($document["_id"]); 
    } 
    $filter = array($BSONfield=>$sdata); 
    foreach ($document as $k => $v) { 
     if (is_array($v)) { 
      foreach ($v as $sk => $sv) { 
       $secureInnerDocument[$sk] = Security::secureQuery($sv); 
      } 
       $secureDocument[$k] = $secureInnerDocument; 
     }else{  
      $secureDocument[$k] = Security::secureQuery($v); 
     } 
    } 
    $dbcollection->update($filter,array('$set'=>$secureDocument)); 
    $objid = (string) $secureDocument['_id']; 
    return $objid; 
} 

답변

1

:

$collection->update(
    array('fieldNameFilter'=>'filterValue'), 
    array($set => array('stringFieldName'=>$value)) 
); 

그런 다음 몇 가지가 있습니다 :

db.collection.update(
    {fieldNameFilter:'filterValue'}, 
    {$set: {'stringFieldName' : 'newValue'}} 
); 

은에 변환 다중 행 업데이트를위한 플래그 등이 여기에 표시되지 않지만 PHP 및 Mongo 문서에 있습니다. 내가 그 PHP로 변환하는 방법에 문제가 있어요 그래 MongoDB - help with a PHP query

0

내 솔루션이 janky가되어 버린 후 문서를 삭제 한 다음 다시 작성합니다.

/** 
* updates document in the collection. 
* This function secures the data 
* 
* @return object ID 
* @param string $collection The name of the collection 
* @param string $BSONfield The $BSON Field you want to index by 
* @param string $document The document contents as an array 
*/ 
Public Static function updateDocument($collection, $BSONfield, $document){ 
    $db = Database::dbConnect(); 
    $collection = Security::secureQuery($collection); 
    $BSONfield = Security::secureQuery($BSONfield); 
    $dbcollection = $db->selectCollection($collection); 
    if(array_key_exists('_id', $document)){ 
     $document["_id"] = new MongoID($document["_id"]); 
    } 
    Database::deleteDocument($collection, $BSONfield, $document); 
    $objid = Database::createDocument($collection, $document); 
    return $objid; 
} 

/** 
* Deletes a document in the collection. 
* This function secures the data 
* 
* @return Boolean  True - if successfully deleted, False if document not found 
* @param string $collection The name of the collection 
* @param string $BSONfield The $BSON Field you want to index by 
* @param string $document The document contents as an array 
*/ 
Public Static function deleteDocument($collection, $BSONfield, $document){ 
    $db = Database::dbConnect(); 
    $collection = Security::secureQuery($collection); 
    $BSONfield = Security::secureQuery($BSONfield); 
    $exists = False; 
    $dbcollection = $db->selectCollection($collection); 
    $documentList = $dbcollection->find(); 
    $sdata = $document[$BSONfield]; 
    if($BSONfield == "_id"){ 
     $sdata = new MongoID($sdata); 
    } 
    foreach ($documentList as $doc) { 
     $documentID = $doc[$BSONfield]; 
     if ($documentID == $sdata){ 
      $exists = True; 
     } 
    } 
    if ($exists){ 
     $deleted = True; 
     $filter = array($BSONfield=>$sdata); 
     $dbcollection->remove($filter,true); 
    }else{ 
     $deleted = False; 
    } 
    return $deleted; 
} 


/** 
* Inserts document into the collection. 
* This function secures the data 
* 
* @return object ID. 
* @param string $collection The name of the collection 
* @param string $document The document contents as an array 
*/ 
Public Static function createDocument($collection, $document){ 
    $db = Database::dbConnect(); 
    $collection = Security::secureQuery($collection); 
    $dbcollection = $db->selectCollection($collection); 
    $secureDocument = array(); 
    $secureInnerDocument = array(); 
    foreach ($document as $k => $v) { 
     if (is_array($v)) { 
      foreach ($v as $sk => $sv) { 
       $secureInnerDocument[$sk] = Security::secureQuery($sv); 
      } 
       $secureDocument[$k] = $secureInnerDocument; 
     }else{ 
      if ($k == '_id'){ 
       $secureDocument[$k] = $v; 
      }else{ 
       $secureDocument[$k] = Security::secureQuery($v); 
      } 
     } 
    } 
    $dbcollection->insert($secureDocument); 
    $objid = (string) $secureDocument['_id']; 
    return $objid; 
} 

을하고 내가 어떻게 주사에서 모든 데이터를 보호하고 있습니다 : 여기 경우 사람의 코드를 찾고있다

/** 
* Secures string to be inputed into a database. 
* 
* @return Retuns secure string 
* @param String $string String to be secured 
*/ 
Public Static Function secureQuery($string){ 
    $secureString = strtr($string, array(
      "'" => "0x27", 
      "\"" => "0x22", 
      "\\" => "0x5C", 
      "<" => "0x3C", 
      ">" => "0x3E", 
      "=" => "0x3D", 
      "+" => "0x2B", 
      "&" => "0x26", 
      "{" => "0x7B", 
      "}" => "0x7D", 
    )); 
    return $secureString; 
} 

/** 
* Un-Secures string to be inputed into a database. 
* 
* @return Retuns unsecure string 
* @param String $string String to be un-secured 
*/ 
Public Static Function unsecureQuery($string){ 
    $secureString = strtr($string, array(
      "0x27" => "'", 
      "0x22" => "\"", 
      "0x5C" => "\\", 
      "0x3C" => "<", 
      "0x3E" => ">", 
      "0x3D" => "=", 
      "0x2B" => "+", 
      "0x26" => "&", 
      "0x7B" => "{", 
      "0x7D" => "}", 
    )); 
    return $secureString; 
} 

즐기세요!

0

$set 연산자를 올바르게 사용하지 않는 것 같습니다. MongoDB docs에 따라 업데이트 문서의 형식을 지정해야합니다.

{$ 세트 : {필드 : 값}}

중첩 된 키에 $set를 실행하는 경우, 당신은 그들에 도착하는 점 표기법을 사용해야합니다. 예를 들어;

{$ 세트 : {field.nest : 값}} 그것은 매우 직접 변환

+0

: 또한보고 할 수 있습니다

. 중첩 배열 "$ set"중첩 배열의 키를 만들 수 있습니까? (Key) = array ('$ set'=> myNestedArray) 또는 myArray [Key] = array ('$ set'=> 배열 ($ BSONfield => myNestedArray)? – JBurlison

+0

아니요, 나의 두번째 예제'array ('$ set'=> array ('nested.field'=> value)'' –

관련 문제