查询指令
eq
eq 指令接受一个字面量 (literal),可以是 number, boolean, string, object, array。
比如筛选出所有自己发表的文章,除了用传对象的方式:
const myOpenID = "xxx"db.collection('articles').where({ _openid: myOpenID})
还可以用指令:
const dbCmd = db.commandconst myOpenID = "xxx"db.collection('articles').where({ _openid: dbCmd.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 dbCmd = db.commanddb.collection('articles').where({ stat: dbCmd.eq({ publishYear: 2018, language: 'zh-CN' })})
neq
neq 指令接受一个字面量 (literal),可以是 number, boolean, string, object, array。
如筛选出品牌不为 X 的计算机:
const dbCmd = db.commanddb.collection('goods').where({ category: 'computer', type: { brand: dbCmd.neq('X') },})
gt
字段大于指定值。 如筛选出价格大于 2000 的计算机:
const dbCmd = db.commanddb.collection('goods').where({ category: 'computer', price: dbCmd.gt(2000)})
gte
字段大于或等于指定值。
lt
字段小于指定值。
lte
字段小于或等于指定值。
in
字段值在给定的数组中。 筛选出内存为 8g 或 16g 的计算机商品:
const dbCmd = db.commanddb.collection('goods').where({ category: 'computer', type: { memory: dbCmd.in([8, 16]) }})
nin
字段值不在给定的数组中。 筛选出内存不是 8g 或 16g 的计算机商品:
const dbCmd = db.commanddb.collection('goods').where({ category: 'computer', type: { memory: dbCmd.nin([8, 16]) }})
and
表示需同时满足指定的两个或以上的条件。 如筛选出内存大于 4g 小于 32g 的计算机商品: 流式写法:
const dbCmd = db.commanddb.collection('goods').where({ category: 'computer', type: { memory: dbCmd.gt(4).and(dbCmd.lt(32)) }})
前置写法:
const dbCmd = db.commanddb.collection('goods').where({ category: 'computer', type: { memory: dbCmd.and(dbCmd.gt(4), dbCmd.lt(32)) }})
or
表示需满足所有指定条件中的至少一个。如筛选出价格小于 4000 或在 6000-8000 之间的计算机: 流式写法:
const dbCmd = db.commanddb.collection('goods').where({ category: 'computer', type: { price:dbCmd.lt(4000).or(dbCmd.gt(6000).and(dbCmd.lt(8000))) }})
前置写法:
const dbCmd = db.commanddb.collection('goods').where({ category: 'computer', type: { price: dbCmd.or(dbCmd.lt(4000), dbCmd.and(dbCmd.gt(6000), dbCmd.lt(8000))) }})
如果要跨字段 “或” 操作:(如筛选出内存 8g 或 cpu 3.2 ghz 的计算机)
const dbCmd = db.commanddb.collection('goods').where(dbCmd.or( { type: { memory: dbCmd.gt(8) } }, { type: { cpu: 3.2 } }))
