[ {name:'AL',groups:['A','B']}, {name:'AL',groups:['A','C']}, {name:'BL',groups:null} ]
聚合查询,想查出 groups 不存在或者 groups 包含 B 的结果
如下语句有问题(还是会查出包含‘C’的),帮看看,谢谢
db.db_types.aggregate([ { $match: { "$or": [ { "groups": 'B' }, { "groups": { $exists: false } } ] } ])
![]() | 1 xiongbiao 2021-05-20 19:34:34 +08:00 没有问题 |
![]() | 2 Vegetable 2021-05-20 19:39:29 +08:00 唯一的问题时,null 是 exists 的 |
![]() | 3 dfourc 2021-05-21 14:08:08 +08:00 返回带 C 的,这不是很正常嘛 ```json { $match: { "groups": { $ne: null } } ``` |
![]() | 4 xkeyideal 2021-05-21 14:46:22 +08:00 仅限给出的数据结构,将 groups 从数组变成 map,就可以不需要使用 mongodb 的 aggregate,有时候也需要照顾一下系统的性能,使用普通的查询即可,db.db_types.find({"groups.B": 1}) |