1

我想知道 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);
4

1 回答 1

2

您需要将$push第 2 行的命令用单引号 ( ') 而非双引号 ( '") 括起来。

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

双引号将导致 PHP 将其$push视为变量并尝试将其替换为变量的值。

于 2012-07-28T11:50:05.023 回答