Skip to main content

db.command.aggregate.in

1. Operator Description

Function: Given a value and an array, returns true if the value is in the array, otherwise returns false.

Declaration: db.command.aggregate.in([value, array])

2. Operator Parameters

FieldTypeRequiredDescription
-<Array>ExpressionRequiredarray of aggregate expressions, where value can be any expression,

| array` can be any expression that resolves to an array. |

3. Sample Code

Suppose the collection shops contains the following records:

{ "_id": 1, "topsellers": ["bread", "ice cream", "butter"] }
{ "_id": 2, "topsellers": ["ice cream", "cheese", "yagurt"] }
{ "_id": 3, "topsellers": ["croissant", "cucumber", "coconut"] }

Flag the records where the best-selling product contains ice cream.

// 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('price')
.aggregate()
.project({
included: $.in('ice cream', '$topsellers')
})
.end()
console.log(res.data)
}

The returned result is as follows:

{ "_id": 1, "included": true }
{ "_id": 2, "included": true }
{ "_id": 3, "included": false }