如何在任意深度查找 MongoDB 字段名称 [英] How to find MongoDB field name at arbitrary depth

查看:37
本文介绍了如何在任意深度查找 MongoDB 字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将某种草率的 XML 数据导入到 Mongo 数据库中.每个文档都有嵌套的子文档,深度约为 5-10.我想 find() 具有特定字段特定值的文档,其中该字段可能出现在子文档中的任何深度(并且可能出现多次).

I imported some sort-of sloppy XML data into a Mongo database. Each Document has nested sub-documents to a depth of around 5-10. I would like to find() documents that have a particular value of a particular field, where the field may appear at any depth in the sub-documents (and may appear multiple times).

我目前正在将每个文档提取到 Python 中,然后搜索该字典,但如果我能声明一个过滤器原型,数据库将只返回在其内容中某处具有特定字段名称值的文档,那就太好了.

I am currently pulling each Document into Python and then searching that dictionary, but it would be nice if I could state a filter prototype where the database would only return documents that have a particular value of the field name somewhere in their contents.

这是一个示例文档:

{
    "foo": 1,
    "bar": 2,
    "find-this": "Yes!",
    "stuff": {
        "baz": 3,
        "gobble": [
            "wibble",
            "wobble",
            {
                "all-fall-down": 4,
                "find-this": "please find me"
            }                
        ],
        "plugh": {
            "plove": {
                "find-this": "Here too!"
            }
        }
   }
}

因此,我想查找具有find-this"字段的文档,并且(如果可能)能够找到具有find-this"字段特定值的文档.

So, I'd like to find documents that have a "find-this" field, and (if possible) to be able to find documents that have a particular value of a "find-this" field.

推荐答案

您对 BSON 文档不是 XML 文档的某些声明是正确的.由于 XML 被加载到由节点"组成的树结构中,因此搜索任意键非常容易.

You are right in the certain statement of a BSON document is not an XML document. Since XML is loaded into a tree structure that comprises of "nodes", searching on an arbitary key is quite easy.

一个MonoDB文档处理起来没那么简单,而且它在很多方面都是一个数据库",所以一般都希望数据位置有一定的一致性",以便于两者的索引"和搜索.

A MonoDB document is not so simple to process, and this is a "database" in many respects, so it is generally expected to have a certain "uniformity" of data locations in order to make it easy to both "index" and search.

尽管如此,它还是可以做到的.但这当然意味着在服务器上执行递归过程,这意味着使用 $where.

Nonetheless, it can be done. But of course this does mean a recursive process executing on the server and this means JavaScript processing with $where.

作为一个基本的 shell 示例,但是一般的 function 只是一个字符串参数给 $where 操作符在其他地方:

As a basic shell example, but the general function is just a string argument to the $where operator everywhere else:

db.collection.find(
  function () {
    var findKey = "find-this",
        findVal = "please find me";

    function inspectObj(doc) {
      return Object.keys(doc).some(function(key) {
        if ( typeof(doc[key]) == "object" ) {
          return inspectObj(doc[key]);
        } else {
          return ( key == findKey && doc[key] == findVal );
        }
      });
    }
    return inspectObj(this);
  }
)

所以基本上,测试对象中存在的键,看看它们是否匹配所需的字段名称"和内容.如果这些键之一恰好是对象",则递归到函数中并再次检查.

So basically, test the keys present in the object to see if they match the desired "field name" and content. If one of those keys happens to be an "object" then recurse into the function and inspect again.

JavaScript .some() 确保找到的第一个"匹配项将从搜索函数返回,给出 true 结果并返回在某个深度存在键/值"的对象.

JavaScript .some() makes sure that the "first" match found will return from the search function giving a true result and returning the object where that "key/value" was present at some depth.

请注意,$where 本质上意味着遍历整个集合,除非有其他一些有效的查询过滤器可以应用于集合的索引".

Note that $where essentially means traversing your whole collection unless there is some other valid query filter than can be applied to an "index" on the collection.

因此请谨慎使用,或者根本不使用,只需将数据重新构建为更可行的形式即可.

So use with care, or not at all and just work with re-structring the data into a more workable form.

但这会给你匹配.

这篇关于如何在任意深度查找 MongoDB 字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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