如果找到 string/pattern,则将其从列表中删除

If string/pattern is found then remove it from the list

我有一个 JSON 文件,我正在使用 PyMongo.

从 MongoDB 数据库中检索该文件

我通过执行以下操作将 Python 游标转换为列表:

db = mongo_client.test_db
table = db.test_table
doc = list(table.find())

现在我要做的是检查列表是否包含特定字符串,如果包含它应该只保留字符串的子字符串。

列表:

doc=[{'_id': ObjectId('5f45228293d7b757bcbd2d67'),'features': [DBRef('featId', ObjectId('5f452e3793d7b757bcbd2d88'))]}]

所以目前我正在使用下面的代码来检查 DBRef 是否存在于列表中,该列表工作正常。 但是我不太确定用什么替换打印语句,以便只保留 ObjectId.

if "DBRef" in str(doc):
    print("its here") #remove the dbref and only keep alphanumeric value
else:
    print("not here") #do nothing to the list

预期输出(如果在列表中找到 DBRef):

doc=[{'_id': ObjectId('5f45228293d7b757bcbd2d67'),'features': ObjectId('5f452e3793d7b757bcbd2d88')}]

据我了解,您在列表中有一个字典,因此您可以只更新其中的一些值。

不确定您想要的 doc 变量输出是什么,列表中有多少个字典等等,但是,这是我想出的:

  1. 找到一个值,需要使用正则表达式或仅通过引用对象 ID 来保存
  2. 使用所需的 key:value 对
  3. 创建临时字典
  4. 更新列表中现有的字典

doc[0] 指的是列表的第一个元素。

'''python

选项 1:

#regex version in case saving ObjectId as bson.objectid is not required

import re

object_value = re.search("DBRef\('featId', ObjectId\((.*?)\)", str(doc[0]['features'])).group(1)
d1 = {'features': f'ObjectId({object_value})'}
doc[0].update(d1)

选项 2:

#accessing the required value directly and saving it as ObjectId
from bson.objectid import ObjectId
from bson.dbref import DBRef

object_value_no_regex = doc[0]['features'][0].id
d1 = {'features': ObjectId(object_value_no_regex)}
doc[0].update(d1)

谢谢!