PHP code example of tcb13 / sequel-mongo-php

1. Go to this page and download the library: Download tcb13/sequel-mongo-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tcb13 / sequel-mongo-php example snippets


// Start a MongoDB Client to create a connection to your Database
$mongoConnection = new \MongoDB\Client("mongodb://127.0.0.1", [
	"username" => "user",
	"password" => "pass"
]);

/** @var \MongoDB\Database $mongo */
$mongo = $mongoConnection->selectDatabase("DatabaseName");

$qb = new QueryBuilder($mongo);
$qb->collection("Users")
   ->select("_id")
   ->find();
$result = $qb->toObject();
var_dump($result);

$qb = new QueryBuilder($mongo);
$qb->collection("Users")
   ->select("_id")
   ->limit(2)
   ->findAll();
$result = $qb->toObject();
var_dump($result);

// Use the Query Builder
$qb = new QueryBuilder($mongo);
$qb->collection("Users")
   ->select("_id", "name", "email", "active")
   ->where("setting", "!=", "other")
   ->where("active", false)
   ->where(function ($qb) {
	   $qb->where("isValid", true)
	      ->orWhere("dateActivated", "!=", null);
   })
   ->limit(100)
   ->order("dateCreated", "ASC")
   ->findAll();
$result = $qb->toObject();
var_dump($result);

->where("id", 1)
->where(function ($qb) {
    $qb->where("isValid", true)
       ->orWhere("dateActivated", "!=", null);
})

$qb = new QueryBuilder($mongo);
		$qb->collection("Orders")
		   ->whereIn("itemCount", [22,4])
		   ->findAll();
		$result = $qb->toArray();

->whereStartsWith("item", "start")
->whereEndsWith("item", "end")
->whereContains("item", "-middle-")
->whereRegex("item", ".*-middle-.*")

->whereRegex("item", ".*-middle-.*")

$qb = new QueryBuilder($mongo);
$result = $qb->collection("Users")
   ->where("userid", $this->id)
   ->count();
var_dump($result);

$qb = new QueryBuilder($mongo);
$result = $qb->collection("TestCollection")
             ->insert([
	             "item" => "test-insert",
	             "xpto" => microtime(true)
             ]);
var_dump($result);

$items = [
	[
		"item" => "test-insert-multi",
		"xpto" => microtime(true)
	],
	[
		"item" => "test-insert-multi",
		"xpto" => microtime(true)
	]
];

$qb     = new QueryBuilder($mongo);
$result = $qb->collection("TestCollection")
             ->insert($items);

$qb     = new QueryBuilder($mongo);
$result = $qb->collection("TestCollection")
             ->whereStartsWith("item", "test-insert-")
             ->delete();
var_dump($result);

// Update item
$qb     = new QueryBuilder($mongo);
$result = $qb->collection($collection)
             ->where("_id", new MongoID("51ee74e944670a09028d4fc9"))
             ->update([
	             "item" => "updated-value " . microtime(true)
             ]);
var_dump($result);

$qb = new QueryBuilder($mongo);
$qb->collection("Orders")
    //->select("_id", "products#joined.sku")
    //->join(["products" => "products#joined"], "sku", "=", "item"])
    //->join("products", "sku", "=", "item")
   ->join("Products", "sku", "item")
   ->findAll();
$result = $qb->toArray();
var_dump($result);

$qb = new QueryBuilder($mongo);
$qb->collection("Orders")
   ->select("id", new Max("datecreated", "lastorder"))
   ->where("userid", "u123")
   ->find();
$result = $qb->toArray();
var_dump($result);

$qb = new QueryBuilder($mongo);
$qb->collection("Orders")
   ->select("id", new Min("datecreated", "lastorder"))
   ->where("userid", "u123")
   ->find();
$result = $qb->toArray();
var_dump($result);

$qb = new QueryBuilder($mongo);
$qb->collection("Orders")
   ->where("id", 12345)
   ->update([
    	new Increment("status")
    ]);

$qb = new QueryBuilder($mongo);
$qb->collection("Orders")
   ->select("id")
   ->where(new ArrayContains("prioritaryItems", "123"))
   ->findAll();
$result = $qb->toArray();
var_dump($result);

$qb = new QueryBuilder($mongo);
$qb->collection("Orders")
   ->select("id", new ArrayLength("prioritaryItems", "prioritaryItems_lenght"))
   ->where("prioritaryItems_lenght", ">", 0)
   ->findAll();
$result = $qb->toArray();
var_dump($result);

$qb = new QueryBuilder($mongo);
$qb->collection("Users")
   ->where("id", 123)
   ->update([
        new ArrayPush("tokens", "...")
    ]);
$result = $qb->toArray();
var_dump($result);

$qb = new QueryBuilder($mongo);
$qb->collection("Users")
   ->where("id", 123)
   ->update([
        new ArrayPull("tokens", "...")
    ]);
$result = $qb->toArray();
var_dump($result);

QueryBuilder::$pipelineDebug = true; // Enable pipeline debugging!

// Run a query
$result = (new QueryBuilder())->collection("xyz")
            ->where("active", true)
            ->findAll()
            ->toArray();

// Fetch the pipeline built by the Query Builder
var_dump(QueryBuilder::getLastPipelineLog()); // Get the pipeline built for the last query
var_dump(QueryBuilder::getPipelineLogs()); // Get all pipelines ever built by the query builder
shell
$ composer