Php 获取我们插入的mongodb文档的"u id",如果它没有';t不存在,如果已存在则更新

Php 获取我们插入的mongodb文档的"u id",如果它没有';t不存在,如果已存在则更新,php,mongodb,mongodb-php,Php,Mongodb,Mongodb Php,我有一个在{LISTID:1,EMAIL:1}上具有唯一索引的集合订阅者。 如果文档不存在,我想插入它,如果已经存在,我想更新它,但在任何情况下,我都想获得文档的_id,不管它是插入的还是更新的 $mongo = new Mongo(); $db = $mongo->test; $collection = $db->subscribers; $criteria = array('LISTID' => 86, 'EMAIL' => 'opp20071980@gmail.c

我有一个在{LISTID:1,EMAIL:1}上具有唯一索引的集合订阅者。 如果文档不存在,我想插入它,如果已经存在,我想更新它,但在任何情况下,我都想获得文档的_id,不管它是插入的还是更新的

$mongo = new Mongo();
$db = $mongo->test; 
$collection = $db->subscribers;
$criteria = array('LISTID' => 86, 'EMAIL' => 'opp20071980@gmail.com');
$data = array('LISTID' => 86, 'EMAIL' => 'opp20071980@gmail.com', 'FNAME' => 'Oleg');
$result = $collection->update($criteria, $data, array('fsync' => true, 'upsert' => true));    
var_dump($data);
var_dump($result);
如果插入文档,我将得到以下结果:

array
  'LISTID' => int 86
  'EMAIL' => string 'opp20071980@gmail.com' (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 'opp20071980@gmail.com' (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?

请使用
$set
更改
$data
变量,然后重试

$data =  array('$set' => array('LISTID' => 86, 'EMAIL' => 'opp20071980@gmail.com', 'FNAME' => 'Oleg'));

这里的问题是
MongoCollection.update()
不会返回
\u id
(就像
MongoCollection.insert()

如果数据库中没有匹配项,并且您有
upsert=>true
,则在
upserted
对象中会得到一个
id
。如果有匹配项,则不会

如果要更新或插入单个文档,可以使用
查找和修改
命令和
向上插入
(在v.1.3.0-beta中添加了帮助程序)

两种情况下的结果将不同,请参见此处:

找到并更新记录:

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

            [LISTID] => 86
            [EMAIL] => opp20071980@gmail.com
        )

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

    [ok] => 1
)
未找到记录,请插入:

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

        )

    [ok] => 1
)

您必须在两个不同的位置获取
\u id
,这取决于Find和Modify是否插入或更新了文档。

它不起作用。结果是相同的:数组'UpdateExisting'=>布尔真'n'=>int 1'连接id'=>int 10'fsyncFiles'=>int 7'err'=>null'ok'=>float 1的结果是什么de>var_dump($collection->findOne(数组('LISTID'=>86,'EMAIL'=>)opp20071980@gmail.com“);
类似这样的内容:数组“EMAIL”=>string”opp20071980@gmail.com'(length=21)'FNAME'=>string'Oleg'(length=4)'LISTID'=>int 86'\u id'=>object(MongoId)[6]public'$id'=>string'50645fe9e0dae94a0bd25d07'(length=24)如果该文档已经存在,它将返回文档。但是该文档存在,并且此操作将更新该文档,则不会返回id。请尝试在更新后打印\r
$criteria
$data
,并检查其中是否包含
[\u id]
。我对此不确定。
Array
(
    [value] => 
    [lastErrorObject] => Array
        (
            [updatedExisting] => 
            [n] => 1
            [upserted] => MongoId Object
                (
                    [$id] => 5064708213995e82a829753e
                )

        )

    [ok] => 1
)