本文介绍了Perl mongodb删除记录问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,删除指定的文档.

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)})->{_id};
$posts->remove({"_id" => $post_to_delete});

}

如果使用此代码:

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

}

删除所有文档.

mongodb不能接受oid作为删除文档的条件吗?

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

推荐答案

使用MongoDB :: OID方法创建_id对象,而不是$ conn-> oid;

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});

}

这篇关于Perl mongodb删除记录问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 17:03