文本搜索仅返回相关的子文档 [英] Text search to return only relevant subdocuments

查看:55
本文介绍了文本搜索仅返回相关的子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文档结构:

{ 
    data : {
        "0" : {
                 "title" : "example1",...
         },
        "1" : {
                 "title" : "example2",...
         }
}              

我已经在此文档上创建了Text index:

I've created a Text index on this document:

db.collection.createIndex({"$**" : "text"}, {name: "collIndex"})

现在运行Text query

db.collection.find({$text : {$search: "example1"}})

我要拿回完整的Document.是否只能获取包含相关文本数据的子文档?例如上述查询将导致

I'm getting back the full Document. is it possible to get only the subDocument that contains the relevant text data? for example for the query mentioned above will result in

 { "0" : {"title" : "example1",...} ..}

-编辑--

与数组数据相同的问题:

same question with data an an array:

data : [{title: "example1"..}, {title : "example2"}]

我只能获取包含text query的数组条目吗?

can i get only the array entries that contains the text query?

推荐答案

否. MongoDB中的查询返回文档,而不是子文档.对于某些类型的查询,可以返回第一个匹配的数组元素,但是对于文本查询则不是这种情况.通常,您将能够使用聚合管道来提取所有匹配的数组元素,但是使用文本索引也是不可能的.如果希望查询与子文档匹配,则应重新设计架构,以使子文档成为完整的文档.

No. Queries in MongoDB return documents, not subdocuments. For some types of queries, it is possible to return the first matching array element, but this is not the case for text queries. Normally you'd be able to use the aggregation pipeline to extract all matching array elements, but this is also not possible with a text index. If you want queries to match subdocuments, you should redesign your schema so the subdocuments are full-fledged documents.

这篇关于文本搜索仅返回相关的子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆