当前位置:数据库 > mongodb >>

mongodb模糊查询以及$type使用

mongodb模糊查询以及$type使用
 
最近有一监控业务,由于数据采集到非数字内容,导致监控图表无易做图常显示,所以要找出这部分数据,进行删除,然后开发员从源头更正插入数据库的数据,不再产生非数字内容。
    下面举一个例子:
建立测试数据:
 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" }
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,