私はブログシステムを書いています。現在、投稿へのコメントに取り組んでいます。すべての投稿のスキーマは次のとおりです。
{
"topic": "Post Topic",
"post": "<p>\r\n\tPost text</p>\r\n",
"time_added": 1343167025,
"author": "Ashley Brooks",
"_id": {
"$oid": "500f1a315759c73805000001"
}
}
それでは、この投稿にコメントを追加したいと思います。コメントを保存する最良の方法は、次のようなものだと思います。
{
"topic": "Post Topic",
"post": "<p>\r\n\tPost text</p>\r\n",
"time_added": 1343167025,
"author": "Ashley Brooks",
"_id": {
"$oid": "500f1a315759c73805000001"
},
"comments" : [
{
"author":"James Brown",
"comment":"My awesome comment"
},
{
"author":"Jimmy White",
"comment":"And this is my comment"
}
]
}
Mongoのドキュメントをいくつか読みましたが、新しいコメントを追加する適切な方法を見つけることができませんでした。これが私が今している方法です:
exports.post = function(req, res) {
if (req.currentUser) {
db.collection("posts", function (err, collection) {
var obj_id = BSON.ObjectID.createFromHexString(req.params.id);
console.log(obj_id);
collection.findAndModify(
{_id: obj_id}, // query for a post
{$push: {
comments: [ {author: req.body.comment, name: "testname" } ]
}
},
function(err, object) {
if (err){
console.warn(err.message);
}else{
res.redirect('/');
}
});
});
} else {
res.redirect('/');
}
};
それは私にそのようなエラーを返します:
exception: must specify remove or update
私が間違っているのは何ですか?
PSところで、そのような一意のIDまたはsmthで個別のコメントをマークするとよいと思います。コメントを追加するたびにobj_idパラメーターを追加するようにMongoを指定するにはどうすればよいですか?前もって感謝します。