更新所有在Mongoose中不具有相同名称的字段(Update All Fields which is don't have the same name in Mongoose)
我试图根据req.body更新Document中的所有字段。
在这种情况下,Schema没有在Mongoose中定义。 所以,我可以自由填充将要发布到模式中的内容
例如,我在Document中有2个这样的Scheme:
模式1:
{ "name": { "type": "text", "value": "Afdallah" }, "item": { "type": "text", "value": "Books" } }
另一个就是这样
模式2:
{ "name": { "type": "text", "value": "Afdallah" }, "email": { "type": "email", "value": "afdallah.war@gmail.com" } }
我的问题是,如何更新所有领域时,他们没有相同的字段名称?
我尝试这样更新字段。
const output = await Order.findOne({ _id: req.params.id }); try { output.set(req.body); await output.save(); res.send("Success Update"); } catch (err) { res.status(422).send(err); }
I'm trying to update all fields in Document based on req.body.
In this case, the Schema are not defined in Mongoose. So, I can fill freely what will be in post to schema
For example, I've 2 Scheme like this in the Document:
Schema 1:
{ "name": { "type": "text", "value": "Afdallah" }, "item": { "type": "text", "value": "Books" } }
And the other is like this
Schema 2:
{ "name": { "type": "text", "value": "Afdallah" }, "email": { "type": "email", "value": "afdallah.war@gmail.com" } }
My question is, how to update all fields when they don't have same fields name?
I've try like this to update the fields.
const output = await Order.findOne({ _id: req.params.id }); try { output.set(req.body); await output.save(); res.send("Success Update"); } catch (err) { res.status(422).send(err); }
原文:https://stackoverflow.com/questions/48453810