抖音开放平台Logo
开发者文档
“/”唤起搜索
控制台
  • 快速入门
  • 开发指南
  • 功能服务
  • 行业解决方案
  • 迁移指南
  • API 参考
  • 云函数
  • 云托管
  • 触发器接口开发文档
  • 云数据库
  • 客户端
  • 服务端
  • 如何使用云数据库SDK
  • SDK概览
  • Database
  • Collection
  • Document
  • Command
  • Aggregate Command
  • 对象存储
  • 媒资管理OpenApi
  • 配置中心SDK
  • 公共错误码
  • 最佳实践
  • 产品计费
  • 常见问题
  • 产品动态
  • Command

    数据库操作符,通过 db.command 获取。
    原始 mongo 中筛选年龄大于10的语法为:
    db.col.find({age : {$gt : 10}});
    利用云数据库封装的command 语法可简化为:
    const _ = db.command; const res = await db.collection('todos').where({ age: _.gt(10) )}).get();
    云数据库SDK 帮助开发者封装了相关 $ 操作符。避免开发者使用了错误的比较操作。

    查询 逻辑操作符

    and

    支持端: Node SDK
    查询操作符,用于表示逻辑 "与" 的关系,表示需同时满足多个查询筛选条件。

    参数

    expressions: any[]

    返回值

    command

    示例语法

      1.用在根条件查询
    const _ = db.command; const res = await db.collection('todos').where(_.and( { age: _.eq(10), }, { sex: _.eq('man'), } ) ).get();
      2.用在字段查询条件
    需传入多个查询操作符或常量,表示字段需满足或匹配给定的条件。
    如以下用前置写法的方式表示 age 字段值大于 4 且小于 7。
    const _ = db.command; const res = await db.collection('todos').where({ age: _.and(_.gt(4), _.lt(7)) }).get();
    用后置的写法表示相同的查询条件。
    const _ = db.command; const res = await db.collection('todos').where({ age: _.gt(4).and(_.lt(7)) }).get();
    Command 默认也可以直接链式调用其他 Command,默认表示多个 Command 的与操作,因此上述代码还可以精简为:
    const _ = db.command; await await db.collection('todos').where({ age: _.gt(4).lt(7) }).get();

    or

    支持端:Node SDK
    查询操作符,用于表示逻辑 "或" 的关系。或指令有两种用法,一是可以进行字段值的 “或” 操作,二是也可以进行跨字段的 “或” 操作。

    参数

    expressions: any[]

    返回值

    Command

    示例语法

      1.字段值的或操作
    筛选出年龄小于5或者大于6的todos。
    流式写法:
    const _ = db.command const res = await db.collection('todos').where({ age: _.lt(5).or(_.gt(6)) }).get();
    前置写法:
    const _ = db.command; const res = await db.collection('todos').where({ age: _.or( _.lt(5), _.gt(6)) }).get();
    前置写法也可接收一个数组:
    const _ = db.command; const res = await db.collection('todos').where({ age: _.or( [_.lt(5), _.gt(6)] ) }).get();
      2.跨字段的操作
    跨字段的 “或” 操作指条件 “或”,相当于可以传入多个 where 语句,满足其中一个即可。
    如筛选出性别为男或年龄为4的 todos:
    const _ = db.command; const res = await db.collection('await').where(_.or({ age: 4 }, { sex: 'man' })).get();

    nor

    支持端: Node SDK。
    查询操作符,用于表示逻辑 "都不" 的关系,表示需不满足指定的所有条件。如果记录中没有对应的字段,则默认满足条件。

    参数

    expressions: any[]

    返回值

    command

    示例语法

    筛选出年龄既不小于4又不大于7的 todos :
    const _ = db.command; const res = await db.collection('todos').where({ age: _.nor(_.lt(4), _.gt(7)) }).get();
    筛选出年龄不小于 10 且性别不为男的todos:
    const _ = db.command; const res = await db.collection('todos').where(_.nor([ { age: _.lt(10), }, { sex: 'man', } ])).get()
    以上会筛选出满足以下条件之一的todos:
      1. 年龄不小于 10 且 性别不为男。
      2. 年龄不小于 10 且 性别不存在。
      3.年龄字段不存在 且 性别不为男。
      4.年龄字段不存在 且 性别字段不存在。

    比较 逻辑操作符

    eq

    支持端:Node SDK。
    查询筛选条件,表示字段等于某个值。eq 指令接受一个字面量 (literal),可以是 number, boolean, string, object, array, Date。

    参数

    value: any

    返回值

    command

    示例语法

    筛选出价格等于12。
    const _ = db.command; const res = await db.collection('todos').where({ price: _.eq(12) }).get();
    筛选出字段等于某个对象的。
    例如筛选出 person 年龄为12,性别为男。
    const _ = db.command; const res = await db.collection('todos').where({ person: _.eq({ "age": 12, "sex": "man" }) }).get();

    neq

    支持端: Node SDK。
    查询筛选条件,表示字段不等于某个值。neq 指令接受一个字面量 (literal),可以是 number, boolean, string, object, array, Date。

    参数

    value: any

    返回值

    command

    使用说明

    表示字段不等于某个值,和 eq 相反。

    lt

    支持端:Node SDK。
    查询筛选操作符,表示需小于指定值。可以传入 Date 对象用于日期比较。

    参数

    value: any

    返回值

    command

    示例语法

    找出价格小于10的。
    const _ = db.command const res = await db.collection('todos').where({price: _.lt(10)}).get();

    lte

    支持端: Node SDK
    查询筛选操作符,表示需小于或等于指定值。可以传入 Date 对象用于进行日期比较。

    参数

    value: any

    示例语法

    筛选价格小于等于10的。
    const _ = db.command const res = await db.collection('todos').where({ price: _.lte(10) }).get();

    gt

    支持端:Node SDK
    查询筛选操作符,表示需大于指定值。可以传入 Date 对象用于进行日期比较。

    参数

    value: any

    返回值

    command

    示例语法

    筛选价格大于 10 的。
    const _ = db.command const res = await db.collection('todos').where({ price: _.gt(10) }).get();

    gte

    支持端:Node SDK。
    查询筛选操作符,表示需大于或等于指定值。可以传入 Date 对象用于进行日期比较。

    参数

    value: any

    返回值

    command

    示例语法

    找出价格大于等于10的
    const _ = db.command const res = await db.collection('todos').where({ price: _.gte(10) }).get();

    in

    支持端: Node SDK
    查询筛选操作符,表示要求值在给定的数组内。

    参数

    value: any[]

    返回值

    command

    示例语法

    找出价格为0或者10的。
    const _ = db.command const res = await db.collection('todos').where({ price: _.in([0, 10]) }).get();

    nin

    支持端:Node SDK。
    查询筛选操作符,表示要求值不在给定的数组内。

    参数

    value: any[]

    返回值

    command

    示例语法

    找出价格不是 0 或 10 的 todo
    const _ = db.command const res = await db.collection('todos').where({ price: _.nin([0, 10]) }).get();

    exists

    支持端:Node SDK
    判断字段是否存在。

    参数

    value: boolean

    返回值

    command

    示例语法

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo" }
    找出存在 name 字段的元素
    const _ = db.command; const res = await db .collection('command_todos') .where({ name: _.exists(true), })
    返回结果如下:
    { "_id": "xxx", "name": "demo" }

    错误语法

    const _ = db.command; await db.collection('dbcommand_error_todos').where(_.exists(1)).get();

    错误信息

    { "errCode": 156401, "errMsg": "exists 参数必须传递一个布尔值" }

    mod

    支持端:Node SDK
    查询筛选操作符,给定除数 divisor 和余数 remainder,要求字段作为被除数时 value % divisor = remainder。

    参数

    divisor: number
    remainder: number

    返回值

    Command

    示例语法

    假设集合示例数据如下:
    { "_id": "xxx", "age": 1, "name": "demo" } { "_id": "xxx", "age": 10, "name": "demo" } { "_id": "xxx", "age": 15, "name": "demo" }
    找出年龄为 10 的倍数的字段的元素
    const _ = db.command; const res = await db .collection('command_todos') .where({ age: _.mod(10, 0), })
    返回结果如下:
    { "_id": "xxx", "name": "demo", "age": 10 }

    错误说明

    错误语法1:
    const _ = db.command; await db .collection('command_todos') .where({ age: _.mod(10, '1'), }) .get();
    错误信息
    { "errCode": 156401, "errMsg": "mod必须传递数字" }
    错误语法2:
    const _ = db.command; await db .collection('command_todos') .where({ age: _.mod(10), }) .get();
    错误信息
    { "errCode": 156401, "errMsg": "mod 必须传除数和余数" }

    all

    支持端:Node SDK
    数组查询操作符。用于数组字段的查询筛选条件,要求数组字段中包含给定数组的所有元素。

    参数

    values: any[]

    返回值

    Command

    示例语法1:普通数组

    假设集合示例数据如下:
    {"_id": "xxx", "cities": [10, 15, 20], "name": "demo"} {"_id": "xxx", "cities": [40, 50, 60], "name": "demo1"}
    找出 cities 数组字段同时包含 10 和 15 的元素
    const _ = db.command; const res = await db .collection('elem_match_command_todos') .where({ cities: _.all([10, 15]), }) .get();
    返回结果如下:
    {"_id":"xxx","name":"demo","cities":[10,15,20]}

    示例语法 2:对象数组

    如果数组元素是对象,则可以用 _.elemMatch 匹配对象的部分字段
    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "cities": [{ "age": 10, "area": 10 }] } { "_id": "xxx", "name": "demo1", "cities": [{ "age": 20, "area": 20 }] }
    找出数组字段中至少同时包含一个满足 “area 大于 0 且 age 大于 0” 的元素和一个满足 “area 小于20 且 age 小于20” 的元素
    const _ = db.command; const res = await db .collection('elem_match_command_todos') .where({ cities: _.all([ _.elemMatch({ age: _.gt(0), area: _.gt(0), }), _.elemMatch({ age: _.lt(20), area: _.lt(20), }), ]), }) .get();
    返回结果如下:
    { "_id": "xxx", "name": "demo", "cities": [{ "age": 10, "area": 10 }] }

    错误语法

    const _ = db.command; await db.collection('dbcommand_error_todos').where({ name: _.all(1 as any) }).get();

    错误信息

    { "errCode": 156401, "errMsg": "all 参数必须传递一个数组" }

    elemMatch

    支持端:Node SDK
    用于数组字段的查询筛选条件,要求数组中包含至少一个满足 elemMatch 给定的所有条件的元素。

    参数

    condition: Object|Command
    匹配条件

    返回值

    Command

    示例语法1:数组是对象数组的情况

    假设集合示例数据如下:
    { "_id":"xxx", "cities":[ {"age":10, "area":10} ], "name":"demo" } { "_id":"xxx", "cities":[ {"age":20, "area":20} ], "name":"demo1" }
    找出 cities 数组字段中至少同时包含 一个满足" age 和 area 大于0 且小于20 " 的元素
    const _ = db.command; const res = await db .collection('elem_match_command_todos') .where({ cities: _.elemMatch({ age: _.gt(0).lt(20), area: _.gt(0).lt(20), }), }) .get();
    返回结果如下:
    { "_id":"xxx", "cities":[ {"age":10, "area":10} ], "name":"demo" }

    示例语法2: 数组元素都是普通数据类型的情况

    假设集合示例数据如下:
    { "_id":"xxx", "name":"demo", "cities":[10, 20] } { "_id":"xxx", "name":"demo1", "cities":[20, 30] }
    找出 cities 数组字段中至少同时包含 一个满足" 大于0且小于15" 的元素
    const _ = db.command; const res = await db .collection('elem_match_command_todos') .where({ cities: _.elemMatch(_.gt(0).lt(15)), }) .get();
    返回结果如下:
    { "_id":"xxx", "name":"demo", "cities":[10, 20] }

    错误语法

    const _ = db.command; await db .collection('dbcommand_error_todos') .where({ name: _.elemMatch(1) }) .get();

    错误信息

    { "errCode": 156401, "errMsg": "elemMatch 参数必须传递一个对象" }

    size

    支持端:Node SDK
    更新操作符,用于数组字段的查询筛选条件,要求数组长度为给定值。

    参数

    value: string

    返回值

    Command

    示例语法

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "scores": [60, 80, 90] }
    找出 scores 数组字段长度为 3 的所有记录
    const _ = db.command; const res = await db .collection('command_todos') .where({ scores: _.size(3), }) .get();
    返回结果如下:
    { "_id": "xxx", "name": "demo", "scores": [60, 80, 90] }

    错误语法

    const _ = db.command; await db .collection('dbcommand_error_todos') .where({ name: _.size('1' as any) }) .get();

    错误信息

    { "errCode": 156401, "errMsg": "size 参数必须传递一个整型" }

    set

    支持端:Node SDK
    更新操作符,用于设定字段等于指定值。

    参数

    value: any

    返回值

    Command

    使用说明

    这种方法相比传入纯 JS 对象的好处是能够指定字段等于一个对象

    示例语法

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "style": { "color": "green", "size": "large" } }
    更新 name 为 demo 的元素
    以下方法只会更新 style.color 为 red,而不是将 style 更新为 { color: 'red' },即不影响 style 中的其他字段:
    await db .collection('command_todos') .where({ name: 'demo' }) .update({ style: { color: 'red', }, });
    更新后查询结果如下:
    { "_id": "xxx", "name": "demo", "style": { "color": "red", "size": "large" } }
    以下方法更新 style 为 { color: 'blue'}
    await db .collection('command_todos') .where({ name: 'demo' }) .update({ style: db.command.set({ color: 'blue', }), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "style": { "color": "blue" } }

    remove

    支持端:Node SDK
    更新操作符,用于表示删除某个字段。

    返回值

    Command

    示例语法

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "style": { "color": "green", "size": "large" } }
    删除 style 字段:
    await db.collection('command_todos').where({ name: 'demo' }).update({ style: db.command.remove(), });
    移除后查询结果如下:
    { "_id": "xxx", "name": "demo" }

    inc

    支持端:Node SDK
    更新操作符,原子操作,用于指示字段自增。

    参数

    value: number
    自增量,可正可负

    返回值

    Command

    原子自增

    多个用户同时写,对数据库来说都是将字段自增,不会有后来者覆写前者的情况。

    示例语法

    假设集合示例数据如下:
    { "_id": "xxx", "age": 10, "name": "demo" }
    将 age 的年龄自增 10
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.inc(10), });
    更改后的查询结果如下:
    { "_id": "xxx", "age": 20, "name": "demo" }

    错误语法

    const _ = db.command; await db .collection('dbcommand_error_todos') .where({ name: '1' }) .update({ age: _.inc('1' as any), });

    错误信息

    { "errCode": 156401, "errMsg": "inc 参数必须传递一个整型" }

    mul

    支持端:Node SDK
    更新操作符,原子操作,用于指示字段自乘某个值

    参数

    value: number
    自乘量,可正可负

    返回值

    Command

    原子自乘

    多个用户同时写,对数据库来说都是将字段自乘,不会有后来者覆写前者的情况

    示例语法

    假设集合示例数据如下:
    { "_id": "xxx", "age": 10, "name": "demo" }
    将 age 的年龄自乘 10:
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.mul(10), });
    更改后的查询结果如下:
    { "_id": "xxx", "age": 100, "name": "demo" }

    错误语法

    const _ = db.command; await db .collection('dbcommand_error_todos') .where({ name: '1' }) .update({ age: _.mul('1' as any), });

    错误信息

    { "errCode": 156401, "errMsg": "mul 参数必须传递一个整型" }

    min

    支持端:Node SDK
    更新操作符,给定一个值,只有该值大于字段当前值才进行更新。

    参数

    value: any

    返回值

    Command

    示例语法

    假设集合示例数据如下:
    { "_id": "xxx", "age": 100, "name": "demo" }
    如果字段 age > 50,则更新到 50
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.min(50), });
    更改后的查询结果如下:
    { "_id": "xxx", "age": 50, "name": "demo" }

    max

    支持端:Node SDK
    更新操作符,给定一个值,只有该值小于字段当前值才进行更新。

    参数

    value: any

    返回值

    Command

    示例语法

    假设集合示例数据如下:
    { "_id": "xxx", "age": 10, "name": "demo" }
    如果字段 age < 50,则更新到 50
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.max(50), });
    更改后的查询结果如下:
    { "_id": "xxx", "age": 50, "name": "demo" }

    rename

    支持端:Node SDK
    更新操作符,字段重命名。如果需要对嵌套深层的字段做重命名,需要用点路径表示法。不能对嵌套在数组里的对象的字段进行重命名。

    参数

    value: string

    返回值

    Command

    示例语法1:重命名顶层字段

    假设集合示例数据如下:
    { "_id": "xxx", "age": 10, "name": "demo" }
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.rename('temp_age'), });
    更改后的查询结果如下:
    { "_id":"xxx", "name":"demo", "temp_age":10 }

    示例语法2:重命名嵌套字段

    假设集合示例数据如下:
    { "_id": "xxx", "person": { "age": 10}, "name": "demo" }
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ person: { age: _.rename('person.temp_age'), }, });
    更改后的查询结果如下:
    { "_id": "xxx", "person": { "temp_age": 10 }, "name": "demo" }

    错误语法

    const _ = db.command; await db .collection('dbcommand_error_todos') .where({ name: '1' }) .update({ age: _.rename(1), });

    错误信息

    { "errCode": 156401, "errMsg": "rename 参数必须传递一个字符串" }

    push

    支持端:Node SDK
    数组更新操作符。对一个值为数组的字段,往数组添加一个或多个值。或字段原为空,则创建该字段并设数组为传入值。

    参数

    value:Object
    属性
    类型
    默认值
    必填
    说明
    each
    Array.<any>
    ​
    是
    要插入的所有元素
    position
    number
    ​
    否
    从哪个位置开始插入,不填则是尾部
    sort
    number
    ​
    否
    对结果数组排序
    slice
    number
    ​
    否
    限制结果数组长度

    返回值

    Command

    参数说明

    position 说明
    要求必须同时有 each 参数存在。
    非负数代表从数组开始位置数的位置,从 0 开始计。如果数值大于等于数组长度,则视为在尾部添加。负数代表从数组尾部倒数的位置,比如 -1 就代表倒数第二个元素的位置。如果负数数值的绝对值大于等于数组长度,则视为从数组头部添加。
    sort 说明
    要求必须同时有 each 参数存在。给定 1 代表升序,-1 代表降序。
    如果数组元素是记录,则用 { <字段>: 1 | -1 } 的格式表示根据记录中的什么字段做升降序排序。
    slice说明
    要求必须同时有 each 参数存在
    值
    说明
    0
    将字段更新为空数组
    正数
    数组只保留前 n 个元素
    负数
    数组只保留后 n 个元素

    示例语法1:尾部添加元素

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "age": [10, 20]}
    尾部添加元素
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.push([30, 40]), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "age": [10, 20, 30, 40] }

    示例语法2:从第二个位置开始插入

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "age": [10, 20, 30]}
    从第二个位置开始插入
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.push({ each: [5, 6], position: 1, }), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "age":[10, 5, 6, 20, 30] }

    示例语法3:排序

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "age": [10, 20, 30]}
    从第二个位置开始插入后对整个数组做排序
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.push({ each: [5, 6], position: 1, sort: 1, }), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "age": [5, 6, 10, 20, 30] }
    不插入,只对数组做排序
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.push({ each: [], sort: 1, }), });
    更改后的查询结果如下:
    { "_id":"xxx", "name":"demo", "age":[10, 20, 30] }
    如果字段是对象数组,可以如下根据元素对象里的字段进行排序:
    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "age": [ { "age": 18, "sex": "male"}, { "age": 19, "sex": "male"} ] }
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.push({ each: [ { age: 5, sex: 'female' }, { age: 20, sex: 'female' }, ], sort: { age: 1, }, }), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "age": [ { "age": 5, "sex": "female" }, { "age": 18, "sex": "male" }, { "age": 19, "sex": "male" }, { "age": 20, "sex": "female" } ] }

    示例语法4:截断保留

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "age": [10, 20, 30]}
    插入后只保留后 2 个元素
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.push({ each: [ 5, 11 ], slice: -2, }), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "age": [ 5, 11 ] }

    示例语法5:在指定位置插入、然后排序、最后只保留前 2 个元素

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "age": [10, 20, 30]}
    插入后只保留后 2 个元素
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.push({ each: [5, 11], position: 1, slice: 2, sort: 1, }), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "age": [ 5, 10 ] }

    pop

    支持端:Node SDK
    数组更新操作符,对一个值为数组的字段,将数组尾部元素删除

    返回值

    Command

    示例语法

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "age": [10, 20, 30]}
    找出存在 name 字段的元素
    const _ = db.command; await db.collection('command_todos').where({ name: 'demo' }).update({ age: _.pop(), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "age": [10, 20]}

    unshift

    支持端:Node SDK
    数组更新操作符,对一个值为数组的字段,往数组头部添加一个或多个值。或字段原为空,则创建该字段并设数组为传入值。

    参数

    values: any[]

    返回值

    Command

    示例语法

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "age": [10, 20, 30]}
    找出存在 name 字段的元素
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.unshift([5, 6]), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "age": [ 5, 6, 10, 20, 30 ] }

    shift

    支持端:Node SDK
    数组更新操作符,对一个值为数组的字段,将数组头部元素删除。

    返回值

    Command

    示例语法

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "age": [10, 20, 30]}
    找出存在 name 字段的元素
    const _ = db.command; await db.collection('command_todos').where({ name: 'demo' }).update({ age: _.shift(), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "age": [20, 30] }

    pull

    支持端:Node SDK
    数组更新操作符。给定一个值或一个查询条件,将数组中所有匹配给定值或查询条件的元素都移除掉。

    参数

    value: any
    值或查询条件

    返回值

    Command

    示例语法 1:根据常量匹配移除

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "cities": [10, 20] } { "_id": "xxx", "name": "demo1", "cities": [20, 30] }
    根据常量匹配移除
    const _ = db.command; await db .collection('elem_match_command_todos') .where({ name: 'demo', }) .update({ cities: _.pull(10), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "cities": [ 20 ] }

    示例语法 2:根据查询条件匹配移除

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "cities": [10, 20] } { "_id": "xxx", "name": "demo1", "cities": [20, 30] }
    根据查询条件匹配移除
    const _ = db.command; await db .collection('elem_match_command_todos') .where({ name: 'demo', }) .update({ cities: _.pull(_.in([10, 20])), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "cities": [] }

    示例语法 3:对象数组时,根据查询条件匹配移除

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "cities": [ { "age": 10, "area": 10 }] } { "_id": "xxx", "name": "demo1", "cities": [ { "age": 20, "area": 20 } ] }
    根据查询条件匹配移除
    const _ = db.command; await db .collection('elem_match_command_todos') .where({ name: 'demo', }) .update({ cities: _.pull({ age: _.gt(0).lt(20), area: _.gt(0) }), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "cities": [] }

    示例语法 4:有嵌套对象的对象数组时,根据查询条件匹配移除

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "cities": [ { "places": [ { "age": 10, "area": 10 }] } ] } { "_id": "xxx", "name": "demo1", "cities": [ { "places": [{ "age": 20, "area": 20 } ]} ] }
    可用 elemMatch 匹配嵌套在对象数组里面的对象数组字段 places
    const _ = db.command; const res = await db .collection('elem_match_command_todos') .where({ name: 'demo', }) .update({ cities: _.pull({ places: _.elemMatch({ age: _.gt(0).lt(20), area: _.gt(0) }), }), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "cities": [] }

    pullAll

    支持端:Node SDK
    数组更新操作符。给定一个值或一个查询条件,将数组中所有匹配给定值的元素都移除掉。跟 pull 的差别在于只能指定常量值、传入的是数组。

    参数

    value: any
    值或查询条件

    返回值

    Command

    示例语法:根据常量匹配移除

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo", "age": [10, 20, 30] }
    从 age 中移除所有 10 和 30 数字
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.pullAll([10, 30]), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "age": [ 20 ] }

    错误语法

    const _ = db.command; await db .collection('dbcommand_error_todos') .where({ name: '1' }) .update({ age: _.pullAll('1' as any), });

    错误信息

    { "errCode": 156401, "errMsg": "pullAll 参数必须传递数组" }

    addToSet

    支持端:Node SDK
    数组更新操作符。原子操作。给定一个或多个元素,除非数组中已存在该元素,否则添加进数组。

    参数

    value: any|Object
    要添加进数组的一个或多个元素
    属性
    类型
    默认值
    必填
    说明
    each
    Array.<any>
    ​
    是
    数组,用于同时指定多个要插入数组的元素

    返回值

    Command

    示例语法1:添加一个元素

    假设集合示例数据如下:
    { "_id":"xxx", "name": "demo", "age": [10, 20, 30, 40, 50], }
    如果 age 数组中不包含 100,添加进去
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.addToSet(100), });
    更改后的查询结果如下:
    { "_id": "xxx", "name": "demo", "age": [ 10, 20, 30, 40, 50, 100 ] }

    示例语法2:添加多个元素

    假设集合示例数据如下:
    { "_id":"xxx", "name": "demo", "age": [10, 20, 30, 40, 50], }
    需传入一个对象,其中有一个字段 each,其值为数组,每个元素就是要添加的元素
    const _ = db.command; await db .collection('command_todos') .where({ name: 'demo' }) .update({ age: _.addToSet({ $each: [100, 200, 300], }), });
    更改后的查询结果如下:
    { "_id":"xxx", "name":"demo", "age":[10, 20, 30, 40, 50, 100, 200, 300] }

    expr

    支持端:Node SDK
    查询操作符,用于在查询语句中使用聚合表达式,方法接收一个参数,该参数必须为聚合表达式。

    参数

    aggregateExpression: Expression
    要添加进数组的一个或多个元素

    返回值

    Command

    使用说明

      1.expr 可用于在聚合 match 流水线阶段中引入聚合表达式
      2.expr 可用在普通查询语句(where)中引入聚合表达式

    示例语法:

    假设集合示例数据如下:
    { "_id": "xxx", "name": "demo2", "inStock": 5, "ordered": 10 } { "_id": "xxx", "name": "demo2", "inStock": 5, "ordered": 10 }
    找出订单量大于库存量的记录:
    const _ = db.command; const res = await db .collection('command_todos') .where(_.expr(_.gt(['$ordered', '$inStock']))) .get();
    返回的查询结果如下:
    { "_id": "xxx", "name": "demo2", "inStock": 5, "ordered": 10 }