查询和过滤键名而不是MongoDB中的值 [英] Query and filter key names instead of values in MongoDB

查看:354
本文介绍了查询和过滤键名而不是MongoDB中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从集合中找到与某个字符串部分匹配的所有键名.

I want to find all key names from a collection that partially match a certain string.

我最接近的是检查某个键是否存在,但这是完全匹配的:

The closest I got was to check if a certain key exists, but that's an exact match:

db.collection.find({ "fkClientID": { $exists:1 }})

我想获取所有以fk开头的键.

I'd like to get all keys that start with fk instead.

推荐答案

如果您拥有最新的MongoDB 3.4.4,则可以在$objectToArray. rel ="nofollow noreferrer"> $redact 作为最快速的最快方式,这可以通过本机运算符完成.并不是说扫描馆藏是快速的".但要尽快做到这一点:

If you have the latest MongoDB 3.4.4 then you can use $objectToArray in an aggregate statement with $redact as the the most blazing fast way this can possibly be done with native operators. Not that scanning the collection is "fast". but as fast as you get for this:

db[collname].aggregate([
  { "$redact": {
    "$cond": {
      "if": {
        "$gt": [
          { "$size": { "$filter": {
            "input": { "$objectToArray": "$$ROOT" },
            "as": "doc",
            "cond": {
              "$eq": [ { "$substr": [ "$$doc.k", 0, 2 ] }, "fk" ]
            }
          }}},
          0
        ]
      },
      "then": "$$KEEP",
      "else": "$$PRUNE"
    }
  }}
 ])

目前未公开的$objectToArray将数组中的对象"转换为键"和值"形式.因此:

The presently undocumented $objectToArray translates an "object" into "key" and "value" form in an array. So this:

{ "a": 1, "b": 2 }

成为这个:

[{ "k": "a", "v": 1 }, { "k": "b", "v": 2 }]

$$ROOT一起使用,这是一个引用当前文档对象"的特殊变量,我们将其转换为数组,以便可以检查"k"的值.

Used with $$ROOT which is a special variable referring to the current document "object", we translate to an array so the values of "k" can be inspected.

然后只需要应用$filter并使用$substr来获取键"字符串的前面的字符即可.

Then it's just a matter of applying $filter and using $substr to get the preceding characters of the "key" string.

作为记录,这将是MongoDB 3.4.4获得匹配键的唯一列表的最佳方法:

For the record, this would be the MongoDB 3.4.4 optimal way of obtaining an unique list of the matching keys:

db[collname].aggregate([
  { "$redact": {
    "$cond": {
      "if": {
        "$gt": [
          { "$size": { "$filter": {
            "input": { "$objectToArray": "$$ROOT" },
            "as": "doc",
            "cond": {
              "$eq": [ { "$substr": [ "$$doc.k", 0, 2 ] }, "fk" ]
            }
          }}},
          0
        ]
      },
      "then": "$$KEEP",
      "else": "$$PRUNE"
    }
  }},
  { "$project": { 
    "j": {
      "$filter": {
        "input": { "$objectToArray": "$$ROOT" },
         "as": "doc",
         "cond": {
           "$eq": [ { "$substr": [ "$$doc.k", 0, 2 ] }, "fk" ]
         }
      }
    }
  }},
  { "$unwind": "$j" },
  { "$group": { "_id": "$j.k" }}
 ])

这是安全的规定,它正在考虑该密钥可能并非在所有文档中都存在,并且该文档中可能存在多个密钥.

That's the safe provision, which is considering that the key may not be present in all documents and that there could possibly be multiple keys in the document.

如果您完全确定您始终"在文档中存在密钥,并且只有一个,那么您可以将其缩写为$group:

If you are absolutely certain that you "always" have the key present in the document and that there will only be one, then you can shorten to just $group:

db[colname].aggregate([
  { "$group": { 
    "_id": {
      "$arrayElemAt": [
        { "$map": {
          "input": { "$filter": {
            "input": { "$objectToArray": "$$ROOT" },
             "as": "doc",
             "cond": {
               "$eq": [ { "$substr": [ "$$doc.k", 0, 2 ] }, "fk" ]
             }
          }},
          "as": "el",
          "in": "$$el.k"
        }},
        0
      ]
    }
  }}
 ])


早期版本中最有效的方法是使用 $where 允许对JavaScript表达式求值的语法.并不是说评估JavaScript的任何事情是您可以做的最高效"的事情,但是对于任何数据存储来说,分析键"而不是数据"并不是最佳选择:


The most efficient way in earlier versions would be using the $where syntax that allows a JavaScript expression to evaluate. Not that anything that evaluates JavaScript is the "most" efficient thing you can do, but analyzing "keys" as opposed to "data" is not optimal for any data store:

db[collname].find(function() { return Object.keys(this).some( k => /^fk/.test(k) ) })

内联function只是shell的简写,也可以写成:

The inline function there is just shell shorthand and this could also be written as:

db[collname].find({ "$where": "return Object.keys(this).some( k => /^fk/.test(k) )" })

$where 的唯一要求是表达式为您要返回的任何文档返回一个true值,因此文档不变地返回.

The only requirement for $where is that the expression returns a true value for any document you want to return, so the documents return unaltered.

这篇关于查询和过滤键名而不是MongoDB中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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