简体   繁体   中英

Find field between two documents with multiple condition in Python

   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

      
      
      

I need to match data from both collections and fetch required data, if two documents got fetched from coll2 then we need to skip else proceed.

I am successful in this with out having an issue:

if coll2 has proper 'endDate' to match, but in some scenarios the the 'endDate' would be 00 then I need to consider it has 20500202 (IF end data was 00) how can I do this in above mongo?

Find query, I am unable to achieve this if 'endDate' is 00

For example, if enddate of coll1 is 20210303, and end date of coll2 is 00, then we should consider it as

(20500202 > 20210303) 

not

(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"
}

match id,type,startdate<(coll1 startdate),endDate>(coll1 endDate)

if end date of coll2 is zero then we need to consider it as

(20500202 > 20210301)

not

(00 > 20210303) 

so the desired output needed from coll2 is

{
    "startdate": 20200101,
    "endDate": 0,
    "documentId": "DSC0001",
    "documentDesc": "Value document"
}

-- collection coll2 which to match from coll1 ex_2

{
    "Id": "ID1",
    "type": "DATA",
    "startdate": 20200101,
    "endDate": 20200401,
    "documentId": "DSC0001",
    "documentDesc": "Value document"
}

if endDate of coll2 is in proper format then we can directly consider it as

20200401>20210303

so the desired output needed from coll2 is

{
    "startdate": 20200101,
    "endDate": 0,
    "documentId": "DSC0001",
    "documentDesc": "Value document"
}

Please suggest me how to achieve this

thanks in advance

What you are looking for is possible only in MongoDB Aggregation.

The below code is what you are looking for if my understanding is right.

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,
      }
    },
  },
])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM