2012-06-05 60 views
0

我有這個對象在我的分貝。

Array(
[_id] => MongoId Object 
    (
     [$id] => 4fcdb3b8749d786c03000002 
    ) 

[email] => [email protected] 
[folder] => Array 
    (
     [root] => Array 
      (
       [feeds] => Array 
        (
         [0] => Array 
          (
           [feedID] => MongoId Object 
            (
             [$id] => 4fcdaa9e749d786c03000001 
            ) 

           [title] => title.com 
          ) 

        ) 

       [title] => root 
      ) 

    ) 


[status] => 1 
[username] => foouser) 

我想從[飼料]拉項目[0]。 我用這個代碼,但它不工作:

$usersCollection->update(array("username" => "foouser"), array('$pull'=> array('folder'=>array('root'=>array('feeds'=>array('feedID'=>$id)))))); 

當我使用這個價值未設置的$拉相反,充滿了[文件夾]將會消失。

+0

只是一個猜測,但你的'$ id'一個字符串,或適當的MongoID?即'$ real_id = new MongoId(「$ id」);'?? http://www.php.net/manual/en/mongoid.construct.php –

+0

這是一個MongiID,這不是問題。我無法拉根也:$ usersCollection-> update(array(「username」=>「foouser」),array('$ pull'=> array('folder'=>'root'))); – Arash

回答

1

您需要指定數組元素的完整值。您還必須使用'folder.root.feeds',因爲這是您要從中拉出的字段。您不希望拉動folder中的數組元素不受影響。

在外殼上,你可以運行:

db.so.update(
    { 'username' : 'foouser' }, 
    { $pull: 
     { 'folder.root.feeds' : 
      { 
       "feedId" : ObjectId("4fcdaa9e749d786c03000001"), 
       "title" : "title.com" 
      } 
     } 
    } 
); 

而在PHP中,這將轉化爲:

$c->update(
    array('username' => 'foouser'), 
    array('$pull' => 
     array('folder.root.feeds' => 
      array(
       'feedId' => new MongoId('4fcdaa9e749d786c03000001'), 
       'title' => 'title.com', 
      ) 
     ) 
    ) 
); 
+0

仍然不能正常工作 – Arash

+0

適用於我:http://derickrethans.nl/files/dump/so-10893490.php.txt你有最近的MongoDB版本嗎? – Derick

+0

只是複製你的代碼和working.I認爲問題是我的insert.another問題,如何我可以使用$,如果我不知道'根'?文件夾。$。供稿? – Arash