Alexander Riedel
Alexander Riedel

Reputation: 1359

MongoDB remove elements depending on all other elements (Iterating)

I want to remove ($reduce) elements from my MongoDB Objects with condition if the same Object has a similar element. My Object:

  {
    "_id": "5eabf8b144345b36b00bfbaa",
    "ranktime": [
      {
        "pos": "15",
        "datum": "Mon May 01 2020 12:25:14 GMT+0200 (GMT+02:00)",
        "source": "SOURCE2"
      },
      {
        "pos": "10",
        "datum": "Fri May 05 2020 12:25:14 GMT+0200 (GMT+02:00)",
        "source": "SOURCE2"
      },
      {
        "pos": "15",
        "datum": "Mon May 01 2020 18:45:27 GMT+0200 (GMT+02:00)",
        "source": "SOURCE2"
      },
      {
        "pos": "20",
        "datum": "Fri May 05 2020 18:45:27 GMT+0200 (GMT+02:00)",
        "source": "SOURCE1"
      },
      {
        "pos": "10",
        "datum": "Fri May 05 2020 12:25:14 GMT+0200 (GMT+02:00)",
        "source": "SOURCE2"
      },
      {
        "pos": "15",
        "datum": "Mon May 01 2020 18:45:27 GMT+0200 (GMT+02:00)",
        "source": "SOURCE2"
      }
    ]
  }

So I want to remove the entry in ranktime if ranktime.source == "SOURCE2" and if the date is the same as with the object before. Actually I have to iterate through the single elements of ranktime. Is this possible in MongoDB ?

The Expected outcome would be:

  {
    "_id": "5eabf8b144345b36b00bfbaa",
    "ranktime": [
      {
        "pos": "15",
        "datum": "Mon May 01 2020 12:25:14 GMT+0200 (GMT+02:00)",
        "source": "SOURCE2"
      },
      {
        "pos": "10",
        "datum": "Fri May 05 2020 12:25:14 GMT+0200 (GMT+02:00)",
        "source": "SOURCE2"
      },
      {
        "pos": "20",
        "datum": "Fri May 05 2020 18:45:27 GMT+0200 (GMT+02:00)",
        "source": "SOURCE1"
      }
    ]
  }

Upvotes: 1

Views: 76

Answers (1)

mickl
mickl

Reputation: 49945

So based on your example you want to output the ranktime unless it is SOURCE2 and the same date has already been added to the output (but only for SOURCE2).

You can use $reduce as previously but you need to scan previosly added elements which can be achieved using $anyElementTrue operator and since your output contains the third element I'm assuming the repeated date is a stop condition only if the same date has been added for SORUCE2 so $filter is also needed to prepare the set of previosly added SOURCE2s:

db.col.updateMany({}, [
    {
        $set: {
            ranktime: {
                $reduce: {
                    input: "$ranktime",
                    initialValue: [],
                    in: {
                        $cond: [ 
                            { 
                                $and: [ 
                                    { "$eq": [ "$$this.source", "SOURCE2" ] },
                                    {
                                        $anyElementTrue: {
                                            $map: {
                                                input: { $filter: { input: "$$value", as: "prev", cond: { $eq: { "$$prev.source", "SOURCE2" } } } }, // already added SOURCE2 elements
                                                as: "addedElement",
                                                in: { "$eq": [ { $substr: [ "$$addedElement.datum", 0, 15 ] }, { $substr: [ "$$this.datum", 0, 15 ] } ] }
                                            }                        
                                        }
                                    }
                                ]
                            },
                            "$$value", // skip current element ($$this) 
                            { $concatArrays: [ "$$value", [ "$$this" ] ] } // add current element to the output
                        ]
                    }
                }
            }
        }
    }
])

Mongo Playground

Upvotes: 1

Related Questions