Skip to main content

db.command.aggregate.strcasecmp

1. Operator Description

Function: Compares two strings in a case-insensitive manner and returns the comparison result.

Declaration: db.command.aggregate.strcasecmp([expression1, expression2])

2. Operator Parameters

FieldTypeRequiredDescription
-<Array>ExpressionRequiredArray of aggregate expressions. They are valid as long as expression1 and expression2 can be resolved to strings.

Note:

The comparison result can be 1, 0, or -1.

  • 1: The string resolved by expression1 > the string resolved by expression2
  • 0: The string resolved by expression1 = the string resolved by expression2
  • -1: The string resolved by expression1 < the string resolved by expression2

3. Sample Code

Suppose the collection students contains the following records:

{ "firstName": "Yuanxin", "group": "a", "lastName": "Dong", "score": 84 }
{ "firstName": "Weijia", "group": "a", "lastName": "Wang", "score": 96 }
{ "firstName": "Chengxi", "group": "b", "lastName": "Li", "score": 80 }

Use strcasecmp to compare the values of the firstName field and the lastName field:

// Sample code in the Cloud Function environment
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx'
})

const db = app.database()
const $ = db.command.aggregate
const _ = db.command

exports.main = async (event, context) => {
const res = await db
.collection('students')
.aggregate()
.project({
_id: 0,
result: $.strcasecmp(['$firstName', '$lastName'])
})
.end()
console.log(res.data)
}

The returned result is as follows:

{ "result": 1 }
{ "result": 1 }
{ "result": -1 }