【问题标题】:Suggested MongoDB document structure建议的 MongoDB 文档结构
【发布时间】:2012-07-28 11:47:09
【问题描述】:

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

【问题讨论】:

  • 错误消息给了你正确的提示..你希望 '$push' 将它作为命令发送到 MongoDB 服务器,而不是 "$push" 它将评估 @ 的内容987654323@ 变量。

标签: php json mongodb


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2019-03-17
    • 2016-07-24
    • 2011-02-19
    • 2016-02-29
    • 2018-08-23
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多