如何在猫鼬中动态地将值推入对象的属性?
how to push value in to object's property dynamically in mongoose?
我想根据使用 mongoose 从前端收到的 属性 值将值推入数组。
架构:
authencity: {
fake: [],
notSure: [],
authentic: [],
}
现在我想根据从前端接收到的 属性 值将值推入数组
const result = await News.findOneAndUpdate({ name: newsName }, { $push: { `authencity[${authencityType}]`: email } });
authencityType 可以是 fake、notSure、authentic 之类的东西,基于这个值我想将该值插入到相应的数组中。
示例数据:
Sample Document :{
"_id": {
"$oid": "5f0af1e09a724b06c86bfaec"
},
"authencity": {
"fake": [],
"notSure": [],
"authentic": []
},
"name": "Rajasthan deputy CM Sachin Pilot\u2019s supporters come to Gurugram resort, call police summon a joke",
"description": "",
"url": "https://news.google.com/__i/rss/rd/articles/CBMiqwFodHRwczovL3d3dy5oaW5kdXN0YW50aW1lcy5jb20vaW5kaWEtbmV3cy9yYWphc3RoYW4tZGVwdXR5LWNtLXNhY2hpbi1waWxvdC1zLXN1cHBvcnRlcnMtY29tZS10by1ndXJ1Z3JhbS1yZXNvcnQtY2FsbC1wb2xpY2Utc3VtbW9uLWEtam9rZS9zdG9yeS1ZQ1BQVDFlandtb1RoQXJaRXVabmNOLmh0bWzSAa0BaHR0cHM6Ly9tLmhpbmR1c3RhbnRpbWVzLmNvbS9pbmRpYS1uZXdzL3JhamFzdGhhbi1kZXB1dHktY20tc2FjaGluLXBpbG90LXMtc3VwcG9ydGVycy1jb21lLXRvLWd1cnVncmFtLXJlc29ydC1jYWxsLXBvbGljZS1zdW1tb24tYS1qb2tlL3N0b3J5LVlDUFBUMWVqd21vVGhBclpFdVpuY05fYW1wLmh0bWw?oc=5",
"category": "general",
"language": "en",
"country": "in",
"__v": 0
}
接收到的数据:电子邮件:abcd@gmail.com authencityType 可能是假的、不确定的或真实的
我想根据收到的 authencityType 将值推送到特定数组中
您需要使用 the dot notation 而不是方括号来定义您的路径:
`authencity.${authencityType}`
比如authenticity.fake
尝试:
const result = await News.findOneAndUpdate({ name: newsName }, { $push: { `authencity.${authencityType}`: email } }, { new: true });
另请注意,在 mongoose 的 findOneAndUpdate 版本中,您需要传递 new: true 参数才能 return 修改文档
const newColumn = `authencity.${authencityType}`;
const result = await News.findOneAndUpdate({ name: newsName }, { $push: { [newColumn]: email } }, { new: true });
在这里,我创建了一个动态字符串来使用模板文字构造列名,然后用 [] 将其括起来,因此 mongo 将认为这是一个列名
我想根据使用 mongoose 从前端收到的 属性 值将值推入数组。
架构:
authencity: {
fake: [],
notSure: [],
authentic: [],
}
现在我想根据从前端接收到的 属性 值将值推入数组
const result = await News.findOneAndUpdate({ name: newsName }, { $push: { `authencity[${authencityType}]`: email } });
authencityType 可以是 fake、notSure、authentic 之类的东西,基于这个值我想将该值插入到相应的数组中。
示例数据:
Sample Document :{
"_id": {
"$oid": "5f0af1e09a724b06c86bfaec"
},
"authencity": {
"fake": [],
"notSure": [],
"authentic": []
},
"name": "Rajasthan deputy CM Sachin Pilot\u2019s supporters come to Gurugram resort, call police summon a joke",
"description": "",
"url": "https://news.google.com/__i/rss/rd/articles/CBMiqwFodHRwczovL3d3dy5oaW5kdXN0YW50aW1lcy5jb20vaW5kaWEtbmV3cy9yYWphc3RoYW4tZGVwdXR5LWNtLXNhY2hpbi1waWxvdC1zLXN1cHBvcnRlcnMtY29tZS10by1ndXJ1Z3JhbS1yZXNvcnQtY2FsbC1wb2xpY2Utc3VtbW9uLWEtam9rZS9zdG9yeS1ZQ1BQVDFlandtb1RoQXJaRXVabmNOLmh0bWzSAa0BaHR0cHM6Ly9tLmhpbmR1c3RhbnRpbWVzLmNvbS9pbmRpYS1uZXdzL3JhamFzdGhhbi1kZXB1dHktY20tc2FjaGluLXBpbG90LXMtc3VwcG9ydGVycy1jb21lLXRvLWd1cnVncmFtLXJlc29ydC1jYWxsLXBvbGljZS1zdW1tb24tYS1qb2tlL3N0b3J5LVlDUFBUMWVqd21vVGhBclpFdVpuY05fYW1wLmh0bWw?oc=5",
"category": "general",
"language": "en",
"country": "in",
"__v": 0
}
接收到的数据:电子邮件:abcd@gmail.com authencityType 可能是假的、不确定的或真实的
我想根据收到的 authencityType 将值推送到特定数组中
您需要使用 the dot notation 而不是方括号来定义您的路径:
`authencity.${authencityType}`
比如authenticity.fake
尝试:
const result = await News.findOneAndUpdate({ name: newsName }, { $push: { `authencity.${authencityType}`: email } }, { new: true });
另请注意,在 mongoose 的 findOneAndUpdate 版本中,您需要传递 new: true 参数才能 return 修改文档
const newColumn = `authencity.${authencityType}`;
const result = await News.findOneAndUpdate({ name: newsName }, { $push: { [newColumn]: email } }, { new: true });
在这里,我创建了一个动态字符串来使用模板文字构造列名,然后用 [] 将其括起来,因此 mongo 将认为这是一个列名