开发者

Perl mongodb remove record question

use the following code, delete the specified document.

sub delete_post{
my $post_id = shift;
my $post_to_delete = $posts->find_one({"_id" => $conn->oid($post_id)})->{开发者_C百科_id};
$posts->remove({"_id" => $post_to_delete});

}

if use this code:

sub delete_post{
my $post_id = shift;
$posts->remove({"_id" => $conn->oid($post_id)});

}

remove all documents.

Does mongodb cannot accept the oid as criteria to delete document?


Use the MongoDB::OID method to create _id object instead of $conn->oid;

sub delete_post{
my $post_id = shift;
my $oid = MongoDB::OID->new(value => $post_id);
$posts->remove({"_id" => $oid});
$db->log->insert({"removed_post" => $post_id});

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜