PyMongo-通过正则表达式从集合中选择子文档 [英] PyMongo- selecting sub-documents from collection by regex

查看:43
本文介绍了PyMongo-通过正则表达式从集合中选择子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们以以下集合为例:

Lets take for example the following collections:

{
    '_id': '0',
    'docs': [
        {'value': 'abcd', 'key': '1234'},
        {'value': 'abef', 'key': '5678'}
    ]
}
{
    '_id': '1',
    'docs': [
        {'value': 'wxyz', 'key': '1234'},
        {'value': 'abgh', 'key': '5678'}
    ]
}

我希望能够仅选择文档"列表下值"包含字符串ab"的子文档.我期望得到的是以下集合:

I want to be able to select only the sub-documents under the 'docs' list which 'value' contains the string 'ab'. What I'm expecting to get is the following collections:

{
    '_id': '0',
    'docs': [
        {'value': 'abcd', 'key': '1234'},
        {'value': 'abef', 'key': '5678'}
    ]
}
{
    '_id': '1',
    'docs': [
        {'value': 'abgh', 'key': '5678'}
    ]
}

从而过滤掉不匹配的子文档.

Thus, filtering out the unmatched sub-documents.

推荐答案

您需要一个聚合管道,分别匹配每个子文档,然后将匹配的子文档重新加入数组:

You need an aggregation pipeline that matches each subdocument separately, then re-joins the matching subdocuments into arrays:

from pprint import pprint
from bson import Regex

regex = Regex(r'ab')
pprint(list(col.aggregate([{
    '$unwind': '$docs'
}, {
    '$match': {'docs.value': regex}
}, {
    '$group': {
        '_id': '$_id',
        'docs': {'$push': '$docs'}
    }
}])))

我假设col"是一个指向您的 PyMongo Collection 对象的变量.这输出:

I assume "col" is a variable pointing to your PyMongo Collection object. This outputs:

[{u'_id': u'1', 
  u'docs': [{u'key': u'5678', u'value': u'abgh'}]},
 {u'_id': u'0',
  u'docs': [{u'key': u'1234', u'value': u'abcd'},
            {u'key': u'5678', u'value': u'abef'}]}]

字符串的r"前缀使其成为 Python 的原始"字符串,以避免任何正则表达式代码的问题.在这种情况下,正则表达式只是ab",因此r"前缀不是必需的,但现在这是一个很好的做法,因此您将来不会犯错误.

The "r" prefix to the string makes it a Python "raw" string to avoid any trouble with regex code. In this case the regex is just "ab" so the "r" prefix isn't necessary, but it's good practice now so you don't make a mistake in the future.

这篇关于PyMongo-通过正则表达式从集合中选择子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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