【问题标题】:MongoDB update where multiple condition matchMongoDB更新多个条件匹配
【发布时间】:2018-08-27 08:07:51
【问题描述】:

我有以下 mongoDB 文档:

        {
        "_id" : "5b76c3c037548390fdb5b40e",
        "userId" : "4601",
        "news" : [
            {
                "newsId" : "1",
                "progress" : "0",
            },
            {
                "newsId" : "2",
                "progress" : "100",
            },
            {
                "newsId" : "3",
                "progress" : "45",
            },
        ],
        "modified" : ISODate("2018-08-21T19:13:43.301+05:30"),
        "created" : ISODate("2018-08-21T19:13:43.301+05:30"),
        "completionStatus" : "0",
    },
    {
        "_id" : "5b76c3c037548390fdb5b40e",
        "userId" : "1234",
        "news" : [
            {
                "newsId" : "2",
                "progress" : "100",
            },
            {
                "newsId" : "3",
                "progress" : "100",
            },
        ],
        "modified" : ISODate("2018-08-21T19:13:43.301+05:30"),
        "created" : ISODate("2018-08-21T19:13:43.301+05:30"),
        "completionStatus" : "0",
    }

news 数组可以包含 n 个 JSON。该数组的一个用户数为 3,其他用户数为 2。

现在我想根据以下条件将 completionStatus 更新为 1:

条件 1:修改文档的日期是今天。已经有一个修改过的键。

条件2:新闻数组的每个元素的进度都是100。

【问题讨论】:

    标签: node.js mongodb mongoose nosql


    【解决方案1】:

    这应该可行:

    collection.update({
      modified: {
        $gte: startOfDay,
        $lte: endOfDay
      },
      news: {
        $not: {
          $elemMatch: {
            progress: {
              $ne: "100"
            }
          }
        }
      }
    }, {
      $set: {
        completionStatus: "1"
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多