当前位置:Gxlcms > mysql > mongodb模糊查询以及$type使用

mongodb模糊查询以及$type使用

时间:2021-07-01 10:21:17 帮助过:5人阅读

mongodb模糊查询以及$type使用 最近有一监控业务,由于数据采集到非数字内容,导致监控图表无法正常显示,所以要找出这部分数据,进行删除,然后开发员从源头更正插入数据库的数据,不再产生非数字内容。 下面举一个例子: 建立测试数据: for(i=1;i=100;i++

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" }

{ "_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后,图表恢复正常

人气教程排行