开发者

Perl mongodb remove record question

开发者 https://www.devze.com 2023-02-20 09:20 出处:网络
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};

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

精彩评论

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