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

SDK Database Command Update Array Operator


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Command.push(values: Object): Command

Support: Small program 2.8.3, cloud function 1.2.1, Web

The array updates the operator. F or a field with an array of values, add one or more values to the array. Or if the field was originally empty, the field is created and an array is set as an incoming value.

Parameters

values: Object

Property Type The default Required Description
each Array.<any> Is All elements to insert
position number Whether Where to start inserting, do not fill is the tail
sort number Whether Sort the result array
slice number Whether Limit the length of the result array

Returns a value

Command

Description of the parameters

Position description

The requirement must have an each parameter present at the same time.

A non-negative number represents the position of the number of positions from the beginning of the array, starting at 0. I f the value is greater than or equal to the length of the array, it is considered to be added at the tail. A negative number represents the position from the bottom of the array, for example - 1 represents the position of the penultimate element. If the absolute value of a negative number is greater than or equal to the length of the array, it is considered to be added from the head of the array.

Sort description

The requirement must have an each parameter present at the same time. Given 1 for ascending, -1 for descending.

If the array element is a record, it is sorted in the form of a field of . | .

Description of the slice

The requirement must have an each parameter present at the same time

Value Description
0 Update the field to an empty array
Positive Array only reserves the first N elements
negative number An array only reserves N elements

Upgrade Instructions

The above definitions are supported from the small program 2.8.3 / cloud function SDK 1.2.1, for the previous version, use the following function signature, the new version is compatible with the old version.

Old Edition Signature: Incoming an array, each element of the array will be appended to the tail of the array of fields

function push(values: any[]): Command

Example 1: Tail Add Element

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.push(['mini-program', 'cloud'])
  }
})

Example 2: Insert from the second position

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.push({
      each: ['mini-program', 'cloud'],
      position: 1,
    })
  }
})

Example 3: Sort

Sort the entire array after insertion

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.push({
      each: ['mini-program', 'cloud'],
      sort: 1,
    })
  }
})

Do not insert, sort only arrays

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.push({
      each: [],
      sort: 1,
    })
  }
})

If the field is an array of objects, you can sort them as follows based on the fields in the element object:

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.push({
      each: [
        { name: 'miniprogram', weight: 8 },
        { name: 'cloud', weight: 6 },
      ],
      sort: {
        weight: 1,
      },
    })
  }
})

Example 4: Truncated hold

Only the last 2 elements are retained after insertion

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.push({
      each: ['mini-program', 'cloud'],
      slice: -2,
    })
  }
})

Example 5: Insert, then sort, and finally retain only the first 2 elements at the specified location

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.push({
      each: ['mini-program', 'cloud'],
      position: 1,
      slice: 2,
      sort: 1,
    })
  }
})

Command.pop(): Command

Support: small programs, cloud functions, web

The array updates the operator to remove the tail element of the array from a field with an array value

Returns a value

Command

The sample code

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.pop()
  }
})

Command.unshift(values: any[]): Command

Support: small programs, cloud functions, web

The array updates the operator to add one or more values to the head of the array for a field with an array value. Or if the field was originally empty, the field is created and an array is set as an incoming value.

Parameters

values: any[]

Returns a value

Command

The sample code

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.unshift(['mini-program', 'cloud'])
  }
})

Command.shift(): Command

Support: small programs, cloud functions, web

The array updates the operator to remove the array header element from a field with an array value.

Returns a value

Command

The sample code

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.shift()
  }
})

Command.pull(value: any): Command

Support: Small program 2.8.3, cloud function 1.2.1, Web

The array updates the operator. Given a value or a query condition, all elements in the array that match a given value or query condition are removed.

Parameters

value: any

Value or query criteria

Returns a value

Command

Example code 1: Removed based on constant matching

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.pull('database')
  }
})

Example code 2: Remove based on query criteria matching

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.pull(_.in(['database', 'cloud']))
  }
})

Example Code 3: When an array of objects is removed according to the query criteria match

Suppose there are fields in the array of elements structured as follows

{
  "type": string
  "area": number
  "age": number
}
const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    places: _.pull({
      area: _.gt(100),
      age: _.lt(2),
    })
  }
})

Example Code 4: When there is an array of objects with nested objects, they are removed according to query criteria matching

Suppose you have a field in the array of elements structured as follows

{
  "name": string
  "places": Place[]
}

The Place structure is as follows:

{
  "type": string
  "area": number
  "age": number
}

You can use elemMatch to match the object array field places nested within the object array

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    cities: _.pull({
      places: _.elemMatch({
        area: _.gt(100),
        age: _.lt(2),
      })
    })
  }
})

Command.pullAll(value: any): Command

Support: Small program 2.8.3, cloud function 1.2.1, Web

The array updates the operator. G iven a value or a query condition, remove all elements in the array that match the given value. The difference with pull is that only constant values can be specified and arrays are passed in.

Parameters

value: any

Value or query criteria

Returns a value

Command

Example code: Removed based on constant matching

Remove all database and cloud strings from the tags

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.pullAll(['database', 'cloud'])
  }
})

Command.addToSet(value: any| Object): Command

Support: Small program 2.8.3, cloud function 1.2.1, Web

The array updates the operator. A tomic operation. Given one or more elements, add an array unless it already exists in the array.

Parameters

value: any| Object

One or more elements to add to the array

Property Type The default Required Description
each Array.<any> Is An array that specifies multiple elements to insert into an array at the same time

Returns a value

Command

Example Code 1: Add an element

If the tags array does not contain databases, add them

const _ = db.command
db.collection('todos').doc('doc-id').update({
  data: {
    tags: _.addToSet('database')
  }
})

Example code 2: Add multiple elements

An object needs to be passed in, with a field, each with an array of values, and each element is the element to be added

  const _ = db.command
  db.collection('todos').doc('doc-id').update({
    data: {
      tags: _.addToSet({
        each: ['database', 'cloud']
      })
    }
  })