Perl mongodb删除记录问题
使用以下代码,删除指定的文档Perl mongodb删除记录问题,perl,mongodb,Perl,Mongodb,使用以下代码,删除指定的文档 sub delete_post{ my $post_id = shift; my $post_to_delete = $posts->find_one({"_id" => $conn->oid($post_id)})->{_id}; $posts->remove({"_id" => $post_to_delete}); } 如果使用此代码: sub delete_post{ my $post_id = shift; $posts
sub delete_post{
my $post_id = shift;
my $post_to_delete = $posts->find_one({"_id" => $conn->oid($post_id)})->{_id};
$posts->remove({"_id" => $post_to_delete});
}
如果使用此代码:
sub delete_post{
my $post_id = shift;
$posts->remove({"_id" => $conn->oid($post_id)});
}
删除所有文档
mongodb是否不能接受oid作为删除文档的标准?使用mongodb::oid方法来创建_id对象,而不是$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});
}为什么需要
$conn->oid
?下面的方法不起作用吗<代码>$posts->remove({“\u id”=>$post\u id})代码>灾难。第一种方法,随机删除一个文档或删除所有文档。第二种方法,删除none。是关于在find方法中不安全地使用_id作为键的另一个问题。