2014-10-28 59 views
1

我看到,我們可以限制使用本文檔中提到的方法MongoDB的文本搜索掃描的項目數:http://docs.mongodb.org/manual/tutorial/limit-number-of-items-scanned-for-text-search/MongoDB的文本搜索

讓我寫在這裏短暫。我有庫存文檔的集合:

{ _id: 1, dept: "tech", description: "lime green computer" } 
{ _id: 2, dept: "tech", description: "wireless red mouse" } 
{ _id: 3, dept: "kitchen", description: "green placemat" } 
{ _id: 4, dept: "kitchen", description: "red peeler" } 
{ _id: 5, dept: "food", description: "green apple" } 
{ _id: 6, dept: "food", description: "red potato" } 

然後創建一個索引:

db.inventory.ensureIndex(
    { 
    dept: 1, 
    description: "text" 
    } 
) 

我可以寫這個查詢和它的作品:

db.inventory.find({ dept: "kitchen", $text: { $search: "green" } }) 

現在,如果我用這個方法,我可以在特定的部門進行搜索。我的問題是,我想要這個功能,但我也想要在所有部門搜索的自由。但是,此查詢不起作用:db.inventory.find({ $text: { $search: "green" } })

我正在使用版本2.6.5。

回答

0

您創建的索引是複合索引,因此無法運行db.inventory.find({ $text: { $search: "green" } })

如果你想同時運行,你可以改變指數

db.inventory.ensureIndex(
    { 
    description: "text", 
    dept: 1 
    } 
) 

但會有第一查詢更多的掃描。