【发布时间】:2021-09-09 12:28:05
【问题描述】:
def method(class, doc): # read and fetch data from coll1 and get required fields
id = doc['id']
type = doc['type']
startdate = doc['startDate']
endDate = doc['endDate'])
match = class.mongo_db['coll2'] # match fields in coll1 and coll2 and fetch data from coll2
target = match.find({'id': id, 'type': type,'startDate':{'$lte': startdate},'endDate':{'$gte': endDate}})
Data = []
for data1 in target:
Data.append(data1)
if len(Data) == 1:
---------------
----------
----
--
else:
skip
我需要匹配两个集合中的数据并获取所需的数据,如果从 coll2 获取了两个文档,那么我们需要跳过 else 继续。
我在这方面取得了成功,没有遇到任何问题:
如果 coll2 有合适的 'endDate' 来匹配,但在某些情况下,'endDate' 会是 00,那么我需要考虑它有 20500202(如果结束数据是 00)我怎么能在上面的 mongo 中做到这一点?
查找查询,如果 'endDate' 为 00,我无法实现这一点
例如,coll1的结束日期是20210303,coll2的结束日期是00,那么我们应该认为它是
(20500202 > 20210303)
不是
(00 > 20210303)`
`
-- source collection coll1
{
"Id": "ID1",
"type": "DATA",
"startdate": 20200101,
"endDate": 20200301
}
-- collection coll2 which to match from coll1 ex_1
{
"Id": "ID1",
"type": "DATA",
"startdate": 20200101,
"endDate": 00,
"documentId": "DSC0001",
"documentDesc": "Value document"
}
匹配 id,type,startdate(coll1 endDate)
如果 coll2 的结束日期为零,那么我们需要将其视为
(20500202 > 20210301)
不是
(00 > 20210303)
所以 coll2 所需的输出是
{
"startdate": 20200101,
"endDate": 0,
"documentId": "DSC0001",
"documentDesc": "Value document"
}
--collection coll2 与 coll1 ex_2 匹配的集合
{
"Id": "ID1",
"type": "DATA",
"startdate": 20200101,
"endDate": 20200401,
"documentId": "DSC0001",
"documentDesc": "Value document"
}
如果 coll2 的 endDate 格式正确,那么我们可以直接将其视为
20200401>20210303
所以 coll2 所需的输出是
{
"startdate": 20200101,
"endDate": 0,
"documentId": "DSC0001",
"documentDesc": "Value document"
}
请建议我如何实现这一目标
提前致谢
【问题讨论】:
-
任何帮助将不胜感激,请就此提出建议
-
请以正确的 JSON 格式提供输入文档的示例,以及所需的结果(也以正确的 JSON 格式)。
-
@barrypicker 添加请告诉我它是否有用
标签: python python-3.x mongodb if-statement pymongo