2012-07-28 59 views
1

我想知道什麼是MongoDB中「客戶服務」文檔的最佳佈局。我想過這樣的事情,但不確定。建議的MongoDB文檔結構

{ 
"userid":(MONGODBUSERID), 
"subject":"subjectofissue", 
"issue":"issue that the user has", 
"replies":[{ 
    "whoreplied":"staff/customer", 
    "uid":"userid of replier", 
    "msg":"msg of reply" 
}] 
} 

這是最好的方法嗎?如果是的話,我將如何更新PHP的答覆陣列?我需要插入到回覆數組中而不覆蓋過去的回覆。

我綁這一點,但有以下錯誤


致命錯誤:未捕獲的異常「MongoException」有消息「零長度的密鑰是不允許的,您使用了$用雙引號?在/home/MYSITE/public_html/core.php:347 堆棧跟蹤:

我的代碼

$array = array(
    "$push" => array(
     "replies" => array(
      "whoreplied" => "user", 
      "uid" => new MongoId($this->data['uid']), 
      "msg" => $this->data['issue'] 
     ) 
    ) 
); 

$collection->update(array(
    "_id" => new MongoId($this->data['customerID']) 
), $array); 
+0

錯誤消息給了你正確的提示..你想'$ push'把它作爲一個命令發送到MongoDB服務器,而不是「$ push」,它將評估$ push變量的內容。 – Stennie 2012-07-28 22:53:20

回答

2

你需要圍繞與單引號(')代替雙引號線2的$push命令('")。

$array = array(
    '$push' => array(
     "replies" => array(
      "whoreplied" => "user", 
      "uid" => new MongoId($this->data['uid']), 
      "msg" => $this->data['issue'] 
     ) 
    ) 
); 

雙引號將導致PHP治療$push作爲變量,並試圖與變量的值來代替它。

+0

我會給一個去 – RussellHarrower 2012-07-28 11:54:39

+0

我得到錯誤閱讀我的編輯 – RussellHarrower 2012-07-28 13:01:47

+0

@RussellHarrower更新回答 – 2012-07-28 13:08:47