当前位置:K88软件开发文章中心编程全书微信小程序 → 文章内容

微信小程序云开发服务端数据库API 查询筛选条件

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:03:28

由 Carrie 创建, 最后一次修改 2018-11-15 db.command.eq查询筛选条件,表示字段等于某个值。eq 指令接受一个字面量 (literal),可以是 number, boolean, string, object, array。方法签名:function eq(value: any): Command比如筛选出所有自己发表的文章,除了用传对象的方式:const myOpenID = 'xxx'db.collection('articles').where({ _openid: myOpenID})还可以用指令:const _ = db.commandconst myOpenID = 'xxx'db.collection('articles').where({ _openid: _.eq(openid)})注意 eq 指令比对象的方式有更大的灵活性,可以用于表示字段等于某个对象的情况,比如:// 这种写法表示匹配 stat.publishYear == 2018 且 stat.language == 'zh-CN'db.collection('articles').where({ stat: { publishYear: 2018, language: 'zh-CN' }})// 这种写法表示 stat 对象等于 { publishYear: 2018, language: 'zh-CN' }const _ = db.commanddb.collection('articles').where({ stat: _.eq({ publishYear: 2018, language: 'zh-CN' })})示例代码const cloud = require('wx-server-sdk')cloud.init()const db = cloud.database()const _ = db.commandexports.main = async (event, context) => { try { return await db.collection('articles').where({ stat: _.eq({ publishYear: 2018, language: 'zh-CN' }) }) .get() } catch(e) { console.error(e) }}db.command.neq表示字段不等于某个值,和 db.command.eq 相反db.command.lt查询筛选条件,表示字段需小于指定值。方法签名:function lt(value: number): Command示例代码找出进度小于 50 的 todoconst cloud = require('wx-server-sdk')cloud.init()const db = cloud.database()const _ = db.commandexports.main = async (event, context) => { try { return await db.collection('todos').where({ progress: _.lt(50) }) .get() } catch(e) { console.error(e) }}db.command.lte查询筛选条件,表示字段需小于或等于指定值。方法签名:function lte(value: number): Command示例代码找出进度小于或等于 50 的 todoconst cloud = require('wx-server-sdk')cloud.init()const db = cloud.database()const _ = db.commandexports.main = async (event, context) => { try { return await db.collection('todos').where({ progress: _.lte(50) }) .get() } catch(e) { console.error(e) }}db.command.gt查询筛选条件,表示字段需大于指定值。方法签名:function gt(value: number): Command示例代码找出进度大于 50 的 todoconst cloud = require('wx-server-sdk')cloud.init()const db = cloud.database()const _ = db.commandexports.main = async (event, context) => { try { return await db.collection('todos').where({ progress: _.gt(50) }) .get() } catch(e) { console.error(e) }}db.command.gte查询筛选条件,表示字段需大于或等于指定值。方法签名:function gte(value: number): Command示例代码找出进度大于或等于 50 的 todoconst cloud = require('wx-server-sdk')cloud.init()const db = cloud.database()const _ = db.commandexports.main = async (event, context) => { try { return await db.collection('todos').where({ progress: _.gte(50) }) .get() } catch(e) { console.error(e) }}db.command.in查询筛选条件,表示字段的值需在给定的数组内。方法签名:function in(values: any[]): Command示例代码找出进度为 0 或 100 的 todoconst cloud = require('wx-server-sdk')cloud.init()const db = cloud.database()const _ = db.commandexports.main = async (event, context) => { try { return await db.collection('todos').where({ progress: _.in([0, 100]) }) .get() } catch(e) { console.error(e) }}db.command.in查询筛选条件,表示字段的值需不在给定的数组内。方法签名:function nin(values: any[]): Command示例代码找出进度不是 0 或 100 的 todoconst cloud = require('wx-server-sdk')cloud.init()const db = cloud.database()const _ = db.commandexports.main = async (event, context) => { try { return await db.collection('todos').where({ progress: _.nin([0, 100]) }) .get() } catch(e) { console.error(e) }}

微信小程序云开发服务端数据库API 查询筛选条件