代码之家  ›  专栏  ›  技术社区  ›  Muhammad Umer

这些都是正确的使用方法还是运算符?

  •  1
  • Muhammad Umer  · 技术社区  · 8 年前

    我见过3种使用或运算符的方法。。。

    where: { $or : [ {attr:val}, {attr:val}] } 
    

    --

    where: { $or : { attr:val, attr:val} } 
    

    where: { attr: { $or: [val, val] } }
    

    这些都有效吗?有什么发现吗?

    1 回复  |  直到 8 年前
        1
  •  1
  •   num8er    8 年前

    我可能错了,不打算投更多的票。

    official documentation .

    并启用 logging mode (see logging param) 查看特定查询的结果


    where: { $or : [ {attr:val1, some: val3}, {attr:val2} ] } 
    

    ... WHERE ((attr = val1 AND some = val3) OR (attr = val2))
    


    这个我不知道哪个值是用来比较的。因为它不是数组,所以我猜它将使用上一个定义,并且因为没有其他字段来构建查询,所以它不会使用 OR

    where: { $or : { attr: val1, attr: val2} } 
    

    ... WHERE attr = val2   
    

    where: { 
      user_id: 5, 
      $or : { attr: val1, attr: val2} 
    } 
    

    是

    ... WHERE user_id = 5 OR attr = val2  
    


    第三个有点模棱两可,我建议用 $in

    where: { attr: { $in: [val1, val2] } }
    

    ... WHERE attr IN (val1, val2)
    
    推荐文章