【问题标题】:How to filter nested object property to update a document in Express and MongoDB?如何过滤嵌套对象属性以更新 Express 和 MongoDB 中的文档?
【发布时间】:2022-01-05 17:43:21
【问题描述】:

我想过滤电子邮件以更改 JSON 文件。我无法过滤对象的嵌套属性。如何做到这一点。

app.put("/users/:email", async (req, res) => {
          const email = req.params.email;
          //console.log(email);
          **const filter = { email: email };**
          const result = await userCollection.updateOne(filter, {
            $set: { value: "00" },
          });
          res.json(result);
        });

来自 MongoDB 的 JSON 文档:

[
  { "firstName" : "John",  
    "lastName"  : "Doe",
    "age"       : 23,
    "info"      : {"userID": 3434, "email": "test@test.com", "value": 35}
   },

  { "firstName" : "Mary",  
    "lastName"  : "Smith",
     "age"      : 32,
    "info"      : {"userID": 343, "email": "test2@test.com", "value": 65} }
]    
         

如何从 JSON 文件中过滤电子邮件?所以我可以通过过滤电子邮件来更新值。

【问题讨论】:

标签: node.js reactjs json mongodb express


【解决方案1】:

您必须使用这样的嵌套字段创建过滤器:

const filter = { "info.email": email };

【讨论】:

    猜你喜欢
    • 2021-02-13
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2018-08-08
    • 1970-01-01
    • 2022-01-24
    • 2011-10-17
    相关资源
    最近更新 更多