1.模糊查询
单字段
var keyWord = 'abc' const db = wx.cloud.database() const _ = db.command db.collection('product').where({ title: { $regex: '.*' + keyWord, $options: 'i' } }).get()
多字段
var keyWord = 'abc' const db = wx.cloud.database() const _ = db.command db.collection('product').where(_.or([ { title: db.RegExp({ regexp: '.*' + keyWord, options: 'i', }) }, { subtitle: db.RegExp({ regexp: '.*' + keyWord, options: 'i', }) } ])).get()
2.多个条件中的一个
const _ = db.command db.collection('todos').where({ progress: _.in([0, 100]) }) .get({ success: console.log, fail: console.error })
3.基于时间范围的数据统计
统计当前月的订单
const $ = db.command.aggregate var today = new Date() var Y = today.getFullYear() var M = today.getMonth() + 1 var dateStr = Y + '-' + M var d = new Date(dateStr), a = $.dateFromString({ dateString: d.toJSON() }) db.collection('order').aggregate() .addFields({ matched: $.gte(['$time', a]), }) .match({ matched:!0 }) .group({ _id: null, num: $.sum('$num'), money: $.sum('$money'), count: $.sum(1) }) .end()
参考链接:https://developers.weixin.qq.com/community/develop/doc/000caa1ef70b98d59869e25c454400