Download the PHP package maer/mongo-query without Composer
On this page you can find all versions of the php package maer/mongo-query. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download maer/mongo-query
More information about maer/mongo-query
Files in maer/mongo-query
Package mongo-query
Short Description A basic query builder in PHP for mongo
License MIT
Informations about the package mongo-query
Mongo Query Builder for PHP
Why? Because writing Mongo queries in PHP feels awkward and quickly becomes hard to read.
This is still under development and the API might change until there's a 1.x-release.
Note: This is just a wrapper for the mongodb/mongodb
-library and you always have access to the underlying library if you want to do something this wrapper doesn't support.
Usage
- Install
- Simple example
- Query builder
- Insert
- Get data
- Get list of items
- Get first item
- Find
- Count
- Where
- Operators
- Or where
- Order by
- Limit
- Skip
- Select
- Update
- Replace
- Delete
- Additional
- The MongoDB instances
Install
Clone this repository or use composer (recommended) to download the library with the following command:
Simple example
This was just a simple example. Read more below to find out more...
Query builder
Insert
You can either insert one document
or you can insert many in one go
Get data
Most of the below items will return a multi dimensional array with the matching items.
Get list of items
Get first item
Return the first matched item.
Find
Get first item that matches a single condition.
This is the only "getter" that can't have any other criteria.
Count
To get the total count of matched documents, use:
All methods that returns an actual result will also reset the query (remove any criteria previously added).
When it comes to count()
, there might be situations where you don't want this (like when you build pagination). To keep all the criteria, you can pass false
to the count()
-method: count(false)
.
If you pass false
, you can do:
and it will only return the documents that matched the previous criteria.
Where
Usually, you only want to return some specific items which match some type of criteria.
The above will match all items that has some-value
as the value for the key some-key
. This equals: where('some-key', '=', 'some-value')
.
Operators
There are many more operators you can use to narrow down the result.
The below operators are used like this: where($column, $operator, $value)
Operator | Description |
---|---|
= |
Equal to |
!= |
Not equal to |
< |
Lower than |
> |
Higher than |
<= |
Lower or equal to |
>= |
Higher or equal to |
* |
Contains |
=* |
Starts with |
*= |
Ends with |
You can add as many where conditions as you like to the same query. To make it easier, you can chain them:
Or Where
To add an or $or
-block, use orWhere()
:
You can also group the or-credentials:
Order by
To sort the result in a specific way, you can use orderBy(['column' => 'asc|desc'])
.
Limit
You can limit the amount of items returned.
Skip
If you need to add an offset (for using with pagination, for example), you can define how many documents it should skip()
:
Select
You might only want to fetch some specific fields from the documents. You can define what fields to get using select()
Note: This method will always return the _id
-field as well
Pluck
If you only want to get one single field as an array with all the values, you can use pluck()
Any potential duplicates will also be removed.
Update
To update an item, use updateOne(array $data)
:
To update multiple items, use updateMany(array $data)
:
These method returns the number of modified documents.
When updating, you only need to pass the fields and values you want to update. All other values will remain as is in the database.
Replace
The difference between these methods and the update, is that these replaces the complete documents, except from the _id. Example:
Delete
Delete items
To delete multiple items, use deleteMany()
.
These methods returns the number of modified documents.
Additional
The MongoDB instances
Sometimes you want to do some magic that this library doesn't support (yet). To do that, you might need to access the underlying MongoDB instances. You can do that with the getInstance()
-methods.
If you have any questions, suggestions or issues, let me know!
Happy coding!