【问题标题】:mongodb group and count, match and update the other fieldsmongodb 对其他字段进行分组和计数、匹配和更新
【发布时间】:2018-08-17 16:10:47
【问题描述】:

我有以下字段

mongodb 中的 e_id、c_id、m_id 必须在 id传参。

data -> 是多个 event_id、coor_id、manage_id

  {
        "_id" : "1",
        "event_id" : "abscdsadsad890",
        "coor_id" : "cdaddsadewqwae889",
        "manage_id" : "fdfdfsdfdsfsdf908",
        "active" : true
        "date" : ISODate("2014-01-14T00:00:00Z"),
    }

     {
        "_id" : "1",
        "event_id" : "abscdsadsad890",
        "coor_id" : "cdaddsadewqwae889",
        "manage_id" : "fdfdfsdfdsfsdf908",
        "active" : true
        "date" : ISODate("2014-01-14T00:00:00Z"),
    }

 {
        "_id" : "1",
        "event_id" : "abscdsadsad890",
        "coor_id" : "cdaddsadewqwae889",
        "manage_id" : "fdfdfsdfdsfsdf908",
        "active" : true
        "date" : ISODate("2014-01-14T00:00:00Z"),
    }

预期结果:

  {
            "_id" : "1",
            "event_id" : "abscdsadsad890",
            "coor_id" : "cdaddsadewqwae889",
            "manage_id" : "fdfdfsdfdsfsdf908",
            "active" : true
            "date" : ISODate("2014-01-14T00:00:00Z"),
        }

         {
            "_id" : "1",
            "event_id" : "abscdsadsad890",
            "coor_id" : "cdaddsadewqwae889",
            "manage_id" : "fdfdfsdfdsfsdf908",
            "active" : false
            "date" : ISODate("2014-01-14T00:00:00Z"),
        }

     {
            "_id" : "1",
            "event_id" : "abscdsadsad890",
            "coor_id" : "cdaddsadewqwae889",
            "manage_id" : "fdfdfsdfdsfsdf908",
            "active" : false
            "date" : ISODate("2014-01-14T00:00:00Z"),
        }

预期的结果应该是非最新的 记录为活动的虚假和最新的 每个创建的新记录都必须处于活动状态 新记录应该是活动的 = true 和其他 相同的 event_id、coor_id 和 manage_id 必须 活跃=假

我正在尝试使用匹配所有结果,然后分组和计数, 之后如果count大于1,则获取最新的文档找到结果 并更新其余匹配结果,但在 mongodb 我也想这么写。有什么建议吗?

【问题讨论】:

  • 请添加示例数据和预期结果。
  • @Akrion :我也更新了问题和预期结果,但如果只找到一条记录,它也应该保持活动状态,如果超过 1 条,那么旧的记录将被更新。

标签: mongodb aggregation-framework


【解决方案1】:

我设法通过 c# 来做到这一点

首先是我将 event_id、coor_id 和 manage_id 传入 创造。然后使用我做的 c# mongodb 驱动程序是

var list = await collection.AsQueryable().Where(x => c.id == c.id && m.id == m.id).toList();

然后循环并检查第一个不能通过_id等于其他人

然后更新除第一个列表之外的其他列表。

完成...谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 2020-07-30
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多