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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

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

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!


All versions of mongo-query with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0
ext-mongodb Version *
mongodb/mongodb Version ^1.16
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package maer/mongo-query contains the following files

Loading the files please wait ....