【发布时间】:2014-06-06 02:41:35
【问题描述】:
我正在尝试使用 REGEX 在 pymongo 中创建搜索。比赛结束后,我希望将数据附加到模块中的列表中。我以为我已经设置了所有内容,但是无论我为 REGEX 设置什么,它都会返回 0 个结果。代码如下:
REGEX = '.*\.com'
def myModule(self, data)
#after importing everything and setting up the collection function in the DB I call the following:
cursor = collection.find({'multiple.layers.of.data' : REGEX})
data = []
for x in cursor:
matches.append(x)
return matches
这只是我用来过滤存储在 mongodb 中的大量 json 文件的三个模块中的一个。但是,无论我更改此格式(例如 /.*.com/ 以在操作中声明或在 mongo 中使用 $regex...)它永远不会找到我的数据并将其附加到列表中。
编辑:添加完整代码以及我要识别的内容:
RegEx = '.*\.com' #Or RegEx = re.compile('.*\.com')
def filterData(self, data):
db = self.client[self.dbName]
collection = db[self.collectionName]
cursor = collection.find({'data.item11.sub.level3': {'$regex': RegEx}})
data = []
for x in cursor:
data.append(x)
return data
我正在尝试解析 mongodb 中的 JSON 数据。数据的结构如下:
"data": {
"0": {
"item1": "something",
"item2": 0,
"item3": 000,
"item4": 000000000,
"item5": 000000000,
"item6": "0000",
"item7": 00,
"item8": "0000",
"item9": 00,
"item10": "useful",
"item11": {
"0000": {
"sub": {
"level": "letter",
"level1": 0000,
"level2": 0000000000,
"level3": "domain.com"
},
"more_data": "words"
}
}
}
更新:经过进一步测试后,似乎我需要在搜索中包含所有图层。因此,它应该看起来像
collection.find({'data.0.item11.0000.sub.level3': {'$regex': RegEx}})。
但是,“0”可以是 1 - 50,“0000”是随机生成的。有没有办法将这些设置为索引作为变量,以便无论值是什么它都会进入它?它始终是一个数字值。
【问题讨论】:
标签: python regex mongodb pymongo