【MongoDB初識】-條件操作符

來源:互聯網
上載者:User

標籤:

1、條件>,<,>=,<=在MongoDB中的寫法

  >:$gt,<:$lt,>=:$gte,<=:$lte,<>:$ne

  具體使用方法:

db.class.find({"filed":{$gt:value}})  filed>value

db.class.find({"filed":{$lt:value}})   filed<value

db.class.find({"filed":{$gte:value}})   filed>=value

db.class.find({"filed":{$lte:value}})  filed<=value

db.class.find({"filed":{$gt:value,$lt:value1}})  value1>filed>value

db.class.find({"filed":{$ne:5}})    filed<>value

db.class.find({"stuCount":{$gt:10}})db.class.find({"stuCount":{$gte:10}})
db.class.find({"stuCount":{$lt:10}})db.class.find({"stuCount":{$lte:10}})
db.class.find({"stuCount":{$lt:10,$gt:5}})
db.class.find({"stuCount":{$ne:5}})

2、$all匹配所有

    這個操作符跟 SQL 文法的 in 類似,但不同的是, in 只需滿足( )內的某一個值即可, 而$all 必
  須滿足[ ]內的所有值

  db.class.find({stuCount:{$all:[6,8]}}) 查詢結果為空白

  db.class.find({stuCount:{$all:[6]}}) 可以查出一條,等同於db.class.find({"stuCount":6})

3、$in包含

  與 sql 標準文法的用途是一樣的,即要查詢的是一系列枚舉值的範圍內

  db.class.find({stuCount:{$in:[6,8]}})

4、$nin不包含

  db.class.find({stuCount:{$nin:[6,8]}})

5、判斷欄位是否存在

  db.class.find({stuCount:{$exists:true}})

6、NULL值處理

  db.class.find({name:null}) 會查詢出沒有name欄位的資料

  db.class.find({name:{"$in":[null],"$exists":true}}) 查詢結果為有name欄位,且name=null

7、$mod取餘

  db.class.find({stuCount:{$mod:[7,2]}})  對7取餘等於2

 

【MongoDB初識】-條件操作符

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.