MongoDB删除依赖于所有其他元素的元素(迭代) [英] MongoDB remove elements depending on all other elements (Iterating)

查看:55
本文介绍了MongoDB删除依赖于所有其他元素的元素(迭代)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果同一对象具有相似的元素,我想有条件地从我的MongoDB对象中删除($ reduce)元素.我的对象:

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

因此,如果ranktime.source =="SOURCE2",则我想删除ranktime中的条目.日期是否与之前的对象相同.实际上,我必须遍历ranktime的单个元素.在MongoDB中有可能吗?

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 ?

预期结果将是:

  {
    "_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"
      }
    ]
  }

推荐答案

因此,根据您的示例,您要输出 ranktime 除非,它是 SOURCE2 已将相同的日期添加到输出中(但仅适用于 SOURCE2 ).

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).

您可以像以前一样使用 $ reduce ,但是您需要扫描以前添加的元素,可以使用 $ filter 是还需要准备一组预先添加的 SOURCE2 s:

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

蒙戈游乐场

这篇关于MongoDB删除依赖于所有其他元素的元素(迭代)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆