0

我的应用程序每次包含“使用 MongoDB”时都会崩溃;在我的 perl 应用程序文件中。

我已经成功安装了 MongoDB。我可以检查我的数据库使用一个或另一个,检查集合,创建新集合,所有这些都来自 shell。

如果我尝试从 mojolicious 应用程序连接到 mongoDb,例如:

!/usr/bin/env perl

use Mojolicious::Lite;
use MongoDB;
use MongoDB::OID;

my $mongo_port = shift || 27017;

helper 'mongo' => sub {
    my ($self, $name) = @_;
    my $host = 'localhost:' . $mongo_port;
    my $conn = MongoDB::MongoClient->new(host => $host);
    my $db = $conn->get_database('test');
};

helper 'value2oid' => sub {
    my ($self, $value) = @_;
    MongoDB::OID->new($value);
};

如果我有一个工作应用程序并包括:

Use MongoDB;

我得到:

Can't load application from file "/Users/eevitomperi/Desktop/Programming/Perl/mojoliciousApp/foodAbout/app.pl": Can't locate MongoDB.pm in @INC (you may need to install the MongoDB module) (@INC contains: /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.2/darwin-thread-multi-2level /Library/Perl/Updates/5.18.2 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .) at /Users/eevitomperi/Desktop/Programming/Perl/mojoliciousApp/foodAbout/app.pl line 4.
BEGIN failed--compilation aborted at /Users/eevitomperi/Desktop/Programming/Perl/mojoliciousApp/foodAbout/app.pl line 4.

我对 mongo、mojolicious 和 perl 完全陌生,所以我想我没有安装一些软件包?

MongoDB 文件(mongo,mongod ....)是否必须在 mojolicious 项目中?

不确定我遗漏了什么,所有文档都以使用“使用 MongoDB”开始;在 mojolicious 应用程序中,所以不知道该怎么做。

希望有人能指出我错过了什么。

4

1 回答 1

1

安装模块:

cpanm Mojolicious::Plugin::Mongodb

修复以下问题:

Can't write to /Library/Perl/5.18 and /usr/local/bin: Installing modules to /Users/eevitomperi/perl5
! To turn off this warning, you have to do one of the following:
!   - run me as a root or with --sudo option (to install to /Library/Perl/5.18 and /usr/local/bin)
!   - Configure local::lib your existing local::lib in this shell to set PERL_MM_OPT etc.
!   - Install local::lib by running the following commands

通过运行:

cpanm --local-lib=~/perl5 local::lib && eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)

现在我可以从 mojolicious 连接到 Mongo

于 2015-03-11T12:34:31.187 回答