search
HomePHP FrameworkLaravelDetailed explanation of how to install jenssegers/laravel-mongodb extension in laravel

项目中使用了mongoDB,所以就到最大的同性交友网站找到了星星最多的jenssegers/laravel-mongodb

包安装

1、根据README,找到本地laravel对应的包版本
Detailed explanation of how to install jenssegers/laravel-mongodb extension in laravel

2、开发环境中使用 composer requir 引入

3、配置config/app.php

        /*
         * mongoDB
         */
        Jenssegers\Mongodb\MongodbServiceProvider::class,
4、database.php 配置
"mongodb" => [
      "driver" => "mongodb",
      "host" => "127.0.0.1",
      "port" => 27017,
      "database" => "data",
      "username" => "test",
      "password" => "test",
   ],

:如果项目不涉及到mysql,那么可以直接将上面的默认数据引擎改成mongodb,关于env方法的说明就不具体阐述了

'default' => env('DB_CONNECTION', 'mysql'),
5、代码中使用
// 获取数据
        $mongo = \DB::connection("mongodb")
            ->collection($collection)
//            ->where("****","***")
            ->first();

问题

1、unsupported driver [mongodb]

tip1检查phpinfo是否包含mongoDB拓展,如果没有mongoDB的拓展,请自行安装再试
tip2重点!一定要检查laravel的日志文件
在我检查了mongoDb拓展之后还是不支持,查看日志:

Type error: Argument 3 passed to MongoDB\Driver\Server::executeQuery() must be an instance of MongoDB\Driver\ReadPreference or null, array given {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Type error: Argument 3 passed to MongoDB\\Driver\\Server::executeQuery() must be an instance of MongoDB\\Driver\\ReadPreference or null, array given at F:\\project\\interface_center_jtl\\vendor\\mongodb\\mongodb\\src\\Operation\\Find.php:299)

追到具体的位置,通过修改此处的返回,再次打印,发现已经可以正常使用get/first方法。insert方法同理,不过是修改同目录下的InsertMany

Detailed explanation of how to install jenssegers/laravel-mongodb extension in laravel

!!从源头解决这个问题的正确方法,查看phpinfo里面,将mongo的拓展,升级到最新的stable版本!具体请查看我以前的文章centos源码安装php7以上的mongodb拓展,如果是windows,直接下载dll文件就行了

2、Authentication failed.

tip1检查密码账号,再用工具连接,看看是不是自己配置错误

tip2使用原生方法进行测试,如果原生能够连接,包方法却不能连接,那就考虑包的问题

 // %40是@符号的转义
 $manager = new \MongoDB\Driver\Manager("mongodb://mongo:user:passwd%40abcdef@127.0.0.1:27017");
 $query=new \MongoDB\Driver\Query([]);
 $cursor = $manager->executeQuery('test.test', $query);
 dd($cursor);

·如上,发现依旧不能连接,考虑mongo的问题了,再次测试,同事发现连接写法经过修改之后就能成功!如下:

 $manager = new \MongoDB\Driver\Manager("mongodb://mongo:user:passwd%40abcdef@127.0.0.1:27017/data");

·百思不得骑姐之下,对比了mongoDB的版本,发现测试环境居然是个老版本。。/手动喷血

·在和运维与测试沟通了之后,为了保持线上版本的稳定性和敏捷开发的机动性,遂决定做代码兼容。
再次打开包内源码,发现了如下的细节(敲黑板:注意看路径,此时是jenssegers包内)
Detailed explanation of how to install jenssegers/laravel-mongodb extension in laravel知道原理之后,于是在配置文件中做了如下修改:

Detailed explanation of how to install jenssegers/laravel-mongodb extension in laravel

至此,add、commint、push,收工!

相关推荐:最新的五个Laravel视频教程

The above is the detailed content of Detailed explanation of how to install jenssegers/laravel-mongodb extension in laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
Using Laravel: Streamlining Web Development with PHPUsing Laravel: Streamlining Web Development with PHPApr 19, 2025 am 12:18 AM

Laravel optimizes the web development process including: 1. Use the routing system to manage the URL structure; 2. Use the Blade template engine to simplify view development; 3. Handle time-consuming tasks through queues; 4. Use EloquentORM to simplify database operations; 5. Follow best practices to improve code quality and maintainability.

Laravel: An Introduction to the PHP Web FrameworkLaravel: An Introduction to the PHP Web FrameworkApr 19, 2025 am 12:15 AM

Laravel is a modern PHP framework that provides a powerful tool set, simplifies development processes and improves maintainability and scalability of code. 1) EloquentORM simplifies database operations; 2) Blade template engine makes front-end development intuitive; 3) Artisan command line tools improve development efficiency; 4) Performance optimization includes using EagerLoading, caching mechanism, following MVC architecture, queue processing and writing test cases.

Laravel: MVC Architecture and Best PracticesLaravel: MVC Architecture and Best PracticesApr 19, 2025 am 12:13 AM

Laravel's MVC architecture improves the structure and maintainability of the code through models, views, and controllers for separation of data logic, presentation and business processing. 1) The model processes data, 2) The view is responsible for display, 3) The controller processes user input and business logic. This architecture allows developers to focus on business logic and avoid falling into the quagmire of code.

Laravel: Key Features and Advantages ExplainedLaravel: Key Features and Advantages ExplainedApr 19, 2025 am 12:12 AM

Laravel is a PHP framework based on MVC architecture, with concise syntax, powerful command line tools, convenient data operation and flexible template engine. 1. Elegant syntax and easy-to-use API make development quick and easy to use. 2. Artisan command line tool simplifies code generation and database management. 3.EloquentORM makes data operation intuitive and simple. 4. The Blade template engine supports advanced view logic.

Building Backend with Laravel: A GuideBuilding Backend with Laravel: A GuideApr 19, 2025 am 12:02 AM

Laravel is suitable for building backend services because it provides elegant syntax, rich functionality and strong community support. 1) Laravel is based on the MVC architecture, simplifying the development process. 2) It contains EloquentORM, optimizes database operations. 3) Laravel's ecosystem provides tools such as Artisan, Blade and routing systems to improve development efficiency.

Laravel framework skills sharingLaravel framework skills sharingApr 18, 2025 pm 01:12 PM

In this era of continuous technological advancement, mastering advanced frameworks is crucial for modern programmers. This article will help you improve your development skills by sharing little-known techniques in the Laravel framework. Known for its elegant syntax and a wide range of features, this article will dig into its powerful features and provide practical tips and tricks to help you create efficient and maintainable web applications.

The difference between laravel and thinkphpThe difference between laravel and thinkphpApr 18, 2025 pm 01:09 PM

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

Laravel user login function listLaravel user login function listApr 18, 2025 pm 01:06 PM

Building user login capabilities in Laravel is a crucial task and this article will provide a comprehensive overview covering every critical step from user registration to login verification. We will dive into the power of Laravel’s built-in verification capabilities and guide you through customizing and extending the login process to suit specific needs. By following these step-by-step instructions, you can create a secure and reliable login system that provides a seamless access experience for users of your Laravel application.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment