mongodb 模糊查询以及$type使用

本文介绍了一种解决监控图表因非数字内容无法正常显示的问题的方法。通过在MongoDB中使用特定查询语法来查找并移除这些数据,最终使监控图表恢复正常。文中还详细解释了如何利用不同类型的数据类型进行精确查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近有一监控业务,由于数据采集到非数字内容,导致监控图表无法正常显示,所以要找出这部分数据,进行删除,然后开发员从源头更正插入数据库的数据,不再产生非数字内容。<wbr style=""><div style=""> 下面举一个例子:</div> <div style=""> <div style="line-height:25px">建立测试数据:</div> <div style="line-height:25px">for(i=1;i&lt;=100;i++){db.test.insert({id:i,content:"test content",name:"wang"+i});}</div> <div style="line-height:25px">&gt; db.test.find()</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b2"), "id" : 1, "content" : "test content", "name" : "wang1" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b3"), "id" : 2, "content" : "test content", "name" : "wang2" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b4"), "id" : 3, "content" : "test content", "name" : "wang3" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b5"), "id" : 4, "content" : "test content", "name" : "wang4" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b6"), "id" : 5, "content" : "test content", "name" : "wang5" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b7"), "id" : 6, "content" : "test content", "name" : "wang6" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b8"), "id" : 7, "content" : "test content", "name" : "wang7" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b9"), "id" : 8, "content" : "test content", "name" : "wang8" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482ba"), "id" : 9, "content" : "test content", "name" : "wang9" }</div> <div style="line-height:25px"><br style="line-height:25px"></div> <div style="line-height:25px">for(i=1;i&lt;=101;i++){db.test.insert({id:i,content:666888,name:"joe"+i});}</div> <div style="line-height:25px">&gt; db.test.find({content:666888})</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed9748316"), "id" : 1, "content" : 666888, "name" : "joe1" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed9748317"), "id" : 2, "content" : 666888, "name" : "joe2" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed9748318"), "id" : 3, "content" : 666888, "name" : "joe3" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed9748319"), "id" : 4, "content" : 666888, "name" : "joe4" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed974831a"), "id" : 5, "content" : 666888, "name" : "joe5" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed974831b"), "id" : 6, "content" : 666888, "name" : "joe6" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed974831c"), "id" : 7, "content" : 666888, "name" : "joe7" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed974831d"), "id" : 8, "content" : 666888, "name" : "joe8" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed974831e"), "id" : 9, "content" : 666888, "name" : "joe9" }</div> <div style="line-height:25px"><br style="line-height:25px"></div> <div style="line-height:25px">通过模糊查询,查找相关数据:</div> <div style="line-height:25px">&gt; db.test.find({name:/joe/}) ---查询name字段含有joe的数据 等同于db.test.find({name:{$regex:'joe'}})</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed9748316"), "id" : 1, "content" : 666888, "name" : "joe1" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed9748317"), "id" : 2, "content" : 666888, "name" : "joe2" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed9748318"), "id" : 3, "content" : 666888, "name" : "joe3" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed9748319"), "id" : 4, "content" : 666888, "name" : "joe4" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed974831a"), "id" : 5, "content" : 666888, "name" : "joe5" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed974831b"), "id" : 6, "content" : 666888, "name" : "joe6" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed974831c"), "id" : 7, "content" : 666888, "name" : "joe7" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed974831d"), "id" : 8, "content" : 666888, "name" : "joe8" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ececc5e8dc12ed974831e"), "id" : 9, "content" : 666888, "name" : "joe9" }</div> <div style="line-height:25px"><br style="line-height:25px"></div> <div style="line-height:25px">db.test.find({name:/joe/i}) 加了i,那么就不会区分大小写,都会显示,等同于db.test.find({name:{$regex:'joe',$options:'i'}})</div> <div style="line-height:25px">db.test.find({name:/^joe/i}) ^匹配以joe开头的,而且不区分大小写</div> <div style="line-height:25px"><br style="line-height:25px"></div> <div style="line-height:25px"><br style="line-height:25px"></div> <div style="line-height:25px">$type使用:</div> <div style="line-height:25px">&gt; db.test.find({content:{$type:2}}) --显示content是string的数据</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b2"), "id" : 1, "content" : "test content", "name" : "wang1" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b3"), "id" : 2, "content" : "test content", "name" : "wang2" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b4"), "id" : 3, "content" : "test content", "name" : "wang3" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b5"), "id" : 4, "content" : "test content", "name" : "wang4" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b6"), "id" : 5, "content" : "test content", "name" : "wang5" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b7"), "id" : 6, "content" : "test content", "name" : "wang6" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b8"), "id" : 7, "content" : "test content", "name" : "wang7" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482b9"), "id" : 8, "content" : "test content", "name" : "wang8" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ece595e8dc12ed97482ba"), "id" : 9, "content" : "test content", "name" : "wang9" }</div> <div style="line-height:25px">&gt; db.test.find({content:{$type:1}}) --显示content为double类型的</div> <div style="line-height:25px">{ "_id" : ObjectId("504ecfcc5e8dc12ed974837b"), "id" : 1, "content" : 666888, "name" : "JOE1" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ecfcc5e8dc12ed974837c"), "id" : 2, "content" : 666888, "name" : "JOE2" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ecfcc5e8dc12ed974837d"), "id" : 3, "content" : 666888, "name" : "JOE3" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ecfcc5e8dc12ed974837e"), "id" : 4, "content" : 666888, "name" : "JOE4" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ecfcc5e8dc12ed974837f"), "id" : 5, "content" : 666888, "name" : "JOE5" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ecfcc5e8dc12ed9748380"), "id" : 6, "content" : 666888, "name" : "JOE6" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ecfcc5e8dc12ed9748381"), "id" : 7, "content" : 666888, "name" : "JOE7" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ecfcc5e8dc12ed9748382"), "id" : 8, "content" : 666888, "name" : "JOE8" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ecfcc5e8dc12ed9748383"), "id" : 9, "content" : 666888, "name" : "JOE9" }</div> <div style="line-height:25px">{ "_id" : ObjectId("504ecfcc5e8dc12ed9748384"), "id" : 10, "content" : 666888, "name" : "JOE10" }</div> </div> <div style="">下面给出mongodb基于bson类型的列表:</div> <div style=""> <span style="font-family:Verdana,Arial,Helvetica,sans-serif; line-height:normal"></span> <table style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; line-height:23px; width:540px; border-collapse:collapse; margin-bottom:20px; margin-left:6px; font-size:13px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid"><tbody style="line-height:23px"> <tr style="line-height:23px"> <th style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; background-color:rgb(238,238,238); padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; font-weight:bold; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Type Description</th> <th style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; background-color:rgb(238,238,238); padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; font-weight:bold; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Type value</th> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Double</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 1</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> String</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 2</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Object</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 3</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Array</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 4</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Binary data</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 5</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Object id</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 7</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Boolean</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 8</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Date</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 9</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Null</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 10</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Regular expression</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 11</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> JavaScript code</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 13</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Symbol</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 14</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> JavaScript code with scope</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 15</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 32-bit integer</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 16</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Timestamp</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 17</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 64-bit integer</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 18</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Min key</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 255</td> </tr> <tr style="line-height:23px"> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> Max key</td> <td style="border-bottom:rgb(192,192,192) 1px solid; border-left:rgb(192,192,192) 1px solid; padding-bottom:4px; line-height:23px; padding-left:4px; padding-right:4px; border-top:rgb(192,192,192) 1px solid; border-right:rgb(192,192,192) 1px solid; padding-top:4px"> 127</td> </tr> </tbody></table> <span style="font-size:12px; line-height:23px">那么可以根据以上的列表查找出对应数据。</span> </div> <div style=""><span style="font-family:Verdana,Arial,Helvetica,sans-serif; line-height:normal"><span style="font-size:12px; line-height:23px">根据以上方法找出相关数据,remove后,图表恢复正常</span></span></div> </wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值