https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/database/query-array-object.html
如文档中:
集合下一条记录:
{
"numbers": [10, 20, 30]
}
如果查询包含20的,可以用
db.collection('todos').where({
numbers: 20
}).get()
请问如果查询包含10且包含20,或者查询包含10或者包含20的数据,应该怎么查呢?
对于嵌入数组,凡是不知道怎么查的,我都aggregate.unwind,好使。
你这个查询的集合看起来有点诡异,
既包含10和20(集合A),或者包含10(集合B),或者包含20(集合C);
集合A是B和C的交集不是吗,那你要查询的其实是不是B与C的交集(Bc)或者B或者C?
那你这个问题就是可以转变为B和C的并集(B和C的总和)
即包含10的或者包含20的
参考这个数组操作https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/Command.elemMatch.html
用where是有问题的,楼上说的用eq但没有对集合展开,要展开才行,例如:
numbers: _elemMatch(_.eq(10).or(_.eq(20)))
你试试看,有点绕可能看起来
const db = wx.cloud.database();
const _ = db.command
db.collection('todos).where({
numbers: _.eq(20).or(_.eq(10))
}).get()