如何在结构未知的馆藏中搜索? [英] How to search in a Collection with unknown structure?

查看:63
本文介绍了如何在结构未知的馆藏中搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几个小时阅读文档和论坛,试图找到以下问题的解决方案:

I spent several hours reading through docs and forums, trying to find a solution for the following problem:

在Mongo数据库中,我有一个包含一些非结构化数据的集合:

In A Mongo database, I have a collection with some unstructured data:

{数据":某些数据","_ id":"497ce96f395f2f052a494fd4"}

{"data" : "some data" , "_id" : "497ce96f395f2f052a494fd4"}

{"more_data":这里有更多数据","recursive_data":{"some_data":这里有更多数据","_id":"497ce96f395f2f052a4323"}

{"more_data" : "more data here" ,"recursive_data": {"some_data": "even more data here", "_id" : "497ce96f395f2f052a4323"}

{"more_unknown_data":字符串甚至字典","_id":"497ce96f395f2f052a494fsd2"}

{"more_unknown_data" : "string or even dictionaries" , "_id" : "497ce96f395f2f052a494fsd2"}

...

要注意的是,此集合中的元素没有预定义的结构,并且可以是无限级别.

The catch is that the elements in this collections don't have a predefined structure and they can be unlimited levels.

我的目标是创建一个查询,该查询在集合中进行搜索并找到与正则表达式匹配的所有元素(在键和值中).

My goal is to create a query, that searches through the collection and finds all the elements that match a regular expression( in both the keys and the values ).

例如,如果我有一个正则表达式:'^ even more'-它应该返回结构中某处具有字符串"even more"的所有元素.在这种情况下-这将是第二个.

For example, if I have a regex: '^even more' - It should return all the elements that have the string "even more" somewhere in the structure. In this case - that will be the second one.

推荐答案

简单地向每个对象添加一个数组,并使用您希望在其上进行搜索的字符串填充该数组.通常,我会将这些值小写以使不区分大小写的搜索变得容易.

Simply add an array to each object and populate it with the strings you want to be able to search on. Typically I'd lowercase those values to make case-insensitive search easy.

例如标签:[字符串1的副本",字符串2的副本",...]

e.g. Tags : ["copy of string 1", "copy of string 2", ...]

您可以扩展此技术以索引每个元素的每个单词.有时我还会在字段前面添加标识符,例如"genre:rock"允许在特定字段中搜索值(请仔细选择':'字符).

You can extend this technique to index every word of every element. Sometimes I also add the field with an identifier in front of it, e.g. "genre:rock" which allows searches for values in specific fields (choose the ':' character carefully).

在此数组上添加索引,现在您可以搜索集合中任何文档中的任何单词或短语,并且可以搜索"genre:rock"以在特定字段中搜索该值.

Add an index on this array and now you have the ability to search for any word or phrase in any document in the collection and you can search for "genre:rock" to search for that value in a specific field.

这篇关于如何在结构未知的馆藏中搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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