繁体   English   中英

Mongodb - 子文档的键的正则表达式匹配

[英]Mongodb - regex match of keys for subdocuments

我有一些文档保存在一个集合(称为urls )中,如下所示:

{
    payload:{
        url_google.com:{
            url:'google.com',
            text:'search'
        }
    }
},
{
    payload:{
        url_t.co:{
            url:'t.co',
            text:'url shortener'
        }
    }
},
{
    payload:{
        url_facebook.com:{
            url:'facebook.com',
            text:'social network'
        }
    }
}

使用 mongo CLI,是否可以查找与/^url_/匹配的payload子文档? 而且,如果可能的话,是否也可以查询匹配的子文档(例如,确保text存在)?

我在想这样的事情:

db.urls.find({"payload":{"$regex":/^url_/}}).count();

但这会返回 0 结果。

任何帮助或建议都会很棒。

谢谢,

马特

不可能以这种方式查询文档键。 您可以使用$exists搜索精确匹配项,但找不到与模式匹配的键名。

我假设(可能是错误的)您正在尝试查找具有 URL 子文档的文档,并且并非所有文档都有这个? 为什么不将该类型信息下推一个级别,例如:

{
  payload: {
    type: "url",
    url: "Facebook.com",
    ...
  }
}

然后你可以查询:

db.foo.find({"payload.type": "url", ...})

如果我没有注意到你不应该使用点( . )是 MongoDB 中的键名,我也会失职。 在某些情况下,可以像这样创建文档,但是当您尝试查询嵌入的文档时会造成很大的混乱(可以这么说,Mongo 使用点作为“路径分隔符”)。

您可以这样做,但您需要使用聚合:聚合是将每个阶段应用于每个文档的管道。 您有各种各样的阶段来执行各种任务。

我为这个特定问题编写了一个聚合管道。 如果您不需要计数但需要文档本身,您可能需要查看$replaceRoot阶段。

db.getCollection('urls').aggregate([
    {
        // creating a nested array with keys and values
        // of the payload subdocument.
        // all other fields of the original document
        // are removed and only the filed arrayofkeyvalue persists
        "$project": {
            "arrayofkeyvalue": {
                "$objectToArray": "$$ROOT.payload"
            }
        }
    },
    {
        "$project": {
            // extract only the keys of the array
            "urlKeys": "$arrayofkeyvalue.k"
        }
    },
    {
        // merge all documents
        "$group": {
            // _id is mandatory and can be set
            // in our case to any value
            "_id": 1,
            // create one big (unfortunately double
            // nested) array with the keys
            "urls": {
                "$push": "$urlKeys"
            }
        }
    },
    {
        // "explode" the array and create
        // one document for each entry
        "$unwind": "$urls"
    },
    {
        // "explode" again as the arry
        // is nested twice ...
        "$unwind": "$urls"
    },
    {
        // now "query" the documents
        // with your regex
        "$match": {
            "urls": {
                "$regex": /url_/
            }
        }
    },
    {
      // finally count the number of
      // matched documents
        "$count": "count"
    }
])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM