Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program cloud development API constructs the positive object


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Db. Regexp

Starting with the underlying library 2.3.2 (wx-server-sdk starts at 0.0.23), the database supports regular expression queries, and developers can use JavaScript native regular objects or db. T he RegExp method constructs the positive object and then matches the string. A regular match to a field in a query condition requires that the value of the field be matched by a given regular expression, noting that regular expressions are not available within db.command, such as db.command.in.

Using regular expression matching is sufficient for string matching, but not for text matching/searching with long text/big data volumes, because there are performance issues and text search engines such as ElasticSearch should be used for such scenarios.

Db. RegExp is defined as follows:

function RegExp(initOptions: IInitOptions): DBRegExp

interface IInitOptions {
  regexp: string // 正则表达式,字符串形式
  options: string // flags,包括 i, m, s 但前端不做强限制
}

Options support the four flags i, m, s, note that JavaScript native positive objects are constructed with only two flags of i, m, so you must use db when using s this flag. T he RegExp constructor constructs a positive object. The meaning of flag can be found in the table below:

flag Description
Case is insensitive
M Cross-line matching; ^ $
s Allows . to match all characters, including line breaks

Examples of basic usage:

// 原生 JavaScript 对象
db.collection('todos').where({
  description: /miniprogram/i
})

// 数据库正则对象
db.collection('todos').where({
  description: db.RegExp({
    regexp: 'miniprogram',
    options: 'i',
  })
})

// 用 new 构造也是可以的
db.collection('todos').where({
  description: new db.RegExp({
    regexp: 'miniprogram',
    options: 'i',
  })
})