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

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.command

const 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.command

db.collection('articles').where({

stat: _.eq({

publishYear: 2018,

language: 'zh-CN'

})

})

示例代码const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database()

const _ = db.command

exports.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.command

exports.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.command

exports.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.command

exports.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.command

exports.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.command

exports.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.command

exports.main = async (event, context) => {

try {

return await db.collection('todos').where({

progress: _.nin([0, 100])

})

.get()

} catch(e) {

console.error(e)

}

}

db.command.nin支持端:小程序 , 云函数 , Web

查询筛选操作符,表示要求值不在给定的数组内。

参数

value: any[]

返回值

Command

示例代码

找出进度不是 0 或 100 的 todoconst _ = db.command

db.collection('todos').where({

progress: _.nin([0, 100])

})

.get({

success: console.log,

fail: console.error

})

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值