MongoDB删除元素取决于之前的元素(迭代) [英] MongoDB remove elements depending on element before (Iterating)

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

问题描述

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

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

{
   "_id": "5eabf8b144345b36b00bfbaa",
   "ranktime": [{
      "pos":"2",
      "datum":"Mon May 05 2020 12:22:52 GMT+0200 (GMT+02:00)",
      "source":"SOURCE2"
    },{
      "pos":"1",
      "datum":"Fri May 01 2020 12:23:10 GMT+0200 (GMT+02:00)",
      "source":"SOURCE1"
    },{
      "pos":"37",
      "datum":"Fri May 01 2020 12:25:14 GMT+0200 (GMT+02:00)",
      "source":"SOURCE2"
    },{
      "pos":"12",
      "datum":"Fri May 01 2020 12:25:14 GMT+0200 (GMT+02:00)",
      "source":"SOURCE2"
    },{
      "pos":"37",
      "datum":"Fri 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":"2",
      "datum":"Mon May 05 2020 12:22:52 GMT+0200 (GMT+02:00)",
      "source":"SOURCE2"
    },{
      "pos":"1",
      "datum":"Fri May 01 2020 12:23:10 GMT+0200 (GMT+02:00)",
      "source":"SOURCE1"
    },{
      "pos":"37",
      "datum":"Fri May 01 2020 12:25:14 GMT+0200 (GMT+02:00)",
      "source":"SOURCE2"
    }]
}

推荐答案

基本上,您可以使用 $ let $ arrayElemAt语句.新的 $ set 语法使您可以使用聚合在更新语句中:

Basically you can use $reduce to process an array and define previous element using $let and $arrayElemAt statements. The new $set syntax allows you to use aggregation within update statement:

db.col.updateMany({}, [
    {
        $set: {
            ranktime: {
                $reduce: {
                    input: "$ranktime",
                    initialValue: [],
                    in: {
                        $let: {
                            vars: { last: { $arrayElemAt: [ "$$value", -1 ] } },
                            in: {
                                $cond: [ 
                                    { 
                                        $and: [ 
                                            { "$eq": [ "$$last.source", "SOURCE2" ] },
                                            { "$eq": [ { $substr: [ "$$last.datum", 0, 15 ] }, { $substr: [ "$$this.datum", 0, 15 ] } ] },
                                        ]
                                    },
                                    "$$value",
                                    { $concatArrays: [ "$$value", [ "$$this" ] ] }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
])

聚合示例

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

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