Skip to main content

Data operators

Data objects in a datastore can be manipulated in any update operation. To manipulate one or many data object(s) a range of special operators can be applied using the updateOne(...) or updateMany(...) API.

Operators quick overview

$inc operatorโ€‹

Increase or decrease a field value on an existing datastore object.

Example:

// before
{..., "followers": 42}

const operator = {$inc: {"followers": 1}}
// Call update API, e.g. updateMany or updateOne
await conn.updateMany('mycollection', operator, {filter: {"status": "ACTIVE"}})

// after
{..., "followers": 43}

๐Ÿ‘‰ Use negative values to decrement field.

$push operatorโ€‹

Append an element to a sub array on an existing datastore object.

Example:

// before
{"followers": 42, "comments": ["one", "three"]}

const operator = {$push: {"comments": "two"}}
// E.g.: await conn.updateOne('customer', ID, operator);

// after
{"followers": 42, "comments": ["one", "three", "two"]}

$pull operatorโ€‹

Remove an element from a sub array on an existing datastore object.

Pulls all matches from array.

Example:

// before
{"followers": 42, "comments": ["one", "two", "three"]}

const operator = {$pull: {"comments": "two"}}
// E.g.: await conn.updateOne('customer', ID, operator);

// after
{"followers": 42, "comments": ["one", "three"]}

$pop operatorโ€‹

Removes last element of a sub array on an existing datastore object.

Example:

// before
{"followers": 42, "comments": ["one", "three", "two"]}

const operator = {$pop: "comments"}
// E.g.: await conn.updateOne('customer', ID, operator);

// after
{"followers": 42, "comments": ["one", "three"]}

$addToSet operatorโ€‹

Insert an unique element to a sub array on an existing datastore object. Nothing is inserted if item already exists in set.

Example:

// before
{"followers": 42, "comments": ["one", "two", "three"]}

const operator = {$addToSet: {"items": {"name": "one", "price": 1}}}
// E.g.: await conn.updateOne('customer', ID, operator);

// after
{
"followers": 42, "comments": ["one", "two", "three"],
"items": [{"name": "one", "price": 1}]
}

$set operatorโ€‹

Set any object value.

Example:

// before
{"followers": 42, "comments": ["one", "two", "three"]}

const operator = {$set: {"address": {"street": "Main street", "ZIP": 1123}}}
// E.g.: await conn.updateOne('customer', ID, operator);

// after
{
"followers": 42, "comments": ["one", "two", "three"],
"address": {"street": "Main street", "ZIP": 1123}
}

$unset operatorโ€‹

Delete an object property.

// before
{"followers": 42, "comments": ["one", "two", "three"]}

const operator = {$unset: "comments"}
// E.g.: await conn.updateOne('customer', ID, operator);

// after
{
"followers": 42
}

$rename operatorโ€‹

Rename an object property.

// before
{"followers": 42, "comments": ["one", "two", "three"]}

const operator = {$rename: ["comments", "claps"]}
// E.g.: await conn.updateOne('customer', ID, operator);

// after
{
"followers": 42, "claps": ["one", "two", "three"]
}