Download the PHP package app-accounts/mongodb without Composer
On this page you can find all versions of the php package app-accounts/mongodb. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mongodb
Laravel MongoDB
This package adds functionalities to the Eloquent model and Query builder for MongoDB, using the original Laravel API. This library extends the original Laravel classes, so it uses exactly the same methods.
- Laravel MongoDB
- Installation
- Laravel version Compatibility
- Laravel
- Lumen
- Non-Laravel projects
- Testing
- Database Testing
- Configuration
- Eloquent
- Extending the base model
- Extending the Authenticable base model
- Soft Deletes
- Guarding attributes
- Dates
- Basic Usage
- MongoDB-specific operators
- MongoDB-specific Geo operations
- Inserts, updates and deletes
- MongoDB specific operations
- Relationships
- Basic Usage
- belongsToMany and pivots
- EmbedsMany Relationship
- EmbedsOne Relationship
- Query Builder
- Basic Usage
- Available operations
- Schema
- Basic Usage
- Geospatial indexes
- Extending
- Cross-Database Relationships
- Authentication
- Queues
- Laravel specific
- Lumen specific
- Upgrading
- Upgrading from version 2 to 3
- Security contact information
Installation
Make sure you have the MongoDB PHP driver installed. You can find installation instructions at http://php.net/manual/en/mongodb.installation.php
Laravel version Compatibility
Laravel | Package | Maintained |
---|---|---|
9.x | 3.9.x | :white_check_mark: |
8.x | 3.8.x | :white_check_mark: |
7.x | 3.7.x | :x: |
6.x | 3.6.x | :white_check_mark: |
5.8.x | 3.5.x | :x: |
5.7.x | 3.4.x | :x: |
5.6.x | 3.4.x | :x: |
5.5.x | 3.3.x | :x: |
5.4.x | 3.2.x | :x: |
5.3.x | 3.1.x or 3.2.x | :x: |
5.2.x | 2.3.x or 3.0.x | :x: |
5.1.x | 2.2.x or 3.0.x | :x: |
5.0.x | 2.1.x | :x: |
4.2.x | 2.0.x | :x: |
Install the package via Composer:
Laravel
In case your Laravel version does NOT autoload the packages, add the service provider to config/app.php
:
Lumen
For usage with Lumen, add the service provider in bootstrap/app.php
. In this file, you will also need to enable Eloquent. You must however ensure that your call to $app->withEloquent();
is below where you have registered the MongodbServiceProvider
:
The service provider will register a MongoDB database extension with the original database manager. There is no need to register additional facades or objects.
When using MongoDB connections, Laravel will automatically provide you with the corresponding MongoDB objects.
Non-Laravel projects
For usage outside Laravel, check out the Capsule manager and add:
Testing
To run the test for this package, run:
Database Testing
To reset the database after each test, add:
Also inside each test classes, add:
Keep in mind that these traits are not yet supported:
use Database Transactions;
use RefreshDatabase;
Configuration
You can use MongoDB either as the main database, either as a side database. To do so, add a new mongodb
connection to config/database.php
:
For multiple servers or replica set configurations, set the host to an array and specify each server host:
If you wish to use a connection string instead of full key-value params, you can set it so. Check the documentation on MongoDB's URI format: https://docs.mongodb.com/manual/reference/connection-string/
Eloquent
Extending the base model
This package includes a MongoDB enabled Eloquent class that you can use to define models for corresponding collections.
Just like a normal model, the MongoDB model class will know which collection to use based on the model name. For Book
, the collection books
will be used.
To change the collection, pass the $collection
property:
NOTE: MongoDB documents are automatically stored with a unique ID that is stored in the _id
property. If you wish to use your own ID, substitute the $primaryKey
property and set it to your own primary key attribute name.
Likewise, you may define a connection
property to override the name of the database connection that should be used when utilizing the model.
Extending the Authenticable base model
This package includes a MongoDB Authenticatable Eloquent class Jenssegers\Mongodb\Auth\User
that you can use to replace the default Authenticatable class Illuminate\Foundation\Auth\User
for your User
model.
Soft Deletes
When soft deleting a model, it is not actually removed from your database. Instead, a deleted_at timestamp is set on the record.
To enable soft deletes for a model, apply the Jenssegers\Mongodb\Eloquent\SoftDeletes
Trait to the model:
For more information check Laravel Docs about Soft Deleting.
Guarding attributes
When choosing between guarding attributes or marking some as fillable, Taylor Otwell prefers the fillable route. This is in light of recent security issues described here.
Keep in mind guarding still works, but you may experience unexpected behavior.
Dates
Eloquent allows you to work with Carbon or DateTime objects instead of MongoDate objects. Internally, these dates will be converted to MongoDate objects when saved to the database.
This allows you to execute queries like this:
Basic Usage
Retrieving all models
Retrieving a record by primary key
Where
OR Statements
AND statements
whereIn
When using whereNotIn
objects will be returned if the field is non-existent. Combine with whereNotNull('age')
to leave out those documents.
whereBetween
whereNull
whereDate
The usage is the same as whereMonth
/ whereDay
/ whereYear
/ whereTime
Advanced wheres
orderBy
Offset & Limit (skip & take)
groupBy
Selected columns that are not grouped will be aggregated with the $last
function.
Distinct
Distinct requires a field for which to return the distinct values.
Distinct can be combined with where:
Like
Aggregation
Aggregations are only available for MongoDB versions greater than 2.2.x
Aggregations can be combined with where:
Aggregations can be also used on sub-documents:
NOTE: This aggregation only works with single sub-documents (like EmbedsOne
) not subdocument arrays (like EmbedsMany
).
Incrementing/Decrementing the value of a column
Perform increments or decrements (default 1) on specified attributes:
The number of updated objects is returned:
You may also specify additional columns to update:
MongoDB-specific operators
Exists
Matches documents that have the specified field.
All
Matches arrays that contain all elements specified in the query.
Size
Selects documents if the array field is a specified size.
Regex
Selects documents where values match a specified regular expression.
NOTE: you can also use the Laravel regexp operations. These are a bit more flexible and will automatically convert your regular expression string to a MongoDB\BSON\Regex
object.
The inverse of regexp:
Type
Selects documents if a field is of the specified type. For more information check: http://docs.mongodb.org/manual/reference/operator/query/type/#op._S_type
Mod
Performs a modulo operation on the value of a field and selects documents with a specified result.
MongoDB-specific Geo operations
Near
GeoWithin
GeoIntersects
Inserts, updates and deletes
Inserting, updating and deleting records works just like the original Eloquent. Please check Laravel Docs' Eloquent section.
Here, only the MongoDB-specific operations are specified.
MongoDB specific operations
Raw Expressions
These expressions will be injected directly into the query.
You can also perform raw expressions on the internal MongoCollection object. If this is executed on the model class, it will return a collection of models.
If this is executed on the query builder, it will return the original response.
Cursor timeout
To prevent MongoCursorTimeout
exceptions, you can manually set a timeout value that will be applied to the cursor:
Upsert
Update or insert a document. Additional options for the update method are passed directly to the native update method.
Projections
You can apply projections to your queries using the project
method.
Projections with Pagination
Push
Add items to an array.
If you DON'T want duplicate items, set the third parameter to true
:
Pull
Remove an item from an array.
Unset
Remove one or more fields from a document.
Relationships
Basic Usage
The only available relationships are:
- hasOne
- hasMany
- belongsTo
- belongsToMany
The MongoDB-specific relationships are:
- embedsOne
- embedsMany
Here is a small example:
The inverse relation of hasMany
is belongsTo
:
belongsToMany and pivots
The belongsToMany relation will not use a pivot "table" but will push id's to a __related_ids__ attribute instead. This makes the second parameter for the belongsToMany method useless.
If you want to define custom keys for your relation, set it to null
:
EmbedsMany Relationship
If you want to embed models, rather than referencing them, you can use the embedsMany
relation. This relation is similar to the hasMany
relation but embeds the models inside the parent object.
REMEMBER: These relations return Eloquent collections, they don't return query builder objects!
You can access the embedded models through the dynamic property:
The inverse relation is automagically available. You don't need to define this reverse relation.
Inserting and updating embedded models works similar to the hasMany
relation:
You can update embedded models using their save
method (available since release 2.0.0):
You can remove an embedded model by using the destroy
method on the relation, or the delete
method on the model (available since release 2.0.0):
If you want to add or remove an embedded model, without touching the database, you can use the associate
and dissociate
methods.
To eventually write the changes to the database, save the parent object:
Like other relations, embedsMany assumes the local key of the relationship based on the model name. You can override the default local key by passing a second argument to the embedsMany method:
Embedded relations will return a Collection of embedded items instead of a query builder. Check out the available operations here: https://laravel.com/docs/master/collections
EmbedsOne Relationship
The embedsOne relation is similar to the embedsMany relation, but only embeds a single model.
You can access the embedded models through the dynamic property:
Inserting and updating embedded models works similar to the hasOne
relation:
You can update the embedded model using the save
method (available since release 2.0.0):
You can replace the embedded model with a new model like this:
Query Builder
Basic Usage
The database driver plugs right into the original query builder.
When using MongoDB connections, you will be able to build fluent queries to perform database operations.
For your convenience, there is a collection
alias for table
as well as some additional MongoDB specific operators/operations.
If you are familiar with Eloquent Queries, there is the same functionality.
Available operations
To see the available operations, check the Eloquent section.
Schema
The database driver also has (limited) schema builder support. You can easily manipulate collections and set indexes.
Basic Usage
You can also pass all the parameters specified in the MongoDB docs to the $options
parameter:
Inherited operations:
- create and drop
- collection
- hasCollection
- index and dropIndex (compound indexes supported as well)
- unique
MongoDB specific operations:
- background
- sparse
- expire
- geospatial
All other (unsupported) operations are implemented as dummy pass-through methods because MongoDB does not use a predefined schema.
Read more about the schema builder on Laravel Docs
Geospatial indexes
Geospatial indexes are handy for querying location-based documents.
They come in two forms: 2d
and 2dsphere
. Use the schema builder to add these to a collection.
To add a 2dsphere
index:
Extending
Cross-Database Relationships
If you're using a hybrid MongoDB and SQL setup, you can define relationships across them.
The model will automatically return a MongoDB-related or SQL-related relation based on the type of the related model.
If you want this functionality to work both ways, your SQL-models will need to use the Jenssegers\Mongodb\Eloquent\HybridRelations
trait.
This functionality only works for hasOne
, hasMany
and belongsTo
.
The MySQL model should use the HybridRelations
trait:
Within your MongoDB model, you should define the relationship:
Authentication
If you want to use Laravel's native Auth functionality, register this included service provider:
This service provider will slightly modify the internal DatabaseReminderRepository to add support for MongoDB based password reminders.
If you don't use password reminders, you don't have to register this service provider and everything else should work just fine.
Queues
If you want to use MongoDB as your database backend, change the driver in config/queue.php
:
If you want to use MongoDB to handle failed jobs, change the database in config/queue.php
:
Laravel specific
Add the service provider in config/app.php
:
Lumen specific
With Lumen, add the service provider in bootstrap/app.php
. You must however ensure that you add the following after the MongodbServiceProvider
registration.
Upgrading
Upgrading from version 2 to 3
In this new major release which supports the new MongoDB PHP extension, we also moved the location of the Model class and replaced the MySQL model class with a trait.
Please change all Jenssegers\Mongodb\Model
references to Jenssegers\Mongodb\Eloquent\Model
either at the top of your model files or your registered alias.
If you are using hybrid relations, your MySQL classes should now extend the original Eloquent model class Illuminate\Database\Eloquent\Model
instead of the removed Jenssegers\Eloquent\Model
.
Instead use the new Jenssegers\Mongodb\Eloquent\HybridRelations
trait. This should make things more clear as there is only one single model class in this package.
Embedded relations now return an Illuminate\Database\Eloquent\Collection
rather than a custom Collection class. If you were using one of the special methods that were available, convert them to Collection operations.
Security contact information
To report a security vulnerability, follow these steps.
All versions of mongodb with dependencies
illuminate/container Version ^8.0|^9.0|^10.0
illuminate/database Version ^8.0|^9.0|^10.0
illuminate/events Version ^8.0|^9.0|^10.0
mongodb/mongodb Version ^1.6