【问题标题】:remove duplicate document on the basis of condition in mongo aggregation pipeline根据 mongo 聚合管道中的条件删除重复文档
【发布时间】:2019-03-07 22:32:34
【问题描述】:

我有以下文件

{
"_id": "5c6eabcc813ea3fabf972eaa",
"employee_id": "abc",
"name": "singh",
"salary": 0
},
{
"_id": "5c6eabcc813ea3fabf972eaa",
"employee_id": "abc",
"name": "singh",
"salary": 0
},
{
"_id": "5c6eabcc813ea3fabf972eaa",
"employee_id": "abc",
"name": "singh",
"salary": {
"_id": "5c80ddc1ef020744825efe9c",
"month": 2,
"year": 2019
}

如果salary.month 不等于2,我想删除与上面数据集相同的“_id”值的文档。我正在使用聚合管道。请帮助我。

提前致谢。

【问题讨论】:

    标签: mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:

    下面给出的查询可以为您完成。

    db.getCollection('CollectionName').deleteMany({ $where : function() { return (this._id === this.salary._id && this.salary.month === 2) }})
    

    【讨论】:

    • 非常感谢您的回复。你能告诉我如何在条件下进行查找意味着只有在条件满足时才加入
    • 对不起。我无法理解你的评论。你能简单地告诉我你在找什么吗?
    • 我有两个集合,我只想在第二个集合字段值满足某些条件时才进行左外连接(仅当薪水 >10 时才进行 exaplme 连接)
    • 你可以使用 $lookup 和 $match 来做到这一点。例如,请参阅docs.mongodb.com/manual/reference/operator/aggregation/lookup。 $lookup 应该有连接条件并且 $match 包含工资 > 10。
    • 这是加入后的情况。只有条件满足才需要加入
    猜你喜欢
    • 1970-01-01
    • 2017-01-14
    • 2021-08-18
    • 2017-08-29
    • 2018-03-28
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多