[英]How to filter nested object property to update a document in Express and MongoDB?
我想過濾 email 以更改 JSON 文件。 我無法過濾 object 的嵌套屬性。 怎么做。
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 文件中過濾 email? 所以我可以通過過濾 email 來更新值。
您必須創建一個帶有嵌套字段的過濾器,如下所示:
const filter = { "info.email": email };
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.