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

SDK database Command query array operator


May 20, 2021 WeChat Mini Program Development Document


Table of contents


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

Support: Small program 2.8.3, cloud function 1.2.1, Web

Array query operator. A query filter for array fields that requires that the array field contain all elements of a given array.

Parameters

values: any[]

Returns a value

Command

Example code 1: Normal array

Find records for tags array fields that contain both cloud and database

const _ = db.command
db.collection('todos').where({
  tags: _.all(['cloud', 'database'])
})
.get({
  success: console.log,
  fail: console.error
})

Example code 2: An array of objects

If the array element is an object, you can match some of the object's fields with .elemMatch

Suppose there are fields that are defined as follows:

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

Find out that the array field contains at least one element that satisfies "area is greater than 100 and age is less than 2" and an element that satisfies "type is mall and age is greater than 5"

const _ = db.command
db.collection('todos').where({
  places: _.all([
    _.elemMatch({
      area: _.gt(100),
      age: _.lt(2),
    }),
    _.elemMatch({
      name: 'mall',
      age: _.gt(5),
    }),
  ]),
})
.get({
  success: console.log,
  fail: console.error,
})




Command.elemMatch(condition: Object| Command): Command

Support: Small program 2.8.3, cloud function 1.2.1, Web

A query filter for array fields that requires the array to contain at least one element that meets all the criteria given by elemMatch

Parameters

condition: Object| Command

Match the condition

Returns a value

Command

Example code: An array is the case of an array of objects

Suppose the sample data for the collection is as follows:

{
  "_id": "a0",
  "city": "x0",
  "places": [{
    "type": "garden",
    "area": 300,
    "age": 1
  }, {
    "type": "theatre",
    "area": 50,
    "age": 15
  }]
}

Find out that the places array field contains at least one element that satisfies that the "area is greater than 100 and age is less than 2"

const _ = db.command
db.collection('todos').where({
  places: _.elemMatch({
    area: _.gt(100),
    age: _.lt(2),
  })
})
.get()

Note: If you do not specify the conditions as follows directly as follows using elemMatch, it means that the area field of at least one element in the places array field is greater than 100 and the age field of at least one element in the places array field is less than 2:

const _ = db.command
db.collection('todos').where({
  places: {
    area: _.gt(100),
    age: _.lt(2),
  }
})
.get()

Example code: Array elements are normal data types

Suppose the sample data for the collection is as follows:

{
  "_id": "a0",
  "scores": [60, 80, 90]
}

Find out that at least one element in the scores array field that meets "greater than 80 and less than 100" is included

const _ = db.command
db.collection('todos').where({
  places: _.elemMatch(_.gt(80).lt(100))
})
.get()

Command.size(value: string): Command

Support: Small program 2.8.3, cloud function 1.2.1, Web

Update the operator, the query filter for array fields, to require the array length to be a given value

Parameters

value: string

Returns a value

Command

Example

Find all records with a tags array field length of 2

const _ = db.command
db.collection('todos').where({
  places: _.size(2)
})
.get({
  success: console.log,
  fail: console.error,
})