【问题标题】:Find field between two documents with multiple condition in Python在Python中查找具有多个条件的两个文档之间的字段
【发布时间】: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


【解决方案1】:

您正在寻找的只有在 MongoDB Aggregation 中才有可能。

如果我的理解正确,以下代码就是您要查找的代码。

target = match.aggregate([
  {
    "$match": {  # <-- Put all your conditions except for `endDate`
      'id': id, 
      'type': type,
      'startDate':{
        '$lte': startdate
      }
    },
  },
  {
    "$project": {  # <-- Keys you want to be projected from `coll2`
      "documentId": 1,
      "documentDesc": 1,
      "startdate": 1,
      "endDate": {
        "$cond": {
          "if": {
            "$in": [
              "$endDate",
              [  #  <-- Add all your exceptions here
                0,
                "00",
              ]
            ]
          },
          "then": 20500202,
          "else": "$endDate",
        }
      }
    },
  },
  {
    "$match": {  # <-- Add your `endDate` condition here
      "endDate": {
        "$gte": endDate,
      }
    },
  },
])

【讨论】:

  • 是的,这就是我正在寻找的,但我将在目标中获得什么价值,整个 coll2?因为我需要在匹配后从 coll2 中获取 documentId,documentDesc,startdate
  • @ramesh 我已经更新了答案。看看这是不是你要找的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 2016-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-30
  • 1970-01-01
相关资源
最近更新 更多