发表关于mongodb文章的评论

时间:2015-04-06 10:30:40

标签: php mongodb comments

我有问题我想在文章上发表评论,但不会出现任何评论。

这是文章集:

    {
    "_id": ObjectId("552028640faaf24450b7acda"),
    "title": "Hello World !",
    "content": "My new article is beautiful",
    "saved_at": ISODate("2015-04-04T18:07:32.519Z"),
    "comments": [
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null,
    null
    ]
}

我们可以看到,我不知道为什么评论为空。因此,当我在评论中评论时,只有:说......                           1月1日凌晨1点

  

comment.php:

<?php
$id = $_POST['article_id'];
try
    {
    $mongodb    = new MongoClient();
    $collection = $mongodb->myblogsite->articles;
    }
catch (MongoConnectionException $e)
    {
    die('Failed to connect to MongoDB ' . $e->getMessage());
    }
$article = $collection->findOne(array(
    '_id' => new MongoId($id)
));
$comment = array(
    'name' => $_POST['commenter_name'],
    'email' => $_POST['commenter_email'],
    'comment' => $_POST['comment'],
    'posted_at' => new MongoDate()
);
$collection->update(array(
    '_id' => new MongoId($id)
), array(
    '$push' => array(
    'comments' => $comments
    )
));
header('Location: blog.php?id=' . $id);
?>  
  

blog.php的:

        <?php
$id = $_GET['id'];
try
    {
    $connection = new MongoClient();
    $database   = $connection->selectDB('myblogsite');
    $collection = $database->selectCollection('articles');
    }
catch (MongoConnectionException $e)
    {
    die("Failed to connect to database " . $e->getMessage());
    }
$article = $collection->findOne(array(
    '_id' => new MongoId($id)
));
?>

和html表格如下:

<head>
    <meta http-equiv="Content-Type" content="text/html;

      charset=utf-8" />
    <link rel="stylesheet" href="style1.css" />
    <title>My Blog Site</title>
</head>

<body>
    <div id="contentarea">
    <div id="innercontentarea">
        <h1><?php echo $article['title']; ?></h1>
        <p>
            <?php echo $article[ 'content']; ?> </p>
        <div id="comment-section">
            <h3>Comments</h3>
            <?php if (!empty($article[ 'comments'])): ?>
            <h3>comments</h3>
            <?php foreach($article[ 'comments'] as $comment): echo $comment[ 'name']. ' says...';?>
            <p>
                <?php echo $comment[ 'comment']; ?> </p> <span>

        <?php echo date('g:i a, F j', $comment['posted_at']); ?>

      </span>
            <br/>
            <br/>
            <br/>
            <?php endforeach; endif;?>
            <h3>Post your comment</h3>
            <form action="comment.php" method="post"> <span class="input-label">Name</span>
                <input type="text" name="commenter_name" class="comment-input" />
                <br/>
                <br/> <span class="input-label">Email</span>
                <input type="text" name="commenter_email" class="comment-input" />
                <br/>
                <br/>
                <textarea class="input-label" name="comment" rows="5"></textarea>
                <br/>
                <br/>
                <input type="hidden" name="article_id" value="<?php echo $article['_id']; ?>" />
                <input type="submit" name="btn_submit" value="Save" /> </form>
        </div>
    </div>
    </div>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

我认为插入comments数组的问题,对于这个mongo $push有帮助。为此,我创建了以下方案如果您使用my first comment等评论填写表单,则应更新以下文档,如果comments键未显示,则使用mongo upsert and multi true表示创建comments密钥(如果不是presnts),false表示避免多文档更新。

db.collectionName.update({"_id" : ObjectId("552028640faaf24450b7acda")},{"$push":{"comments":"my first comment"}},true,false)

如果您想同时插入多条评论,请使用$pushAll以上查询替换为

db.collectionName.update({"_id" : ObjectId("552028640faaf24450b7acda")},{"$pushAll":{"comments":["my first comment","my second comment","my third comment"]}}},true,false)

或者如果您想设置创建单个评论的时间,请尝试此

db.collectionName.update({"_id" : ObjectId("552028640faaf24450b7acda")}, {"$push":{"comments":{"comment":"my first comment","commentTime": new Date()}}}},true,false)

如果使用不同的时间戳推送所有评论,则使用

db.collectionName.update({"_id" : ObjectId("552028640faaf24450b7acda")},{"$pushAll":{"comments":[{"comment":"A" ,"time":new Date()},{"comment":"B","time":new Date()}]}}},true,false)

或具有相同时间戳的所有评论然后尝试此

var currentsystemTime = new Date()
db.collectionName.update({"_id" : ObjectId("552028640faaf24450b7acda")},{"$pushAll":{"comments":[{"comment":"A" ,"time":currentsystemTime},{"comment":"B","time":currentsystemTime}]}}},true,false)