0

我有一个包含description这样一个字段的文档:

{
  "_id": "item0",
  "description": {
    "parlist": [
      {
        "listitem": {
          "text": {
            "child": "page rous lady",
            "keyword": "officer e"
          }
        }
      },
      {
        "listitem": {
          "text": "shepherd noble "
        }
      }
    ]
  }
}

如何创建文本索引description并搜索特定单词?不知道深度能description走多远,描述的孩子会有多少。我尝试像这样创建索引:

db.collection.ensureIndex({description:"text"})

然后像这样查询:

db.collection.runCommand("text",{$search:"shepherd"})

但它不起作用。

4

2 回答 2

0

您可以简单地为所有文本字段构建一个文本索引:

db.collection.ensureIndex({ "$**": "text" })

然后使用关键字$text搜索:

db.collection.find( { $text: { $search: "shepherd" } } )
于 2014-04-23T10:20:58.397 回答
0

文本索引不适用于完整的子文档,但您可以创建一个包含多个字段的文本索引:

db.collection.ensureIndex(
     {
         "description.parlist.listitem.text": "text",
         "description.parlist.listitem.text.child": "text",
         "description.parlist.listitem.text.keyword": "text"
     }
)
于 2014-04-23T11:26:32.220 回答