w3cschool通过本文主要向大家介绍了微信小程序云开发服务端数据库API 查询指令等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
微信小程序云开发服务端数据库API 查询指令
由 Carrie 创建, 最后一次修改 2018-11-15 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('todo').where({ progress: _.gt(50).and(_.lt(100)) }).get() } catch(e) { console.error(e) } }前置写法:
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('todo').where({
memory: _.and(_.gt(50), _.lt(100))
}).get()
} catch(e) {
console.error(e)
}
}
db.command.or
查询指令,用于表示逻辑 "或" 的关系,表示需同时满足多个查询筛选条件。或指令有两种用法,一是可以进行字段值的 “或” 操作,二是也可以进行跨字段的 “或” 操作。
字段值的 “或” 操作指的是指定一个字段值为多个值之一即可:
字段值的或操作:示例代码
如筛选出进度大于 80 或小于 20 的 todo:
流式写法:
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('todo').where({
progress: _.gt(80).or(_.lt(20))
}).get()
} catch(e) {
console.error(e)
}
}
前置写法:
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('todo').where({
memory: _.or(_.gt(80), _.lt(20))
}).get()
} catch(e) {
console.error(e)
}
}
跨字段的 “或” 操作指条件 “或”,相当于可以传入多个 where 语句,满足其中一个即可,示例:
跨字段的或操作:示例代码
如筛选出进度大于 80 或已标为已完成的 todo:
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('todo').where(_.or([
{
progress: _.gt(80)
},
{
done: true
}
]))
} catch(e) {
console.error(e)
}
}