Using Native Operators in Data Queries

You can resort to using native MongoDB operators in a RESTful call when the Backend Services JavaScript SDK filtering methods do not allow enough flexibility. Not all MongoDB operators are supported. When filtering for update purposes, use the rawUpdate() to pass MongoDB operators.

Important

Native MongoDB operators are not supported when querying for data coming from Data Connectors, offline storage, or Kendo UI DataSource.

In this article:

Filtering Operators

Use the filtering MongoDB operators to supplement the operations described in Filtering.

All examples below are shown as JavaScript objects that you can send as a filter parameter to the querying methods of the JavaScript SDK.

In order to use them with other tools or frameworks, send the operators as the value of a X-Everlive-Filter header.

In this section:

$type

This operator selects all content type items that contain a specified field of a specified BSON type. Use the number representation of the type to specify it in the filter. This can be useful when you want to find out what values have been stored with a particular type.

Specification

var filter = {
    "$type": {
        "MyFieldName": 3
    }
};

$exists

This operator selects items that contain (true) or do not contain (false) the specified field.

Specification

var filter = {
    "MyFieldName": {
        "$exists": true
    }
};

$elemMatch

Important

This section discusses the query selector $elemMatch operator pertaining to arrays, not the projection operator with the same name.

This operator selects documents that contain an array field with at least one element that matches all the specified query criteria.

Specification

In the following filter, Animals is the array field that we are testing. The code will return all items that contain an element Class with the value of Mammalia or Aves in this field.

var filter = { "Animals": {
        "$elemMatch": {
            "Class": {  
                "$in": ["Mammalia", "Aves"]
            }
        }
    }
};

Update Operators

These operators are used to update item fields.

Each example below is shown as a JavaScript object that can be send as a parameter to the rawUpdate() Backend Services JavaScript SDK method together with a filter expression. To use the examples with other tools or frameworks, send the operators in the request body.

Caution

Using the operators in this section without a filter updates all items in the content type.

In this section:

Field Modifiers

$set

This operator replaces a field value with the specified value.

Specification

var attrs = {
    "$set": {
        "MyArrayField" : "NewValue"
    }
};

$inc

This operator increments or decrements the value of a Number-type field with the specified amount. Specify a positive number to request an increase or a negative for a decrease.

Specification

var attrs = {
    "$inc": {
        "Quantity": -3
    }
}

Array Modifiers

The following operators update array fields.

`$`

Acts as a placeholder to update the first element that matches the query condition in an update.

Specification

var query = new Everlive.Query();
query.where().eq("MyArrayField", "OldValue");

var attrs = {
    "$set": {
        "MyArrayField.$": "NewValue"
    }
};

data.rawUpdate(attr, query);

$push

The operation appends one or more items to an array. It creates the specified array field if it doesn't exist.

Specification

var attrs = {
    "$push": {
        "MyArrayFieldA": "NewValue",
        "MyArrayFieldB": "NewValue"
    }
};

$addToSet

Adds elements to an array only if they do not already exist in the set.

Specification

$pop

Removes the first or last item of an array.

Specification

var attrs = {
    "$pop": {
        "MyArrayFieldA": -1, // removes first element
        "MyArrayFieldB": 1 // removes last element
    }
};

$pull

Removes all array elements that match the specified value.

Specification

var attrs = {
    "$pull": {
        "MyArrayField": "tape"
    }
}
Before: { "MyArrayField" : [ "tape", "lp", "cd", "tape", "tape", "cd", "dvd", "lp" ] }
After: { "MyArrayField" : [ "cd", "cd", "dvd" ] }

$pullAll

Removes all matching values from an array.

Specification

var attrs = {
    "$pullAll": {
        "MyArrayField": ["tape", "lp"]
    }
};
Before: { "MyArrayField" : [ "tape", "lp", "cd", "tape", "tape", "cd", "dvd", "lp" ] }
After: { "MyArrayField" : [ "cd", "cd", "dvd" ] }

$each

Adds each of the specified elements to the specified array field. Always use this operator in conjunction with $addToSet or $push.

Specification

var attrs = {
   "$addToSet":{
      "MyArrayField":{
         "$each":[
            "Value1",
            "Value2"
         ]
      }
   }
}

$slice

Specifies the ultimate number of elements in an array during a $push. In other words, the $push will append the array with as many as elements as you specify, but will then only keep N elements, where N is specified using $slice. A negative value only keeps the last elements. A positive value only keeps the first elements. Always use this operator in conjunction with $each.

Specification

The following example appends three new values to MyArrayField and then trims the beginning so that only the last four elements of the resulting array remain.

var attrs = {
    "$push": {
        "MyArrayField": {
            "$each": [
                "Value1",
                "Value2",
                "Value3"
            ],
            "$slice": -4
        }
    }
};

$sort

Sorts the elements in an array field during a $push. Use 1 for ascending order and -1 to specify descending. Always use this operator in conjunction with $each and $slice.

Specification

The following example adds a couple of objects to an MyArrayField and then ensures that the array elements don't exceed three and that they are sorted by the Key1 field inside the added objects in descending order.

var attrs = {
    "$push": {
        "MyArrayField": {
            "$each": [{
                "Key1": "Value1"
            }, {
                "Key2": "Value2"
            }],
            "$slice": -3,
            "$sort": {
                "Key1": -1
            }
        }
    }
};

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.