Oihana PHP Arango

aqlUpsert.php

Table of Contents

Functions

aqlUpsert()  : string
Prepare the query to update an existing document, or creates a new document if it does not exist.

Functions

aqlUpsert()

Prepare the query to update an existing document, or creates a new document if it does not exist.

aqlUpsert([JsonSerializable|null} $init = [] ]) : string
UPSERT [ searchExpression | FILTER filterExpression ]
INSERT insertExpression
UPDATE updateExpression
IN collection

Options in $init :

  • collection : The name of the collection
  • filter : The alternative filterExpression, this syntax for UPSERT operations allows you to use more flexible filter conditions beyond equality matches to look up documents.
  • search : The 'searchExpression' contains the document to be looked for. It must be an object literal (UPSERT { : , ... } ...) without dynamic attribute names. In case no such document can be found in collection, a new document is inserted into the collection as specified in the insertExpression.
  • insert : The document to insert in the collection if the document not exist.
  • update : The document to update in the collection.
  • options : The optional upsert options definition array or object.
  • return : optional expression to define the RETURN clause. Default is Clause::NEW. You can also use Clause::WITH_STATUS to return both the document and the type of operation.
Parameters
$init : JsonSerializable|null} = []

Configuration options for the UPSERT query.

Tags
throws
ReflectionException
UnsupportedOperationException
example

1 - Upsert with UPDATE

$query = aqlUpsert
([
    'search' => [['foo', 'bar']],
    'insert' => [['foo', 'bar']],
    'update' => [['foo', 'baz']],
]);
// Returns: "UPSERT {foo:'bar'} INSERT {foo:'bar'} UPDATE {foo:'baz'} IN @@collection RETURN NEW"

3 - Upsert with return including status

$query3 = aqlUpsert
([
    'search'  => [['foo', 'bar']],
    'insert'  => [['foo', 'bar']],
    'update'  => [['foo', 'baz']],
    'return'  => Clause::WITH_STATUS,
]);
// Returns:
// "UPSERT {foo:'bar'} INSERT {foo:'bar'} UPDATE {foo:'baz'} IN @@collection RETURN { doc: NEW, type: OLD ? 'update' : 'insert' }"
see
https://docs.arangodb.com/stable/aql/high-level-operations/upsert
since
1.0.0
author

Marc Alcaraz

Return values
string

The generated AQL UPSERT query.

On this page

Search results