Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl mongodb删除记录问题_Perl_Mongodb - Fatal编程技术网

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作为键的另一个问题。