Mongodb fuzzy query and $ type usage

Source: Internet
Author: User
Mongodb uses fuzzy search and $ type for recent monitoring services. Because non-numeric data is collected, the monitoring charts cannot be displayed normally. Therefore, you need to find and delete the data, the developer then corrects the data inserted into the database from the source and no longer produces non-numeric content. The following is an example: Create test data: for (i1; i100; I ++

Mongodb uses fuzzy search and $ type for recent monitoring services. Because non-numeric data is collected, the monitoring charts cannot be displayed normally. Therefore, you need to find and delete the data, the developer then corrects the data inserted into the database from the source and no longer produces non-numeric content. The following is an example: Create test data: for (I = 1; I = 100; I ++

Mongodb fuzzy query and $ type usage

Recently, a Monitoring Service has failed to display monitoring charts because non-numeric data is collected. Therefore, you need to find and delete the data, and then the developer can correct the data inserted into the database from the source, non-numeric content is no longer generated.

The following is an example:

Create 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": 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 ("504ecc5e8dc12ed9748316"), "id": 1, "content": 666888, "name": "joe1 "}

{"_ Id": ObjectId ("504ecc5e8dc12ed9748317"), "id": 2, "content": 666888, "name": "joe2 "}

{"_ Id": ObjectId ("504ecc5e8dc12ed9748318"), "id": 3, "content": 666888, "name": "joe3 "}

{"_ Id": ObjectId ("504ecc5e8dc12ed9748319"), "id": 4, "content": 666888, "name": "joe4 "}

{"_ Id": ObjectId ("504ecc5e8dc12ed974831a"), "id": 5, "content": 666888, "name": "joe5 "}

{"_ Id": ObjectId ("504ecc5e8dc12ed974831b"), "id": 6, "content": 666888, "name": "joe6 "}

{"_ Id": ObjectId ("504ecc5e8dc12ed974831c"), "id": 7, "content": 666888, "name": "joe7 "}

{& Quot; _ id & quot;: ObjectId (& quot; 504ecc5e8dc12ed974831d & quot;), & quot; id & quot;: 8, & quot; content & quot;: 666888, & quot; name & quot;: "joe8 "}

{& Quot; _ id & quot;: ObjectId (& quot; 504ecc5e8dc12ed974831e & quot;), & quot; id & quot;: 9, & quot; content & quot;: 666888, & quot; name & quot;: "joe9 "}

You can use fuzzy search to find related data:

> Db. test. find ({name:/joe/}) --- query data that contains joe in the name field is equivalent to db. test. find ({name :{$ regex: 'job '}})

{"_ Id": ObjectId ("504ecc5e8dc12ed9748316"), "id": 1, "content": 666888, "name": "joe1 "}

{"_ Id": ObjectId ("504ecc5e8dc12ed9748317"), "id": 2, "content": 666888, "name": "joe2 "}

{"_ Id": ObjectId ("504ecc5e8dc12ed9748318"), "id": 3, "content": 666888, "name": "joe3 "}

{"_ Id": ObjectId ("504ecc5e8dc12ed9748319"), "id": 4, "content": 666888, "name": "joe4 "}

{"_ Id": ObjectId ("504ecc5e8dc12ed974831a"), "id": 5, "content": 666888, "name": "joe5 "}

{"_ Id": ObjectId ("504ecc5e8dc12ed974831b"), "id": 6, "content": 666888, "name": "joe6 "}

{"_ Id": ObjectId ("504ecc5e8dc12ed974831c"), "id": 7, "content": 666888, "name": "joe7 "}

{& Quot; _ id & quot;: ObjectId (& quot; 504ecc5e8dc12ed974831d & quot;), & quot; id & quot;: 8, & quot; content & quot;: 666888, & quot; name & quot;: "joe8 "}

{& Quot; _ id & quot;: ObjectId (& quot; 504ecc5e8dc12ed974831e & quot;), & quot; id & quot;: 9, & quot; content & quot;: 666888, & quot; name & quot;: "joe9 "}

Db. test. if I is added to find ({name:/joe/I}), it will not be case sensitive and will be displayed, which is equivalent to db. test. find ({name: {$ regex: 'job', $ options: 'I '}})

Db. test. find ({name:/^ joe/I}) ^ matches the string starting with joe and is case insensitive.

$ Type usage:

> Db. test. find ({content: {$ type: 2}) -- displays data whose content is 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}) -- display content of the double type

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

The list of mongodb Based on the bson type is given below:

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 the corresponding data based on the above list.

Find the relevant data based on the above method. After removing the data, the chart returns to normal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.