简体   繁体   English

如何过滤嵌套的 object 属性以更新 Express 和 MongoDB 中的文档?

[英]How to filter nested object property to update a document in Express and MongoDB?

I want to filter the email to change the JSON file.我想过滤 email 以更改 JSON 文件。 I can not filter the nested property of the object.我无法过滤 object 的嵌套属性。 How to do that.怎么做。

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);
        });

A JSON document from MongoDB:来自 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} }
]    
         

How to filter the email from the JSON file?如何从 JSON 文件中过滤 email? So I can update the value by filtering the email.所以我可以通过过滤 email 来更新值。

You must create a filter with a nested field like this:您必须创建一个带有嵌套字段的过滤器,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更新 MongoDB 中嵌套 object 中的文档数据? - How to update document data in nested object in MongoDB? 无法使用来自节点 api 的嵌套 object 属性更新 mongodb 文档 - Unable to update mongodb document with nested object property from node api 在 Express 路由中更新文档中的属性(Mongoose、MongoDB、Express) - Update a property in document in an Express route (Mongoose, MongoDB, Express) 如何使用node / express应用正确插入/更新嵌套的mongodb文档 - how to properly insert/update a nested mongodb document using node/express app Mongodb嵌套文档更新? - Mongodb Nested document update? 如何更新mongodb中的嵌套数组文档 - How to update nested array document in mongodb 如何根据嵌套的 object 过滤 mongo 文档? - How to filter mongo document based on nested object? 如何通过Mongoose Node.js ORM更新MongoDB文档中的嵌套对象字段? - How to update nested object field in MongoDB document via Mongoose Node.js ORM? 如何复制整个 JavaScript object 以更新包含嵌套 arrays 和子文档的 MongoDB/Mongoose 文档? - How can I copy an entire JavaScript object to update a MongoDB/Mongoose document, that contains nested arrays and subdocuments? 如何在嵌套的 object 属性上使用 MongoDB $ne - How to use MongoDB $ne on nested object property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM