MongoDB fuzzy Query and $type use
2012-09-11 14:13:30| Category: MongoDB | Tags: | report | font size < Span class= "FC04 stag" > great small subscription
recently has a monitoring business, because the data collection to non-digital content, resulting in the monitoring chart can not display properly, so to find this part of the data, delete, and then the developer from the source to correct the data inserted into the database, no longer produce non-digital content. Here's an example: Building test Data: 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": objecti D ("504ece595e8dc12ed97482b4"), "id": 3, "Content": "Test Content", "name": "Wang3"} {"_id": ObjectId ("504ece595e8dc12 Ed97482b5 ")," 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 "," NA Me ":" Wang7 "} {" _id ": ObjectId (" 504eCe595e8dc12ed97482b9 ")," id ": 8," content ":" Test Content "," name ":" Wang8 "} {" _id ": ObjectId (" 504ece595e8dc12ed97482 Ba ")," id ": 9," content ":" Test Content "," name ":" Wang9 "} for (i=1;i<=101;i++) {Db.test.insert ({Id:i,content:6 66888,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": 666 888, "name": "Joe5"} {"_id": ObjectId ("504ececc5e8dc12ed974831b"), "id": 6, "content": 666888, "name": "Joe6"} {"_i D ": ObjectId (" 504ececc5e8dc12ed974831c ")," id ": 7," content ": 666888," name ":" Joe7 "} {" _id ": ObjectId (" 504ececc5e8d C12ed974831d ")," id ": 8," content ": 666888," name ":" Joe8 "} {" _id ": ObjectId (" 504ececc5e8dc12ed974831e ")," id ": 9," con Tent ": 666888," name ":" Joe9 "} through fuzzy query, find related data:> db.test.find ({name:/joe/}) --- The Query Name field contains Joe's data equivalent to 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": 66 6888, "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}) add I, then it will not be case sensitive, will be displayed, equivalent to Db.test.find ({name:{$regex: ' Joe ', $options: ' I '}}) Db.test.find ({name:/^joe/i}) ^ match with Joe, and case-insensitive $type use:> Db.test.find ({content:{$type: 2})--Displays the content is a string data {"_id": ObjectId ("504ece595e8dc12ed97482b2"), "id": 1, " Content ":" Test Content "," name ":" Wang1 "} {" _id ": ObjectId (" 504ece595e8dc12ed97482b3 ")," id ": 2," Content ":" Test C Ontent "," 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 ("504ece5 95e8dc12ed97482b7 ")," 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": objecti D ("504ece595e8dc12ed97482ba"), "id": 9, "content": "Test Content", "name": "Wang9"}> db.test.find ({content:{$type: 1 }}) --Displays {"_id" with content of type double: ObjectId ("504ecfcc5e8dc12ed974837b"), "id": 1, "content": 666888, "name": "JOE 1 "} {" _id ": ObjectId (" 504ecfcc5e8dc12ed974837c ")," id ": 2," content ": 666888," name ":" JOE2 "} {" _id ": ObjectId (" 504 Ecfcc5e8dc12ed974837d ")," id ": 3," content ": 666888," name ":" JOE3 "} {" _id ": ObjectId (" 504ecfcc5e8dc12ed974837e ")," I D ": 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," c Ontent ": 666888," name ":" JOE8 "} {" _id ": ObjectId (" 504ecfcc5e8dc12ed9748383 ")," id ": 9," content ": 666888," name ": "JOE9"} {"_id": ObjectId ("504ecfcc5e8dc12ed9748384"), "id": Ten, "Content": 666888, "name": "JOE10"} below gives MongoDB based on Bson List of types:
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 |
then you can find out the corresponding data based on the above list. according to the above method to find the relevant data, remove, the chart back to normal
MongoDB fuzzy Query and $type use