mongodb 模糊查詢以及$type使用

來源:互聯網
上載者:User

標籤:des   style   blog   http   os   io   使用   java   ar   

mongodb 模糊查詢以及$type使用 

2012-09-11 14:13:30|  分類: mongodb |  標籤: |舉報 |字型大小大中小 訂閱

      最近有一監控業務,由於資料擷取到非數字內容,導致監控圖表無法正常顯示,所以要找出這部分資料,進行刪除,然後開發員從源頭更正插入資料庫的資料,不再產生非數字內容。    下面舉一個例子:建立測試資料:for(i=1;i<=100;i++){db.test.insert({id:i,content:"test content",name:"wang"+i});}> db.test.find(){ "_id" : ObjectId("504ece595e8dc12ed97482b2"), "id" : 1, "content" : "test content", "name" : "wang1" }{ "_id" : ObjectId("504ece595e8dc12ed97482b3"), "id" : 2, "content" : "test content", "name" : "wang2" }{ "_id" : ObjectId("504ece595e8dc12ed97482b4"), "id" : 3, "content" : "test content", "name" : "wang3" }{ "_id" : ObjectId("504ece595e8dc12ed97482b5"), "id" : 4, "content" : "test content", "name" : "wang4" }{ "_id" : ObjectId("504ece595e8dc12ed97482b6"), "id" : 5, "content" : "test content", "name" : "wang5" }{ "_id" : ObjectId("504ece595e8dc12ed97482b7"), "id" : 6, "content" : "test content", "name" : "wang6" }{ "_id" : ObjectId("504ece595e8dc12ed97482b8"), "id" : 7, "content" : "test content", "name" : "wang7" }{ "_id" : ObjectId("504ece595e8dc12ed97482b9"), "id" : 8, "content" : "test content", "name" : "wang8" }{ "_id" : ObjectId("504ece595e8dc12ed97482ba"), "id" : 9, "content" : "test content", "name" : "wang9" } for(i=1;i<=101;i++){db.test.insert({id:i,content:666888,name:"joe"+i});}> db.test.find({content:666888}){ "_id" : ObjectId("504ececc5e8dc12ed9748316"), "id" : 1, "content" : 666888, "name" : "joe1" }{ "_id" : ObjectId("504ececc5e8dc12ed9748317"), "id" : 2, "content" : 666888, "name" : "joe2" }{ "_id" : ObjectId("504ececc5e8dc12ed9748318"), "id" : 3, "content" : 666888, "name" : "joe3" }{ "_id" : ObjectId("504ececc5e8dc12ed9748319"), "id" : 4, "content" : 666888, "name" : "joe4" }{ "_id" : ObjectId("504ececc5e8dc12ed974831a"), "id" : 5, "content" : 666888, "name" : "joe5" }{ "_id" : ObjectId("504ececc5e8dc12ed974831b"), "id" : 6, "content" : 666888, "name" : "joe6" }{ "_id" : ObjectId("504ececc5e8dc12ed974831c"), "id" : 7, "content" : 666888, "name" : "joe7" }{ "_id" : ObjectId("504ececc5e8dc12ed974831d"), "id" : 8, "content" : 666888, "name" : "joe8" }{ "_id" : ObjectId("504ececc5e8dc12ed974831e"), "id" : 9, "content" : 666888, "name" : "joe9" } 通過模糊查詢,尋找相關資料:> db.test.find({name:/joe/})    ---查詢name欄位含有joe的資料 等同於db.test.find({name:{$regex:‘joe‘}}){ "_id" : ObjectId("504ececc5e8dc12ed9748316"), "id" : 1, "content" : 666888, "name" : "joe1" }{ "_id" : ObjectId("504ececc5e8dc12ed9748317"), "id" : 2, "content" : 666888, "name" : "joe2" }{ "_id" : ObjectId("504ececc5e8dc12ed9748318"), "id" : 3, "content" : 666888, "name" : "joe3" }{ "_id" : ObjectId("504ececc5e8dc12ed9748319"), "id" : 4, "content" : 666888, "name" : "joe4" }{ "_id" : ObjectId("504ececc5e8dc12ed974831a"), "id" : 5, "content" : 666888, "name" : "joe5" }{ "_id" : ObjectId("504ececc5e8dc12ed974831b"), "id" : 6, "content" : 666888, "name" : "joe6" }{ "_id" : ObjectId("504ececc5e8dc12ed974831c"), "id" : 7, "content" : 666888, "name" : "joe7" }{ "_id" : ObjectId("504ececc5e8dc12ed974831d"), "id" : 8, "content" : 666888, "name" : "joe8" }{ "_id" : ObjectId("504ececc5e8dc12ed974831e"), "id" : 9, "content" : 666888, "name" : "joe9" } db.test.find({name:/joe/i})   加了i,那麼就不會區分大小寫,都會顯示,等同於db.test.find({name:{$regex:‘joe‘,$options:‘i‘}})db.test.find({name:/^joe/i})   ^匹配以joe開頭的,而且不區分大小寫  $type使用:> db.test.find({content:{$type:2}}) --顯示content是string的資料{ "_id" : ObjectId("504ece595e8dc12ed97482b2"), "id" : 1, "content" : "test content", "name" : "wang1" }{ "_id" : ObjectId("504ece595e8dc12ed97482b3"), "id" : 2, "content" : "test content", "name" : "wang2" }{ "_id" : ObjectId("504ece595e8dc12ed97482b4"), "id" : 3, "content" : "test content", "name" : "wang3" }{ "_id" : ObjectId("504ece595e8dc12ed97482b5"), "id" : 4, "content" : "test content", "name" : "wang4" }{ "_id" : ObjectId("504ece595e8dc12ed97482b6"), "id" : 5, "content" : "test content", "name" : "wang5" }{ "_id" : ObjectId("504ece595e8dc12ed97482b7"), "id" : 6, "content" : "test content", "name" : "wang6" }{ "_id" : ObjectId("504ece595e8dc12ed97482b8"), "id" : 7, "content" : "test content", "name" : "wang7" }{ "_id" : ObjectId("504ece595e8dc12ed97482b9"), "id" : 8, "content" : "test content", "name" : "wang8" }{ "_id" : ObjectId("504ece595e8dc12ed97482ba"), "id" : 9, "content" : "test content", "name" : "wang9" }> db.test.find({content:{$type:1}})  --顯示content為double類型的{ "_id" : ObjectId("504ecfcc5e8dc12ed974837b"), "id" : 1, "content" : 666888, "name" : "JOE1" }{ "_id" : ObjectId("504ecfcc5e8dc12ed974837c"), "id" : 2, "content" : 666888, "name" : "JOE2" }{ "_id" : ObjectId("504ecfcc5e8dc12ed974837d"), "id" : 3, "content" : 666888, "name" : "JOE3" }{ "_id" : ObjectId("504ecfcc5e8dc12ed974837e"), "id" : 4, "content" : 666888, "name" : "JOE4" }{ "_id" : ObjectId("504ecfcc5e8dc12ed974837f"), "id" : 5, "content" : 666888, "name" : "JOE5" }{ "_id" : ObjectId("504ecfcc5e8dc12ed9748380"), "id" : 6, "content" : 666888, "name" : "JOE6" }{ "_id" : ObjectId("504ecfcc5e8dc12ed9748381"), "id" : 7, "content" : 666888, "name" : "JOE7" }{ "_id" : ObjectId("504ecfcc5e8dc12ed9748382"), "id" : 8, "content" : 666888, "name" : "JOE8" }{ "_id" : ObjectId("504ecfcc5e8dc12ed9748383"), "id" : 9, "content" : 666888, "name" : "JOE9" }{ "_id" : ObjectId("504ecfcc5e8dc12ed9748384"), "id" : 10, "content" : 666888, "name" : "JOE10" }下面給出mongodb基於bson類型的列表:
Type Description Type value
Double 1
String 2
Object 3
Array 4
Binary data 5
Object id 7
Boolean 8
Date 9
Null 10
Regular expression 11
JavaScript code 13
Symbol 14
JavaScript code with scope 15
32-bit integer 16
Timestamp 17
64-bit integer 18
Min key 255
Max key 127
那麼可以根據以上的列表尋找出對應資料。根據以上方法找出相關資料,remove後,圖表恢複正常

mongodb 模糊查詢以及$type使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.