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

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

问题描述

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

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!"
            }
        }
   }
}

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

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天全站免登陆