2014-10-28 83 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 
    } 
) 

但会有第一查询更多的扫描。