SDK数据库 Command查询数组操作符


当前第2页 返回上一页



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

支持端:小程序 2.8.3, 云函数 1.2.1, Web

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

参数

condition: Object|Command

匹配条件

返回值

Command

示例代码:数组是对象数组的情况

假设集合示例数据如下:

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

找出 places 数组字段中至少同时包含一个满足 “area 大于 100 且 age 小于 2” 的元素

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

注意*:如果不使用 elemMatch 而直接如下指定条件,则表示的是 places 数组字段中至少有一个元素的 area 字段大于 100 且 places 数组字段中至少有一个元素的 age 字段小于 2:

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

示例代码:数组元素都是普通数据类型的情况

假设集合示例数据如下:

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

找出 scores 数组字段中至少同时包含一个满足 “大于 80 且小于 100” 的元素

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

Command.size(value: string): Command

支持端:小程序 2.8.3, 云函数 1.2.1, Web

更新操作符,用于数组字段的查询筛选条件,要求数组长度为给定值

参数

value: string

返回值

Command

示例

找出 tags 数组字段长度为 2 的所有记录

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



标签:微信小程序

返回前面的内容

相关阅读 >>

微信小程序 q&a

sdk数据库 aggregate随机选定记录

微信小程序api 地图-创建mapcontext对象

微信小程序 小程序使用updateprinter

sdk数据库 command聚合操作符变量操作符

微信小程序api card卡券

微信小程序api nfc-取消事件监听

微信小程序api 实时音视频-livepushercontext实例

微信小程序云开发服务端数据库api 在集合上新增记录

微信小程序api-设备-系统信息

更多相关阅读请进入《微信小程序》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...