mongodb not嵌套or/and 查询如何实现
阿神
阿神 2017-04-24 09:10:41
0
1
447

形如 A and not (B or C)
数据库记录结构为:
{
{'pic':[1,2,3,6]}
{'pic':[2,3,4]}
{'pic':[3,4,5,6,7,8]}
{'pic':[5,6,7,8,9]}
}
如果要查询以上的结构,比如想要查找: 3在pic里面,而7 或 9 不在pic里面的字段
mongodb中使用否的操作符一共有4个,分别为 $not $nin $nor $ne

其中 $ne 不适合数组,也就是 pic:{$ne:[7]} 无法实现类似于 pic:{$nin:[7]}的含义。
$nin 中无法嵌套or 或 and操作符,即 pic:{$nin:{$or:[{pic:7}]}} 会报错。

那么应该怎么使用查询才能实现not操作呢? 求教~

阿神
阿神

闭关修行中......

répondre à tous(1)
刘奇
db.test.find({$and: [{pic: 3}, {$nor: [{pic: 7}, {pic: 9}]}]})
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!