All Collections
General questions
Using $regex and $in operators in brainCloud APIs request with examples
Using $regex and $in operators in brainCloud APIs request with examples

Frequently used operators for searching data

Jason Liang avatar
Written by Jason Liang
Updated over a week ago

Some brainCloud APIs take a JSON formatted MongoDB-style where  clause as a parameter. These allow you to perform more complex queries on data to get specific results.

There are several query operators often used for querying data inside searchCriteria or where fields through some brainCloud API requests, especially for get a list of data from *page or *list APIs call (i.e. SysGetEntityPage,  GetPage, GetList etc.), such as comparison query operators ( e.g. $eq , $gt , $gte  ,$lt , $lte ) and evaluation query operators (e.g. $regex , $in ). These operators give users a possibility to get expect data quickly and easily.

This article provides some example of the basics of using $regex , $in with a couple of brainCloud APIs.

Entity->Getpage(), searching with $regex -- to find all entities with entityId includes ea :

{
  "context": {
    "pagination": {
      "rowsPerPage": 50,
      "pageNumber": 1
    },
    "searchCriteria": {
      "entityId": {"$regex": ".*ea.*"}
    },
    "sortCriteria": {
      "createdAt": 1,
      "updatedAt": -1
    }
  }
}

Get the result as expected:

User->SysGetPage(), expression $regex  with $options -- i  to turn case-insensitivity on:

{
  "context": {
    "pagination": {
      "rowsPerPage": 50,
      "pageNumber": 1
    },
    "searchCriteria": {
      "playerName": {
        "$regex": "^h", "$options": "i"
      }
    },
    "sortCriteria": {
      "playerName": 1
    },
    "collationCriteria": {
      "locale": "en",
      "strength": 1
    }
  }
}

so, you will get a result with playerName start with H or h: 

CutomEntity->SysGetEntityPage(), operator $in is used in this request example:

{
  "entityType": "athletes",
  "context": {
    "pagination": {
      "rowsPerPage": 50,
      "pageNumber": 1
    },
    "searchCriteria": {
      "data.position": {"$in":["defense","forward"]}
    },
    "sortCriteria": {
      "createdAt": 1
    }
  }
}

this request will return all the entities with position is defense or forward :

Did this answer your question?