# db.command.or

Query command. Describes the logical "or" relationship, indicating that at least one query filter is required to be met. The "or" command can be used in two ways: to perform an "or" operation on the value of a field, or to perform an "or" operation on the values of multiple fields.

The "or" operation on the value of a field is to specify one value of the field to one of multiple values:

Sample code of the "or" operation on the value of a field:

Filter out the todos whose progress is greater than 80 or less than 20:

Streaming writing:

const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
  try {
    return await db.collection('todo').where({
      progress: _.gt(80).or(_.lt(20))
    }).get()
  } catch(e) {
    console.error(e)
  }
}

Pre-writing:

const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
  try {
    return await db.collection('todo').where({
      memory: _.or(_.gt(80), _.lt(20))
    }).get()
  } catch(e) {
    console.error(e)
  }
}

A cross-field "or" operation is an "or" condition equivalent to a condition in which at least one of multiple imported "where" statements is met. For example:

Sample code of the cross-field "or" operation:

Filter out the todos whose progress is greater than 80 or that have been completed:

const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
  try {
    return await db.collection('todo').where(_.or([
      {
        progress: _.gt(80)
      },
      {
        done: true
      }
    ]))
  } catch(e) {
    console.error(e)
  }
}
点击咨询小助手