2012-09-27 69 views
1

我有一个收集订阅者,它在{LISTID:1,EMAIL:1}上具有唯一索引。 我想插入一个文件,如果它不存在,并更新它,如果已经存在,但在任何情况下,我想获取文档的_id无关紧要,如果它插入或更新。获取我们插入的mongodb文档的_id,如果它不存在并更新(如果它已经存在)

$mongo = new Mongo(); 
$db = $mongo->test; 
$collection = $db->subscribers; 
$criteria = array('LISTID' => 86, 'EMAIL' => '[email protected]'); 
$data = array('LISTID' => 86, 'EMAIL' => '[email protected]', 'FNAME' => 'Oleg'); 
$result = $collection->update($criteria, $data, array('fsync' => true, 'upsert' => true));  
var_dump($data); 
var_dump($result); 

如果文档被插入,我得到的结果是:

array 
    'LISTID' => int 86 
    'EMAIL' => string '[email protected]' (length=21) 
    'FNAME' => string 'Oleg' (length=4) 

array 
      'updatedExisting' => boolean false 
      'upserted' => 
      object(MongoId)[6] 
       public '$id' => string '506446e4e0dae94a0bd25d06' (length=24) 
      'n' => int 1 
      'connectionId' => int 10 
      'fsyncFiles' => int 7 
      'err' => null 
      'ok' => float 1 

但是,如果它的更新,我得到的结果,而不_id:

array 
    'LISTID' => int 86 
    'EMAIL' => string '[email protected]' (length=21) 
    'FNAME' => string 'Oleg' (length=4) 
array 
    'updatedExisting' => boolean true 
    'n' => int 1 
    'connectionId' => int 10 
    'fsyncFiles' => int 7 
    'err' => null 
    'ok' => float 1 

你能告诉我怎么去_id即使记录更新,但没有插入?

回答

2

这里的问题是,MongoCollection.update()不会返回_id(就像MongoCollection.insert()

如果在数据库中没有比赛,你有upsert=>true,你会得到一个idupserted如果有匹配

如果要更新或插入单个文档,可以使用findAndModify命令upsert(帮手v.1.3.0-β加)

$mongo = new Mongo(); 
$db = $m->mydatabase; 
$query = array('LISTID' => 86, 'EMAIL' => '[email protected]'); 
$update = array('$set' => array('LISTID' => 86, 'EMAIL' => '[email protected]', 'FNAME' => 'Oleg')); 

$result = $db->command(
    array(
    "findandmodify" => "test",//name of collection 
    "query" => $query, 
    "update" => $update, 
    'upsert' => 1 
) 
); 

在这两种情况下,结果会有所不同,在这里看到:

记录发现,更新:

Array 
(
    [value] => Array 
     (
      [_id] => MongoId Object 
       (
        [$id] => 506470963e20d69457000000 
       ) 

      [LISTID] => 86 
      [EMAIL] => [email protected] 
     ) 

    [lastErrorObject] => Array 
     (
      [updatedExisting] => 1 
      [n] => 1 
     ) 

    [ok] => 1 
) 

发现没有记录,插入:

Array 
(
    [value] => 
    [lastErrorObject] => Array 
     (
      [updatedExisting] => 
      [n] => 1 
      [upserted] => MongoId Object 
       (
        [$id] => 5064708213995e82a829753e 
       ) 

     ) 

    [ok] => 1 
) 

根据findAndModify是否插入或更新文档,您必须在两个不同的位置获得_id

+0

谢谢。我会试试看。 – Oleg

+0

在我看来,它的工作原理。 – Oleg

0

更改$data变量与$set并再试一次。

$data = array('$set' => array('LISTID' => 86, 'EMAIL' => '[email protected]', 'FNAME' => 'Oleg')); 
+0

它不起作用。结果是相同的:阵列 'updatedExisting'=>布尔真 'N'=> INT 1 '的ConnectionId'=> INT 10 'fsyncFiles'=> INT 7 'ERR'=>空 'OK' => float 1 – Oleg

+0

var_dump($ collection-> findOne(array('LISTID'=> 86,'EMAIL'=>'[email protected]')));' –

+0

这样的结果会是什么样的:array 'EMAIL'=> string'[email protected]'(length = 21) 'FNAME'=> string'Oleg'(length = 4) 'LISTID'=> int 86 '_id'=> object(MongoId)[6] public'$ id'=> string'50645fe9e0dae94a0bd25d07'(length = 24) – Oleg

相关问题