2015-06-10 63 views
0

我有一個包含類的集合,每個文件看起來是這樣的:MongoDB的文本搜索不會對長字符串的工作,但工作在簡短的字符串

一個文檔的小片段:

{ 
    "category" : "Programming", 
    "keywords" : [ 
     "SQL", 
     "PHP", 
     "C++" 
    ] 
} 

當我運行下面的命令($搜索是從計算器上發現了一個問題):

db.categories.find({ 
    $text : { 
     $search: 'If user input is inserted without modification into an SQL query, then the application becomes vulnerable to SQL injection, like in the following example: $unsafe_variable = $_POST[\'user_input\']; mysql_query("INSERT INTO `table` (`column`) VALUES (\'$unsafe_variable\')"); That\'s because the user can input something like value\'); DROP TABLE table;--, and the query becomes: INSERT INTO `table` (`column`) VALUES(\'value\'); DROP TABLE table;--\') What can be done to prevent this from happening?' 
    } 
}, {category: 1}) 

我沒有得到任何結果回來了,但是當我截斷字符串看起來像THI s:

db.categories.find({ 
    $text : { 
     $search: 'If user input is inserted without modification into an SQL query' 
    } 
}, {category: 1}) 

我確實得到了結果。爲什麼較長的字符串不會給我結果,而第二個較短的字符串會給我結果?

+0

我敢說你的用例在這裏似乎與文本搜索的意圖相反。一般來說,輸入是關鍵術語或「標記」,並且您希望在文檔的存儲字符串中搜索這些術語。您正在尋找一個「句子」作爲輸入,並希望將它與存儲在文檔中的標記或「標記」相匹配。您也可以將MongoDB文本搜索視爲「精簡」選項。如果您希望獲得更多的文本搜索功能,則最好使用專用的「全文搜索」產品。 –

+0

關於我能做什麼的建議?基本上我想採取該字符串,並與關鍵字進行比較,並獲得最佳匹配類別 –

回答

0

好的,我找出了什麼是錯的,我做了「精確搜索」和「排除搜索」,因爲我有引號圍繞項目和-標誌。刪除,使我可以搜索我想要的東西,找回我正在尋找的東西。

db.categories.find({ 
    $text : { 
     $search: 'If user input is inserted without modification into an SQL query then the application becomes vulnerable to SQL injection like in the following example unsafevariable POST user input mysql query INSERT INTO table column VALUES unsafevariable Thats because the user can input something like value DROP TABLE table and the query becomes INSERT INTO table column VALUES value DROP TABLE table What can be done to prevent this from happening' 
    } 
}, {category: 1}) 
相關問題