[英]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.